Java SpringMVC为什么我的模型中的数据没有显示在output.jsp中?
我正在使用SpringMVC。为什么我的模型中的数据没有显示在output.jsp中?。我正在做一个非常简单的例子,数据显示在一页上,显示在第二页背面。我检查了数据是否在模型中,但我不知道为什么第二个JSP没有显示它 这是我的控制:Java SpringMVC为什么我的模型中的数据没有显示在output.jsp中?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在使用SpringMVC。为什么我的模型中的数据没有显示在output.jsp中?。我正在做一个非常简单的例子,数据显示在一页上,显示在第二页背面。我检查了数据是否在模型中,但我不知道为什么第二个JSP没有显示它 这是我的控制: @Controller public class RequestController { private static final Logger LOGGER = getLogger(RequestController.class); @Req
@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);
}
}
以下是我的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>
JQuery示例Web应用程序
index.jsp
上下文配置位置
/WEB-INF/spring/root-config.xml
org.springframework.web.context.ContextLoaderListener
春天
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
1.
春天
/
以下是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>
JQuery示例Web应用程序
index.jsp
上下文配置位置
/WEB-INF/spring/root-config.xml
org.springframework.web.context.ContextLoaderListener
春天
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
1.
春天
/
有人能告诉我为什么要更改web.xml使其工作。您的模型仅用于POST请求(在
displayOutputPage
中),但JSP中没有任何地方您提交表单。因此,您只能调用displayRequestPage
,而从不提供模型(另请参见:
有几件事情可能是错误的:
- 确保InputForm具有getter/setter和无参数构造函数
- 尝试使用Spring的或form标记
- 尝试手动将InputForm添加到模型(即model.put())
@Valid
…您不应该这样做,但这不会造成任何伤害
最后,在一篇成功的文章中,您几乎总是希望重定向,而不是直接提供响应。如果出现错误,您应该提供响应。尝试向控制器方法添加一个
模型参数,并尝试填充该模型,而不是创建一个新模型。您使用的ViewResolver是什么?看起来像您的EL未被评估。您可能想尝试以下操作:。我从web.xml中删除了这一行,现在一切都正常了。@SotiriosDelimanolis:很可能,如果是这样的话,我会删除我的答案,但似乎有点长,需要发表评论。他找到了他的答案,请参阅他的问题评论。将此内容留作将来参考。是的。。我将发布我是如何得到它的工作是因为不理解它为什么工作。现在。我只添加了web.xml文件。工作版本和非工作版本…有人能告诉我为什么它现在工作吗!@user2144370:请看我的EDIT2I添加了但是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>
<!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>
<b>Please <a href="./request">click here</a> to restart</b>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >