在spring mvc应用程序实现tile时找不到404错误

时间:2016-06-21 07:14:57

标签: java xml spring spring-mvc

我读了几篇关于这个404错误的文章,但无济于事。当我使用基本网址http://localhost:8080/chatapplication访问此项目时,我收到404错误。我尝试使用网址http://localhost:8080/chatapplication/chat,但结果相同。我的代码如下: -

的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 version="2.5"
         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">
  <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

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

</web-app>

调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.abhishek.controllers" />

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

    </beans>

tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
    <definition name="base.definition"
                template="/WEB-INF/jsp/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
    </definition>

    <definition name="chat" extends="base.definition">
        <put-attribute name="title" value="Chat Application" />
        <put-attribute name="body" value="/WEB-INF/jsp/chat.jsp" />
    </definition>
    </tiles-definitions>

控制器类如下: -

package com.abhishek.controllers;

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

/**
 * Created by CAN14 on 6/21/2016.
 */
public class HelloWorldController {
    @RequestMapping(value="/chat")
    public String index()
    {
        System.out.println("**************");
        return "home";
    }
}

请提前帮助..谢谢..

2 个答案:

答案 0 :(得分:0)

在HelloWorldController类中缺少 @Controller 注释。

答案 1 :(得分:0)

为您提供切片视图解析器的顺序因此,您可以定义其他视图解析器,以防您从未映射的控制器返回任何视图。 从你的控制器你回来&#34; home&#34;。哪个未映射到tiles.xml中。 将其更改为&#34; chat&#34;你会看到chat.jsp带有页眉和页脚,因为它映射在tiles.xml中。

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

你还应该在下面添加仅映射单个jsp而不用tile

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

    <property name="order" value="1"/>
</bean>