0

ZK @ModelAttribute is null

asked 2013-11-06 21:41:00 +0800

laldariz gravatar image laldariz
1

I'm new to Spring with ZK Studio and I'm encountering the following problem, please help me.

I have a web project that use jsp and works fine but when I try to use a forms zul of Zk Studio doesn´t works.

The problem occurs when i want to save the data. When I work in jsp the @ModelAttribute has data, but when I change to zul in the servlet the @ModelAttribute is null. Thank you very much.

This is the controller code:

@Controller
public class C_ILCD_LANGUAGESController extends SelectorComposer<Component>  {

    private static final long serialVersionUID = 1;

    @Autowired
    private C_ILCD_LANGUAGESService c_ilcd_languagesService;

    @RequestMapping("/index")
    public String setupFrom(Map<String, Object> map){
        C_ILCD_LANGUAGES c_ilcd_languages = new C_ILCD_LANGUAGES();
        map.put("c_ilcd_languages", c_ilcd_languages);
        map.put("c_ilcd_languagesList", c_ilcd_languagesService.getAllC_ILCD_LANGUAGES());
        return "c_ilcd_languages";
    }

    @RequestMapping(value="/addLanguage", method=RequestMethod.POST)
    public String addLanguage(@ModelAttribute C_ILCD_LANGUAGES c_ilcd_languages, BindingResult result, Map<String, Object> map){
        C_ILCD_LANGUAGES c_ilcd_languagesResult = new C_ILCD_LANGUAGES();
        c_ilcd_languagesService.add(c_ilcd_languages);
        c_ilcd_languagesResult = c_ilcd_languages;

        map.put("c_ilcd_languages", c_ilcd_languagesResult);
        map.put("c_ilcd_languagesList", c_ilcd_languagesService.getAllC_ILCD_LANGUAGES());
        return "c_ilcd_languages";
    }   
}

This is the jsp code (that works fine):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>C_ILCD_LANGUAGES Management</title>
</head>
<body>
<h1>C_ILCD_LANGUAGES Data</h1>
<form:form action="addLanguage" method="POST" commandName="c_ilcd_languages">
    <table>
        <tr>
            <td>Languages ID</td>
            <td><form:input path="LID" /></td>
        </tr>
        <tr>
            <td>VALUE</td>
            <td><form:input path="LVALUE" /></td>
        </tr>
        <tr>
            <td>DEFINITION</td>
            <td><form:input path="LDEFINITION" /></td>
        </tr>
        <tr>
            <td>DBT_ID</td>
            <td><form:input path="LDBTID" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="action" value="Add" />
            </td>
        </tr>
    </table>
</form:form>
<br>
    <table border="1">
        <th>Languages ID</th>
        <th>VALUE</th>
        <th>DEFINITION</th>
        <th>DBT_ID</th>
        <c:forEach items="${c_ilcd_languagesList}" var="c_ilcd_languages">
            <tr>
                <td>${c_ilcd_languages.LID}</td>
                <td>${c_ilcd_languages.LVALUE}</td>
                <td>${c_ilcd_languages.LDEFINITION}</td>
                <td>${c_ilcd_languages.LDBTID}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

This is the servlet code:

<?xml version="1.0" encoding="UTF-8"?>
    <context:annotation-config />
    <context:component-scan base-package="com.foo.WebMexicaniuhEE" />   


    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="com.foo.WebMexicaniuhEE.controller, org.zkoss.spring.beans.zkcomponents"></context:component-scan>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/zul/"/>
        <property name="suffix" value=".zul"/>
    </bean>
    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 
</beans>

This is the zul code:

<zk xmlns:n="native">
    <window apply="com.foo.WebMexicaniuhEE.controller.C_ILCD_LANGUAGESController">

        <n:form action="addLanguage" method="post" modelAttribute="c_ilcd_languages">
            <textbox id="txtLID1" value="@bind(c_ilcd_languages.LID)" width="30px"></textbox>
            <textbox id="txtLVALUE1" value="@bind(c_ilcd_languages.LVALUE)" width="150px"></textbox>
            <textbox id="txtLDEFINITION1" value="@bind(c_ilcd_languages.LDEFINITION)" width="150px"></textbox>
            <textbox id="txtLDBTID1" value="@bind(c_ilcd_languages.LDBTID)" width="30px"></textbox>
            <button label="Add" type="submit"/>
        </n:form>

        <grid id="gridLanguages" model="@bind(com.foo.WebMexicaniuhEE.model)" mold="paging" pageSize="10">
            <columns>
                <column label="LID" />
                <column label="LVALUE" />
                <column label="LDEFINITION" />
                <column label="LDBTID" />
            </columns>
            <rows>
                <row forEach="${c_ilcd_languagesList}">
                    <label value="${each.LID}"/>
                    <label value="${each.LVALUE}"/>
                    <label value="${each.LDEFINITION}"/>
                    <label value="${each.LDBTID}"/>
                </row>
            </rows>
          </grid> 
    </window>
</zk>

This is the web.xml code:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" > 

    <description><![CDATA[My ZK Application]]></description>
    <display-name>MexicaniuhEE</display-name>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- ZK -->
    <listener>
        <description>ZK listener for session cleanup</description>
        <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
    </listener>
    <servlet>
        <description>ZK loader for ZUML pages</description>
        <servlet-name>zkLoader</servlet-name>
        <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
        <init-param>
            <param-name>update-uri</param-name>
            <param-value>/zkau</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup><!-- Must -->
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zul</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zhtml</url-pattern>
    </servlet-mapping>
    </servlet>

    <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
      <param-name>useFileMappedBuffer</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
    <servlet-mapping>
        <servlet-name>auEngine</servlet-name>
        <url-pattern>/zkau/*</url-pattern>
    </servlet-mapping>

    <!-- [Optional] Session timeout -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.zul</welcome-file>
        <welcome-file>index.zhtml</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>
delete flag offensive retag edit

1 Answer

Sort by » oldest newest most voted
0

answered 2013-11-08 10:48:42 +0800

cor3000 gravatar image cor3000
6280 2 7

Hi,

you are mixing different things here. A ZK Composer is something different than a Spring MVC Controller, so think it doesn't make any sense to mix them.

When using this

<n:form action="addLanguage" method="post" modelattribute="c_ilcd_languages">

it is just writing a native html form, I think the modelAttribute-attribute is simply ignored when posting a form.

When posting the form, you'll completely leave the ZK lifecycle, and just post to the MVC Dispatcher servlet.

I am even wondering how anything else on the page could work (like: @bind(cilcdlanguages.LID)) do you actually get any value out of it?

I also don't see any DelegatingVariableResolver configured...

Please refer to this document how to communicate between ZK and Spring.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-11-06 21:41:00 +0800

Seen: 19 times

Last updated: Nov 08 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More