Java Spring MVC - 在 JSP 视图中没有获得价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20822987/
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-08-13 04:36:32  来源:igfitidea点击:

Spring MVC - Not getting value inside JSP view

javaspringjspspring-mvc

提问by Sandeep

Firstly, I am newbie in the world of Spring MVC.

首先,我是 Spring MVC 世界的新手。

I made simple program where Spring MVC will handle GET request and set a variable named "message". This variable should display set value in JSP but is not doing as expected. Code is getting compiled and running fine. Can you please suggest, what is being done wrong here?

我制作了一个简单的程序,其中 Spring MVC 将处理 GET 请求并设置一个名为“message”的变量。此变量应在 JSP 中显示设置值,但未按预期执行。代码正在编译并运行良好。你能提出建议吗,这里做错了什么?

web.xml

网页.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>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>loginDispacher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>loginDispacher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

loginDispacher-servlet.xml

loginDispacher-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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="com.sandeep" />

    <!-- View resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Authorization.java

授权.java

@Controller
@RequestMapping("/authorization")
public class Authorization {
    String message = "This is message from Java class";

    @RequestMapping(method=RequestMethod.GET)
    public String printHello(ModelMap model){
        System.out.println("From controller");
        model.addAttribute("message", "Hellow Spring MVC Framework!");
        return "authorization";
    }

}

old authorization.jsp

旧授权.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
   <head>
   <title>Hello Spring MVC</title>
   </head>
   <body>
   <h2> <c:out value="${message}" /> </h2>
   </body>
</html>

updated and working authorization.jsp

更新和工作authorization.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
    <h2>
        <c:out value="${message}" />
    </h2>
</body>
</html>

Outputenter image description here

输出在此处输入图片说明

采纳答案by Alexey

Your problem is not related to Spring MVC. ${message}is EL(Expression Language). It's a part of Java EE (and a former part of the JSP specification). It does not work on your page for some reason.

您的问题与 Spring MVC 无关。${message}EL(表达语言)。它是 Java EE 的一部分(也是 JSP 规范的前一部分)。由于某种原因,它在您的页面上不起作用。

Try to replace the beginning of your web.xmlwith the following:

尝试web.xml用以下内容替换您的开头:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

回答by Sotirios Delimanolis

Your web.xmlis declaring your application as being a Servlet 2.3 compatible web application

web.xml将您的应用程序声明为与 Servlet 2.3 兼容的 Web 应用程序

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

Expression Language (EL) was introduced in 2.4.

表达式语言 (EL) 是在 2.4 中引入的。

Change that and you should be in business (as long as your container also supports it).

改变它,你就应该开始营业了(只要你的容器也支持它)。

You can find templates for the different versions here.

您可以在此处找到不同版本的模板。

回答by Ashutosh Tripathi

Just add jstl dependency if it is a maven project.

如果是 maven 项目,只需添加 jstl 依赖项。

<dependency>
     <groupId>jstl</groupId>
     <artifactId>jstl</artifactId>
     <version>1.2</version>
</dependency>

It worked for me.

它对我有用。

回答by Fahad

if none of the above works. add this in your jsp page:

如果以上都不起作用。在你的jsp页面中添加:

<head> <%@ page isELIgnored="false" %> </head>

source: mkyong

来源:mkyong

回答by Sandeep Verma

Add this in your jsp page:

在你的jsp页面中添加:

<%@ page isELIgnored="false" %>

<%@ 页面 isELIgnored="false" %>