[英]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.