How to resolve 404 error and no “header” found (Apache tiles) in the following Spring MVC project?

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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

Following is code of 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> 

This is how the project structure looks like

Case 1: <tiles:insertAttribute name="header" /> commented out in template.jsp 1) When I give the URI as localhost:8080/CtxtRoot/ , I can see template.jsp in browser.

2) But if I give the URI as localhost:8080/CtxtRoot/template , I don't get header.jsp in browser. I get 404 not found error.

Case 2: <tiles:insertAttribute name="header" /> is added in template.jsp and 3) If I give localhost:8080/CtxtRoot/ as URI, I get NoSuchAttributeException: Attribute 'header' not found.

How to resolve the header not found and 404 error?

I tried all the solutions in stackoverflow, coderanch and springIO etc. I couldn't fix them. Its been 48 hours, I am struck with this issue. I need a pair of eyes and some one to help me fix this issue. Thanks a lot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!