如何在以下Spring MVC项目中解决404错误并找不到“标题”(Apache tiles)?

时间:2014-09-20 10:13:59

标签: java spring spring-mvc web apache-tiles

调度-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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.projectName.www" />

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="project" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

     <!--  <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>  -->

    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" /> 





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

以下是web.xml的代码

<web-app>

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

   <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/src/main/webapp/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>-->
        <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>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param> 

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

    <welcome-file-list>
       <welcome-file>/template.jsp</welcome-file>
    </welcome-file-list>  
</web-app>

template.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1 align="center"><font face="Verdana" color="#008A00">WELCOME</font></h1>

        <tiles:insertAttribute name="header" />

     <%--     
     <div></div>
        <tiles:insertAttribute name="navigation_bar" />
        <div></div> 
        <div id="page">
            <tiles:insertAttribute name="center" />
        </div>
        <div></div>
        <div id="footer_wrapper">
            <tiles:insertAttribute name="footer" />
        </div>-->m  --%>  


</body>
</html>

Controller.java

@Controller
public class Controller {

    @Autowired  
    private Service Service;

        //@RequestMapping(value = "/search", method = RequestMethod.GET)
       @RequestMapping(value={"/","/template"} , method = RequestMethod.GET)

        public String getPersonList(ModelMap model) {  

            return "header";  
        }  
}

tiles.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tiles-definitions PUBLIC    
  "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"   
     "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>    
   <definition name="template"  template="/template.jsp">     
 <!-- 
       <put-attribute name="top"    value="/jsp/header.jsp" /> 
         <put-attribute name="header"    value="/jsp/header.jsp" /> 
       <put-attribute name="header"    value="/webapp/jsp/header.jsp" />   
       <put-attribute name="header"    value="/header.jsp" />  -->
        <put-attribute name="header"    value="/WEB-INF/jsp/header.jsp" />

       <put-attribute name="center" value="/WEB-INF/jsp/ads.jsp" />
       <put-attribute name="bottom"   value="/WEB-INF/jsp/footer.jsp" />

    </definition>

    <definition name="header" extends="template">          
         <put-attribute name="header"    value="/header.jsp" />

    </definition>

</tiles-definitions>

header.jsp中

<!doctype html>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles"%>

<html>

        <title>Welcome</title>

    </head>
    <body>
        Hello, Welcome
    </body>
</html>

这就是项目结构的样子

enter image description here

案例1:<tiles:insertAttribute name="header" />在template.jsp中注释掉了 1)当我将URI作为localhost:8080 / CtxtRoot /时,我可以在浏览器中看到template.jsp。

2)但是如果我将URI作为localhost:8080 / CtxtRoot / template,我在浏览器中没有得到header.jsp。我发现404未找到错误。

案例2:<tiles:insertAttribute name="header" />添加到template.jsp和 3)如果我将localhost:8080 / CtxtRoot /作为URI,我得到NoSuchAttributeException:找不到属性'header'。

如何解决未找到的标题和404错误?

我尝试了stackoverflow,coderanch和springIO等中的所有解决方案。我无法修复它们。这是48小时,我对此问题感到震惊。我需要一双眼睛和一些眼睛来帮助我解决这个问题。非常感谢。

0 个答案:

没有答案