我正在使用 Spring MVC。为什么我的模型中的数据没有显示在 output.jsp 中?我正在研究一个非常简单的示例,数据显示在一页上并显示在第二页上。我检查了数据是否在模型中,但我不知道为什么第二个 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 使其工作。
最佳答案
您的模型仅针对 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 不会对其进行评估,只是按原样打印文本。
编辑2:
EL 表达式以前对您不起作用,因为 Servlet 规范 2.3 不支持 EL(在 2.4 中引入)。正如你宣布你的 web.xml
要使用规范 2.3,它只是关闭了 EL 解析的开销。
编辑3:
只是为了澄清这条线
线
<!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
关于java - Spring MVC 为什么我的模型中的数据没有显示在 output.jsp 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15297217/