Spring MVC-在JSP视图中无法获得价值 [英] Spring MVC - Not getting value inside JSP view

查看:56
本文介绍了Spring MVC-在JSP视图中无法获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Spring MVC领域的新手.

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

我制作了一个简单的程序,其中Spring MVC将处理GET请求并设置一个名为"message"的变量.此变量应在JSP中显示设置值,但未达到预期的效果.代码正在编译并且运行良好.您能建议一下,这里做错了什么吗?

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?

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>

已更新且可以使用的授权.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规范的一部分).由于某些原因,它在您的页面上不起作用.

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.

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

Try to replace the beginning of your web.xml with the following:

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

这篇关于Spring MVC-在JSP视图中无法获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆