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

发布于 2024-12-23 16:45:07 字数 4609 浏览 1 评论 0原文

基本上,当我启动项目时,视图都已正确解析,并且正在查找正确的 jsp,但是似乎有某些东西阻止了对 WEB-INF 文件夹内的 jsp 文件夹的访问。

确切的问题是,当我转到 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>

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: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 中。

Basically, what happens when I launch my project is that the views are all resolved properly and the correct jsp is being looked for, however there seems to be something blocking tiles access to my jsp folder inside the WEB-INF folder.

The exact problem is that when I go to localhost/FitterBlog/index.htm I get a 404 error:

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

I have the following code:

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>

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: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");
}
}

I have triple checked that all the JSP files are located in the correct location, as in the layout.jsp file that gets the 404 error is located in WEB-INF/jsp/layout/layout.jsp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-12-30 16:45:07

在我的应用程序中,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>

In my application the jsp's are located in subdirectories of WEB-INF.

If it is the same for you you need to change the tiles config a bit

<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>
旧人 2024-12-30 16:45:07

如果您想将 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>

If You want to store your JSPs in WEB-INF then just set the prefix property in the ViewResolver

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文