无法让Spring MVC 4.0.0在Tomcat7上的/ WEB-INF / pages下提供jsp服务

时间:2014-03-27 00:33:58

标签: spring jsp maven spring-mvc intellij-idea

我正在使用Intellij IDEA 12并尝试运行一个简单的HelloWorld Spring MVC应用程序。虽然web / index.jsp没有问题,但我无法正确显示/WEB-INF/pages/hello.jsp(我正在使用http://localhost:8080/hello)。我一直在说:

  

HTTP状态404 -

     

输入状态报告

     

消息

     

说明请求的资源不可用。

以下是我的设置。有什么建议吗?

感谢。

1。项目结构

--src
  --main
    --java
      --com.beingjavaguys.controller
        --HomeController.java
--web
  --WEB-INF
    --pages
      --hello.jsp
    --spring-servlet.xml
    --web.xml
  --index.jsp
--pom.xml

2。 HomeController中:

package com.beingjavaguys.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

 @RequestMapping("/hello")
 public ModelAndView test() {
    String message = "Welcome to Spring 4.0 !";
    return new ModelAndView("/hello.jsp", "message", message);
 }
}  

第3。的hello.jsp:

<html>
 <head>
  <title>Being Java Guys | Hello World</title> 
 </head>
 <body>
  <h2>Being Java Guys | Hello World</h2>
  <h4>${message}</h4>
 </body>
</html>

4。弹簧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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.beingjavaguys.controller"/>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
  </bean>
</beans>

5。 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_2_5.xsd"
     version="2.5">

<display-name>Spring4MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

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

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

  </web-app>

6。的index.jsp:

<html>
<head>
<title>Being Java Guys | Hello World</title>
</head>
<body>
<h2>Being Java Guys | Hello World</h2>
<h4><a href="hello.html">Click Here</a></h4>
</body>
</html> 

7。 Maven依赖项:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>TestSpring4</groupId>
<artifactId>TestSpring4</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>TestSpring4</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- Shared version number properties -->
    <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
</properties>

<dependencies>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- Core utilities used by other modules.
        Define this if you use Spring Utility APIs
        (org.springframework.core.*/org.springframework.util.*)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Expression Language (depends on spring-core)
        Define this if you use Spring Expression APIs
        (org.springframework.expression.*)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Bean Factory and JavaBeans utilities (depends on spring-core)
        Define this if you use Spring Bean APIs
        (org.springframework.beans.*)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Application Context
        (depends on spring-core, spring-expression, spring-aop, spring-beans)
        This is the central artifact for Spring's Dependency Injection Container
        and is generally always defined-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Various Application Context utilities, including EhCache, JavaMail, Quartz,
        and Freemarker integration
        Define this if you need any of these integrations-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Web application development utilities applicable to both Servlet and
        Portlet Environments
        (depends on spring-core, spring-beans, spring-context)
        Define this if you use Spring MVC, or wish to use Struts, JSF, or another
        web framework with Spring (org.springframework.web.*)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Spring MVC for Servlet Environments
        (depends on spring-core, spring-beans, spring-context, spring-web)
        Define this if you use Spring MVC with a Servlet Container such as
        Apache Tomcat (org.springframework.web.servlet.*)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    </dependencies>
  </project>

1 个答案:

答案 0 :(得分:1)

你要回来了

return new ModelAndView("/hello.jsp", "message", message);

这意味着视图名称为/hello.jsp。 Spring将使用您注册的ViewResolver。那是

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

将应用前缀和后缀,您的视图名称将解析为

/WEB-INF/pages//hello.jsp.jsp

不存在。

您应该返回一个简单hello的视图名称。

return new ModelAndView("hello", "message", message);

现在它将被解决

/WEB-INF/pages/hello.jsp

存在且RequestDispatcher可以转发给它。

不要忘记添加

<mvc:annotation-driven />

以及servlet上下文配置中的相应命名空间声明,否则Spring MVC堆栈不会将@Controller bean注册为处理程序。


使这种依赖

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

由容器提供

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
</dependency>

否则你可能会遇到一些jar地狱问题,因为jstl库通常已包含在Servlet容器中。