[英]Why jsp files inside WEB-INF folder works , but placed under a folder under WEB-INF doesn't?
当我的jsp文件在WEB-INF文件夹中(作为/WEB-INF/file.jsp)时,我可以从localhost:8080 / ProjectCtxtRoot /访问它们,但如果将它们放在/ WEB-中我无法访问它们INF / JSP / file.jsp?
我在web.xml中更改了welcome-list标记中的路径,如下所示
<welcome-file-list>
<welcome-file>/JSP/fileName.jsp</welcome-file>
</welcome-file-list>
我还更改了dispatcher-servlet.xml,如下所示
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
它仍然无效。 用于上述案例的网址是
localhost:8080/ContextRoot/jsp/
localhost:8080/ContextRoot/jsp/fileName.jsp
localhost:8080/ContextRoot/jsp/fileName
它不适用于上述任何网址。
但它正在工作的时候
<welcome-file-list>
<welcome-file>/fileName.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.xml如下
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
用于上述案例的URL是localhost:8080 / ContextRoot /并且它可以工作。
我使用tomcat v 7.0服务器。 我在Eclipse IDE中刷新我的项目,然后清理它,构建它,使用mvn clean install构建war,然后从tomcat manager主页选择war并进行部署。 我每次都这样做。
这就是diapatcher-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="tableName" />
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!-- <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
<bean class=
"org.springframework.web.servlet.view.tiles2.TilesConfigurer"> -->
<!-- <property name="definitions">
<list>
<value>/WEB-INF/views/views.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>
</context-param>
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<welcome-file-list>
<welcome-file>/fileName.jsp</welcome-file>
</welcome-file-list>
</web-app>
好。 当我将整个jsp文件夹从/webapp/WEB-INF/jsp/fileName.jsp移动到/webapp/jsp/fileName.jsp时,它可以工作。 我想知道1.为什么现在有效? 这是正确的做事方式吗? 3.当url为localhost:8080 / CtxtRoot / jsp /或localhost:8080 / CtxtRoot / jsp / search.jsp时,它可以工作,但它不适用于localhost:8080 / AnnaUnivResults / jsp / search。 为什么会这样?
我认为这里有几个问题:
不幸的是我无法为你详细介绍每一个细节,很多弹簧都是可配置的,所以我的解释只是涵盖了最基本的场景。 如果有人发现错误,请告诉我,我会解决它。
对于路径,可能有助于逐步思考问题。
localhost:8080/CtxtRoot/jsp/
,您可以在服务器上有一个Web应用程序,其上下文根为“CtxtRoot”,它将处理该请求。 或者,您可以拥有一个具有“”上下文的应用程序,它可以处理该请求。 这取决于您配置服务器的方式,默认情况下Tomcat将使用war名称作为上下文根。 localhost:8080/CtxtRoot/jsp/
的请求,Web应用程序将基于'jsp'作为路径路由事物。 localhost:8080/CtxtRoot/jsp/
,并且上下文根是'CtxtRoot',那么Web应用程序正在比较'/ jsp /'到所有servlet映射。 对WEB-INF中的静态资源的请求将始终被拒绝,但servlet和过滤器可以并且确实从WEB-INF返回数据。 http://localhost:8080/app/mvc/sales
访问该控制器。 从您的示例中,您正在请求localhost:8080 / ContextRoot / jsp / fileName,因此看起来'CtxRoot'是上下文根,您的servlet的路径是'/',因此它应该将之后的任何内容传递给控制器。 当DispatcherServlet收到请求时,它正在搜索将'jsp'作为路径处理的控制器。 由于你没有,它决定将其视为资源路径。 它使用视图解析器并形成路径/WEB-INF/jsp/jsp/fileName.jsp,这显然不存在。
假设您已经请求localhost:8080 / ContextRoot / fileName,请求将到达DispatcherServlet,它将找不到将“fileName”作为路径处理的Controller,因此会将其视为资源。 它将形成路径/WEB-INF/jsp/fileName.jsp,这将返回结果。
但是,您的Web xml未配置为初始化spring。 因此,您的Web应用程序实际上正在处理您的每个请求,就好像它们是针对Web应用程序根目录的资源一样。 我相信,如果你已经正确地初始化了Spring的请求,它可能会有效。
以下是如何执行此操作的一个很好的示例:
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
请注意,他的web xml有一个ContextLoaderListener,它在你的注释中被注释掉,这对于在web应用程序中初始化spring非常重要。 我还在调度程序中看到了路径/ src / main / resources的注释,但是web xml中的所有路径都应该相对于Web应用程序根目录。 在运行时,Web服务器不了解您的项目,并且'src'不是Web应用程序根目录中的目录。 另请注意,您的MVC内容可能与主弹簧上下文不同,这很常见。
我想如果你做这些事情它会起作用:
提出要求
本地主机:8080 / CtxtRoot /文件名
此外,您一直在谈论欢迎文件,但您提供了完整的资源路径。 欢迎文件仅在用户向目录的根目录发出请求时才会起作用,如下所示:
localhost:8080/CtxtRoot/
该请求将被转发到欢迎文件。 我认为你唯一一次尝试过,jsp碰巧在你的应用程序根目录中,并被配置为欢迎文件,所以它有效。 虽然它“有效”,但实际上并没有使用spring来返回它。
祝你好运。
WEB-INF之外的所有内容都是公开可用的,不对其应用任何身份验证(我们可以通过在web.xml中定义scurity约束来应用),但WEB-INF内的所有资源都是安全的。 您可以将静态页面保留在WEB-INF之外,以及动态页面,例如WEB-INF中的配置文件,帐户。 请交叉检查我可能是错的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.