1

I'm using Spring MVC. Why is data from my model not displaying in output.jsp?. I am working on a very easy example of data coming out on one page and being display on the 2nd back. I checked to see if the data is in the model and it is but I don't know why the 2nd JSP is not displaying it.

Here is my control:

@Controller
public class RequestController {


    private static final Logger LOGGER = getLogger(RequestController.class);

    @RequestMapping(value = "/request" , method = RequestMethod.GET)
       public ModelAndView displayRequestPage(@ModelAttribute("inputForm") InputForm inputForm) {

        return new ModelAndView("input");

    }


    @RequestMapping(value = "/request" , method = RequestMethod.POST)
    public ModelAndView displayOutputPage(@ModelAttribute("inputForm") InputForm inputForm) {

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("inputForm", inputForm);

        return new ModelAndView("display", model);

    }


}

here is my output JSP:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>JQuery Validation Engine</title>
</head>
<body>
 <center>
<h2>JQuery Examples</h2>
 </center>
        <p>Name: <c:out value="${inputForm.name}"/>
        <p>Phone(xxx)xxx-xxxxx: <c:out value="${inputForm.phone}"/>
        <p>Email: <c:out value="${inputForm.email}"/>

    <p>
     <b>Please  <a href="./request">click here</a>  to restart</b>
    </p>

</body>
</html>

this is what I am getting as output:

Name: ${inputForm.name}

Phone(xxx)xxx-xxxxx: ${inputForm.phone}

Email: ${inputForm.email}

Please click here to restart 

Here is my Spring MVC.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <mvc:annotation-driven />

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
          p:basenames="messages" />

    <!-- Declare the Interceptor -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
              p:paramName="locale" />
    </mvc:interceptors>


    <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>

    <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/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

I got it working but changing my web.xml.. Below is the working web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">


  <display-name>JQuery Example Web Application</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

and here is the web.xml that did not work:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

  <display-name>JQuery Example Web Application</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Can someone please tell me why change the web.xml made it work.

SJS
  • 5,607
  • 19
  • 78
  • 105

3 Answers3

2

Your model is only populated for POST requests (in displayOutputPage), but nowhere in your JSP do you submit a form. Therefore, you only ever call displayRequestPage and never provide a model (a <a href creates a GET request).

Furthermore, because you are not submitting a <form>, your inputForm will not have any data in it.

To check this, you can always set breakpoints and see which method is called.

EDIT:

I made the above assumption because you have the following link to restart the process:

<b>Please  <a href="./request">click here</a>  to restart</b>

And as your mapping for displayRequestPage is /request... As to the EL not being evaluated, it may just be that because you don't have an inputForm as you have no model and therefore the EL doesn't evaluate it just prints the text as is.

EDIT2:

The EL expressions did not work for you previously because Servlet Spec 2.3 does not support EL (was introduced with 2.4). As you had declared your web.xml to be using spec 2.3, it simply switched off the overhead of parsing for EL.

EDIT3:

Just to clarify the line

The line

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

told your servlet container to use spec 2.3

beny23
  • 34,390
  • 5
  • 82
  • 85
1

Try adding a model parameter to your controller methods and try to populate that model rather tha creating a new one.

Anubhab
  • 1,736
  • 2
  • 18
  • 28
0

See also: Basic Spring MVC Data Binding

A couple of things that could be wrong:

  • Make sure InputForm has getters/setters and a no-arg constructor
  • Try using Spring's bind tag or form tag.
  • Try just manually adding InputForm to the model (ie model.put()).

If your expecting InputForm to be data bound to the request you might need to use @Valid... you shouldn't but it would not hurt.

Finally on a successful POST you almost always want to redirect and not serve the response directly. If its an error you should serve the response.

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • I added but web.xml files. working and not work versions... can someone please tell me why it works now! – SJS Mar 08 '13 at 15:51