春天的mvc。无法呈现参数

时间:2014-05-08 11:01:46

标签: java spring jsp spring-mvc

我在spring mvc中很新,我正在尝试用不同的教程块用jsp编写简单的helloworld。 Jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
   <body>
      <h1>Hello, ${name}</h1>
   </body>
</html>

控制器代码:

@Controller
public class Main {
@RequestMapping("/hello")
public ModelAndView hello(@RequestParam(value = "name", defaultValue = "moses") String name) {
    ModelAndView mv = new ModelAndView("MyView");
    mv.addObject("name", name);
    return mv;
}
}

分派器-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.stax.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

当我在jsp中输入$ {na时,我的想法会看到这个参数,所以我假设我做的正确。当我运行这个应用程序时,一切正常,除了传递给jsp的参数不会被渲染。而不是“你好,摩西”,我收到“你好,$ {name}”。我究竟做错了什么?如果您需要更多详细信息,请告诉我。 还有我的pom:

<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>com.stax</groupId>
<artifactId>site</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>site Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>site</finalName>
</build>

的web.xml:

  <web-app id="WebApp_ID" version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Archetype Created Web Application</display-name>

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

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

1 个答案:

答案 0 :(得分:4)

您需要在web.xml中指定版本2.5或版本3.0,以便EL默认激活。

根据@chrylis的建议,如果您要开始新项目,则应该结帐Thymeleaf。 这是对JSP的巨大改进。

另一个建议是检查Spring Boot,因为它极大地简化了Spring配置。