所以,我试图学习一些Spring MVC,我尝试的第一个教程有一个model.addAttribute("printme", "From spring");
,在JSP中有${printme}
。
我的控制器很简单:
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(Model modelMap) {
System.out.println("on method");
modelMap.addAttribute("printme", "Hello Spring FROM INDEX !!");
return "index";
}
当我运行代码时,它不起作用,所以我开始添加到JSP。
我在身体里卷起了这个:
<h1>
${param.printme}
<br />
${printme}
<br />
${requestScope.printme}
<br />
<%=request.getParameter("printme")%>
<br />
<%=request.getAttribute("printme")%>
<br />
<%=pageContext.findAttribute("printme")%>
</h1>
我的输出源如下所示:
<h1>
<br />
<br />
Hello Spring FROM INDEX !!
<br />
null
<br />
Hello Spring FROM INDEX !!
<br />
Hello Spring FROM INDEX !!
</h1>
我希望param.printme
为空字符串,以及request.getParameter()
为空。
不应该${printme}
搜索requestScope并找到它吗?
不应该{/ 1}}与
相同${printme}
${requestScope.printme}
和<%=requestScope.getAttribute("printme")%>
? 这里发生了什么,为什么<%=pageContext.findAttribute("printme")%>
找不到属性?
我知道我可以继续使用${printme}
,但它更详细,我想知道为什么它的行为方式如此。
如果重要的话我使用的是Tomcat7.0.52,Spring 4.0 xsds和java ee 3.0 xsds。
答案 0 :(得分:1)
我有以下简单的项目:
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>biz.tugay</groupId>
<artifactId>smvcelex</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>smvcelex Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>smvcelex</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servletContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspf</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>true</scripting-invalid>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>
</web-app>
servletContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="sampleController" class="biz.tugay.SampleController"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
SampleController.java
package biz.tugay;
/* User: koray@tugay.biz Date: 2016/08/16 */
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/")
public class SampleController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String indexController(Model model) {
model.addAttribute("printme", "Hello Spring FROM INDEX !!");
return "index.jsp";
}
}
最后是index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Hello</title>
</head>
<body>
${printme}
</body>
</html>
当我访问localhost:8080时,我会看到文本
Hello Spring FROM INDEX !!
就好......
请将您的项目与此项目进行比较,提供其他代码并在必要时进一步询问。
答案 1 :(得分:-3)
在我看来,你没有在jsp文件的顶部引用jstl。美元符号是jstl的简写。确保在jsp文件的顶部有以下内容。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>