Spring MVC和Apache Tiles - 404错误

时间:2013-09-25 20:58:07

标签: java spring jsp maven spring-mvc

我用Spring 3 MVC编写了“Hello World”网站,现在我正在尝试使用Apache Tiles技术重写它。我正在使用Maven来构建我的项目。 的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>michat</groupId>
<artifactId>WebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <spring.version>3.2.4.RELEASE</spring.version>
    <jdk.version>1.7</jdk.version>
    <tiles.version>3.0.0</tiles.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </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.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>${tiles.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>WebApp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

的web.xml

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

dispatcher.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-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.michat.controller" />
<bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

tiles.xml

<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
    <tiles-definitions>
        <definition name="base" template="/WEB-INF/views/layout.jsp">
                <put-attribute name="header" value="/WEB-INF/views/header.jsp" />
            <put-attribute name="content" value="" />
            <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
        </definition>
        <definition name="home" extends="base">
            <put-attribute name="content" value="/WEB-INF/views/home.jsp" />
        </definition>
    </tiles-definitions>

HomeController.java

package com.michat.controller;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showHomePage() {
        return "home";
    }
}

我在WEB-INF的views目录中定义了footer.jsp,home.jsp,header.jsp和layout.jsp。为什么我会收到此错误?

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/7.0.35

1 个答案:

答案 0 :(得分:1)

请添加到您的dispatcher.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">

<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.michat" />

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <value>/WEB-INF/tiles-defs.xml</value>
    </property>
</bean>

这个

<context:annotation-config/>

将激活各种注释,例如@Required和@Autowired

这个

<mvc:annotation-driven />

需要具有注释驱动的控制器。

下面的配置只是扫描类路径以获取带注释的组件。

<context:component-scan base-package="com.antenna.nn.patient" />

UrlBasedViewResolver是viewResolver,允许将符号视图名称直接解析为URL。瓷砖需要这个。