Spring + Tiles - 访问jsp文件夹时出现404错误

时间:2012-01-03 12:45:49

标签: spring jsp http-status-code-404 tiles

基本上,当我启动项目时会发生的事情是视图都已正确解析并且正在查找正确的jsp,但是似乎有阻止tile访问WEB-INF文件夹中的jsp文件夹的内容。 / p>

确切的问题是,当我转到localhost / FitterBlog / index.htm时,我收到404错误:

The requested resource (/FitterBlog/jsp/layout/layout.jsp) is not available.

我有以下代码:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

调度-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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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.fitterblog.controllers"/>
<context:annotation-config/>

<!-- tiles configuration -->
<bean id="tilesViewResolver" 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>

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_2_0.dtd">

<tiles-definitions>
<definition name="baseLayout" template="/jsp/layout/layout.jsp">
    <put-attribute name="title" value="FitterBlog" />
    <put-attribute name="header" value="/jsp/layout/header.jsp" />
    <put-attribute name="nav" value="/jsp/layout/nav.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/jsp/layout/footer.jsp" />
</definition>

<definition name="index" extends="baseLayout">
    <put-attribute name="body" value="/jsp/index.jsp" />
</definition>   
</tiles-definitions>

MainController.java:

package com.fitterblog.controllers;

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

@Controller
public class MainController {

@RequestMapping(value="index.htm", method=RequestMethod.GET)
public ModelAndView index() {
    return new ModelAndView("index");
}
}

我已经三次检查所有JSP文件都位于正确的位置,就像获取404错误的layout.jsp文件位于WEB-INF / jsp / layout / layout.jsp中一样。

2 个答案:

答案 0 :(得分:3)

在我的应用程序中,jsp位于WEB-INF

的子目录中

如果你的情况相同,你需要稍微更改一下瓷砖配置

<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
    <put-attribute name="title" value="FitterBlog" />
    <put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp" />
    <put-attribute name="nav" value="/WEB-INF/jsp/layout/nav.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
</definition>

<definition name="index" extends="baseLayout">
    <put-attribute name="body" value="/WEB-INF/jsp/index.jsp" />
</definition>   
</tiles-definitions>

答案 1 :(得分:0)

如果您想将JSP存储在WEB-INF中,那么只需在ViewResolver中设置prefix属性

<beanid="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <property name="suffix" value=".jsp"/>
</bean>