2013-03-08 65 views
1

我正在使用Spring MVC。为什么我的模型中的数据不会显示在output.jsp中?我正在研究一个非常简单的数据示例,这些数据在一页上显示出来,并在第二页显示。我检查数据是否在模型中,但是我不知道为什么第二个JSP不显示它。Spring MVC为什么我的模型中的数据不会显示在output.jsp中?

这里是我的控制:

@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); 

    } 


} 

这里是我的输出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> 

这是我得到的输出:

Name: ${inputForm.name} 

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

Email: ${inputForm.email} 

Please click here to restart 

这里是我的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> 

我得到它的工作,但改变我的web.xml ..下面是工作的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> 

,这里是没有工作的web.xml中:

<!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> 

灿有人请告诉我为什么改变web.xml使它工作。

+0

你使用了什么ViewResolver? – 2013-03-08 15:18:56

+0

<豆ID = “视图解析器” \t \t类= “org.springframework.web.servlet.view.UrlBasedViewResolver”> \t \t <属性名= “viewClass类” \t \t \t值=“org.springframework.web。 servlet.view.JstlView “/> \t \t <属性名= ”前缀“ 值= ”/ WEB-INF/JSP /“/> \t \t <属性名= ”后缀“ 值=”。JSP” /> \t – SJS 2013-03-08 15:19:26

+1

看起来你的EL没有被评估。你可能想试试这个:http://stackoverflow.com/q/1721518/738746。 – 2013-03-08 15:20:02

回答

2

您的模型仅填充POST请求(位于displayOutputPage),但您的JSP中无处提交表单。因此,您只需拨打displayRequestPage并永不提供模型(<a href创建GET请求)。

此外,由于您未提交<form>,因此您的inputForm将不会包含任何数据。

要检查这一点,您可以随时设置断点并查看调用哪个方法。

编辑:

我作出了上述假设,因为你有以下链接重新启动该进程:

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

而且作为你displayRequestPage映射/request ...至于EL不是评估,它可能只是因为你没有inputForm,因为你没有模型,因此EL不评估它只是按原样打印文本。

EDIT2:

的EL表达式不为你工作之前,因为Servlet规范2.3不支持EL(与2.4引入)。正如你已经声明你的web.xml使用spec 2.3,它只是关闭EL解析的开销。

EDIT3:

只是为了澄清行

线

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

告诉你的servlet容器使用规格2.3

+0

@SotiriosDelimanolis:很可能,如果是这种情况,我会删除我的答案,但对于评论似乎有点长。 – beny23 2013-03-08 15:37:18

+0

他找到了他的答案,看到他的问题评论。留下以供将来参考。 – 2013-03-08 15:39:42

+0

是的..我会发布我如何得到它的工作,因为不理解为什么它的作品。现在。 – SJS 2013-03-08 15:47:44

0

参见:Basic Spring MVC Data Binding

一对夫妇可能是错误的东西:

  • 确保InputForm具有getter/setter方法和一个无参数的构造
  • 尝试使用Spring的bind tag或表单标签。
  • 尝试只是手动添加InputForm到模型(即model.put())。

如果你期待InputForm被绑定到请求的数据,你可能需要使用@Valid ......你不应该但它不会伤害。

最后,在成功的POST后,您几乎总是希望重定向并且不直接提供响应。如果出现错误,您应该为响应提供服务。

+0

我添加了web.xml文件。工作,而不是工作版本...有人可以告诉我为什么现在可以工作! – SJS 2013-03-08 15:51:51

1

尝试将一个model参数添加到您的控制器方法中,并尝试填充该模型而不是创建一个新模型。

相关问题