java – Spring MVC – 没有在JSP视图中获取值

前端之家收集整理的这篇文章主要介绍了java – Spring MVC – 没有在JSP视图中获取值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我是 Spring MVC世界的新手.

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

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>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中

<?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

@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";
    }

}

旧的authorization.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>

更新和工作的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>

产量

解决方法

您的问题与Spring MVC无关. ${message}是EL(表达语言).它是Java EE的一部分(以及JSP规范的前一部分).由于某种原因,它无法在您的页面上运行.

尝试使用以下内容替换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">
原文链接:https://www.f2er.com/java/127035.html

猜你在找的Java相关文章