Announcement Announcement Module
Collapse
No announcement yet.
Spring tiles2 form submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring tiles2 form submit

    I've been struggling with this for a very long time, so I hope that someone here can help me.

    I have a simple test application, which contains a form to add delivery addresses. It's possible with Javascript to add rows to the table and bind them to a User object. I've only added a field to add a street and the number (String and Integer).

    The problem is, everything works fine when I don't use Tiles. I can add rows to the table, bind them to the objects and submit the form. When I add Tiles to the application, nothing works anymore.

    Some excerpts of the code:
    Code:
        <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/tiles-def.xml</value>
                </list>
            </property>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="order" value="1" />
            <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
        </bean>
        
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <value>
                    /welcome.html = welcomeController
                    /changeaddress.html = changeAddressController
                    /changeaddress.form = changeAddressFormController
                </value>
            </property>
        </bean>
    
        <bean id="welcomeController" class="org..controller.WelcomeController" />
    
        <bean id="changeAddressController" class="org.test.controller.ChangeAddressController" />
    
        <bean id="changeAddressFormController" class="org.test.controller.ChangeAddressFormController" />
    Tiles-definition:
    Code:
        <definition name="template" template="/WEB-INF/jsp/template.jsp" />
    
        <definition name="welcome" extends="template">
            <put-attribute name="content" value="/WEB-INF/jsp/welcome.jsp" />
        </definition>
    
        <definition name="changeaddress" extends="template">
            <put-attribute name="content" value="/WEB-INF/jsp/changeaddress.jsp" />
        </definition>
    
        <definition name="addresschanged" extends="template">
            <put-attribute name="content" value="/WEB-INF/jsp/addresschanged.jsp" />
        </definition>
    JSP:
    Code:
            <h4>Change addresses</h4>
    
            <form method="post" action="${pageContext.request.contextPath}/changeaddress.form">
                <table>
                    <tbody id="addressTable">
                        <tr>
                            <td>First name:</td>
                            <td><s:bind path="user.firstName"><input type="text" size="20" name="firstName" value="${status.value}" /> ${status.errorMessage}</s:bind></td>
                        </tr>
                        <tr>
                            <td>Last name:</td>
                            <td><s:bind path="user.lastName"><input type="text" size="20" name="lastName" value="${status.value}" /> ${status.errorMessage}</s:bind></td>
                        </tr>
                        <c:forEach items="${user.deliveryAddress}" var="deliveryAddress" varStatus="state">
                            <tr>
                                <td>Address ${state.index + 1}</td>
                            </tr>
                            <tr>
                                <td>Street:</td>
                                <td><s:bind path="user.deliveryAddress[${deliveryAddress.key}].street"><input type="text" size="20" name="${status.expression}" value="${status.value}"/></s:bind></td>
                            </tr>
                            <tr>
                                <td>Number:</td>
                                <td><s:bind path="user.deliveryAddress[${deliveryAddress.key}].number"><input type="text" size="3" name="${status.expression}" value="${status.value}" /></s:bind></td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
                <button onclick="addRow();">Add row</button>&nbsp;<input type="submit" value="Submit" />
    
                <script type="text/javascript">var counter = ${state.index + 1}</script>
            </form>
    Controller:
    Code:
    public class ChangeAddressFormController extends SimpleFormController
    {
        @Override
        public ModelAndView onSubmit(Object command)
        {
            User user = (User) command;
    
             for(Integer i : user.getDeliveryAddress().keySet())
            {
                Address u = user.getDeliveryAddress().get(i);
    
                System.out.println("Street: " + u.getStreet());
                System.out.println("Number: " + u.getNumber());
                System.out.println("******************************");
            }
           return new ModelAndView("addresschanged");
        }
    
        protected Object formBackingObject(HttpServletRequest request) throws ServletException
        {
            return new User();    
        }
    }
    When I submit the form, I get the following stacktrace:

    Code:
    org.apache.tiles.impl.CannotRenderException: JSPException including path '/WEB-INF/jsp/changeaddress.jsp'.
    	at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:679)
    	at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:631)
    	at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:307)
    
    Caused by: org.apache.tiles.util.TilesIOException: JSPException including path '/WEB-INF/jsp/changeaddress.jsp'.
    	at org.apache.tiles.servlet.context.ServletTilesRequestContext.wrapServletException(ServletTilesRequestContext.java:298)
    	at org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:82)
    
    Caused by: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    	at org.springframework.web.servlet.tags.BindTag.doStartTagInternal(BindTag.java:121)
    	at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)

  • #2
    Remove the spring tags & use ajax jquery for form submitting

    Comment

    Working...
    X