java Spring MVC 为什么我的模型中的数据没有显示在 output.jsp 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15297217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:08:28  来源:igfitidea点击:

Spring MVC why is data from my model not displaying in output.jsp?

javaspringspring-mvc

提问by SJS

I'm using Spring MVC. Why is data from my model not displaying in output.jsp?. I am working on a very easy example of data coming out on one page and being display on the 2nd back. I checked to see if the data is in the model and it is but I don't know why the 2nd JSP is not displaying it.

我正在使用 Spring MVC。为什么我的模型中的数据没有显示在 output.jsp 中?我正在研究一个非常简单的数据示例,该示例将数据显示在一页上并显示在第二页上。我检查了数据是否在模型中,它是,但我不知道为什么第二个 JSP 没有显示它。

Here is my control:

这是我的控制:

@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);

    }


}

here is my output JSP:

这是我的输出 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>

this is what I am getting as output:

这是我得到的输出:

Name: ${inputForm.name}

Phone(xxx)xxx-xxxxx: ${inputForm.phone}

Email: ${inputForm.email}

Please click here to restart 

Here is my Spring MVC.xml:

这是我的 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>

I got it working but changing my web.xml.. Below is the working web.xml:

我让它工作但改变了我的 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>

and here is the web.xml that did not work:

这是无效的 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>

Can someone please tell me why change the web.xml made it work.

有人可以告诉我为什么更改 web.xml 使其工作。

回答by beny23

Your model is only populated for POST requests (in displayOutputPage), but nowhere in your JSP do you submit a form. Therefore, you only ever call displayRequestPageand never provide a model (a <a hrefcreates a GET request).

您的模型仅为 POST 请求填充(在 中displayOutputPage),但在您的 JSP 中没有任何地方提交表单。因此,您只调用displayRequestPage而不提供模型(a<a href创建 GET 请求)。

Furthermore, because you are not submitting a <form>, your inputFormwill not have any data in it.

此外,由于您没有提交<form>,因此您inputForm将不会有任何数据。

To check this, you can always set breakpoints and see which method is called.

要检查这一点,您始终可以设置断点并查看调用了哪个方法。

EDIT:

编辑:

I made the above assumption because you have the following link to restart the process:

我做出上述假设是因为您有以下链接可以重新启动该过程:

<b>Please  <a href="./request">click here</a>  to restart</b>

And as your mapping for displayRequestPageis /request... As to the EL not being evaluated, it may just be that because you don't have an inputFormas you have no model and therefore the EL doesn't evaluate it just prints the text as is.

由于您的映射displayRequestPage/request...至于 EL 未被评估,这可能只是因为您inputForm没有模型,因此 EL 不会评估它只是按原样打印文本。

EDIT2:

编辑2:

The EL expressions did not work for you previously because Servlet Spec 2.3 does not support EL (was introduced with 2.4). As you had declared your web.xmlto be using spec 2.3, it simply switched off the overhead of parsing for EL.

EL 表达式以前对您不起作用,因为 Servlet Spec 2.3 不支持 EL(在 2.4 中引入)。正如您已声明web.xml要使用规范 2.3,它只是关闭了解析 EL 的开销。

EDIT3:

编辑3:

Just to clarify the line

只是为了澄清这条线

The line

线

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

told your servlet container to use spec 2.3

告诉您的 servlet 容器使用规范 2.3

回答by Anubhab

Try adding a modelparameter to your controller methods and try to populate that model rather tha creating a new one.

尝试向model您的控制器方法添加一个参数并尝试填充该模型而不是创建一个新模型。

回答by Adam Gent

See also: Basic Spring MVC Data Binding

另请参阅:基本 Spring MVC 数据绑定

A couple of things that could be wrong:

一些可能出错的事情:

  • Make sure InputForm has getters/setters and a no-arg constructor
  • Try using Spring's bind tagor form tag.
  • Try just manually adding InputForm to the model (ie model.put()).
  • 确保 InputForm 具有 getter/setter 和无参数构造函数
  • 尝试使用 Spring 的绑定标签或表单标签。
  • 尝试手动将 InputForm 添加到模型中(即 model.put())。

If your expecting InputForm to be data bound to the request you might need to use @Valid... you shouldn't but it would not hurt.

如果您期望 InputForm 绑定到您可能需要使用的请求的数据@Valid......您不应该但它不会受到伤害。

Finally on a successful POST you almost always want to redirect and not serve the response directly. If its an error you should serve the response.

最后,在成功的 POST 中,您几乎总是希望重定向而不是直接提供响应。如果它是一个错误,你应该提供响应。