Spring MVC模型没有显示字符串

时间:2016-07-01 12:35:34

标签: spring jsp spring-mvc el

我的项目是让最终用户登录,然后转到另一个页面来显示消息。如果登录详细信息错误,则会显示相应的错误消息,并为他们提供另一个登录的机会。问题是要显示的消息不是。相反,所有显示的都是;

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>

弹簧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>

2 个答案:

答案 0 :(得分:2)

您在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>

答案 1 :(得分:0)

确保您正在使用:JSP中的Taglib

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

并打印消息:<c:if test="${not empty message}">${message}</c:if>

我强烈建议您使用像eclipse或netbeans这样的IDE,以便在jsp中检测taglib错误和警告很容易。