【问题标题】:Spring MVC model is not displaying a string [duplicate]Spring MVC模型不显示字符串[重复]
【发布时间】:2023-03-08 10:01:01
【问题描述】:

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

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>

【问题讨论】:

  • RealRave,你能解决你的问题吗?我也有。而且,对于上述情况,这也不是一个合适的解决方案。它不是重复的。

标签: spring jsp spring-mvc el


【解决方案1】:

确保您在 JSP 中使用:这个 Taglib

&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;

并打印消息:&lt;c:if test="${not empty message}"&gt;${message}&lt;/c:if&gt;

我强烈建议您使用 Eclipse 或 netbeans 之类的 IDE,以便在您的 jsp 中轻松检测 taglib 错误和警告。

【讨论】:

    【解决方案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>
    

    【讨论】:

    • 嗨,我试过了,但不幸的是它仍然没有显示字符串。有趣的是我昨天能够显示字符串。
    猜你喜欢
    • 2013-09-18
    • 2013-06-01
    • 2015-01-09
    • 2016-09-30
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多