我的项目是让最终用户登录,然后转到另一个页面显示消息。如果登录详细信息错误,它会显示相应的错误消息,并为他们提供再次登录的机会。问题是,本来应该显示的消息却没有显示。相反,所有显示的是;
Message is: ${message}
index.jsp
<form action="login.html" method="post">
Name: <input type="text" name="name"/>
Password: <input type="password" name="password"/>
<input type="submit" value="submit"/>
</form>
LoginController.java
package com.loginmvc.domain;
@Controller
public class LoginController {
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("name");
String password = req.getParameter("password");
if(password.equals("admin")) {
String message = "Welcome " + name;
return new ModelAndView("profilepage", "message", message);
} else {
return new ModelAndView("errorpage", "message",
"Sorry, the name or password is incorrect.");
}
}
}
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>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.loginmvc.domain" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
最佳答案
您在 web.xml
文件中使用旧的描述符。这将导致 EL 被忽略:
<!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>
如果您正在学习教程,我建议您找到一个更新的教程。但是,与此同时,更新您的 web.xml
应该可以解决您眼前的问题:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
...
</web-app>
关于Spring MVC 模型不显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38145600/