【发布时间】:2014-04-17 10:51:27
【问题描述】:
我正在尝试使用 Spring web mvc 运行我的 helloWorld 程序,但我的 JSP 文件中有问题..它无法读取添加到 ModelMap 的“message”属性的值.. jsp 页面正常打开但是它显示 jsp EL,因为它是 ${message} .. 不是 message 的值!
handler-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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-4.0.xsd">
<context:component-scan base-package="com.shura" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloWorld.java
package com.shura;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@RequestMapping(method=RequestMethod.GET)
public String printHello(ModelMap model){
model.addAttribute("message", "Hello World");
return("hello");
}
}
hello.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h1>Spring MVC Hello World Example</h1>
<h2>${message}</h2>
</body>
</html>
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>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>handler</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>handler</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
【问题讨论】:
-
添加你的web.xml,确保它有一个正确的版本,如果它太低就不会启用EL。
-
你能不能试试 model.addObject 而不是 model.addAttribute
标签: java spring jakarta-ee spring-mvc