struts2中css,js等資源無效 非路徑問題(新手問題)


一個小小的Strust2例子

然后發現css,js,圖片用不了,debugger下發現無法訪問這些資源(404錯誤),媽的,那個例子明明可以的,起碼從書上的圖片看。

發現是web.xml中的過濾器的問題,代碼是這樣的:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

   <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

紅色部分的代碼過濾了所有的訪問。這樣不就是連css,js等資源的訪問也過濾掉了嗎?但是那個書上的例子是這樣寫的,大家都是這樣寫的。為毛!!。媽蛋。

strust.xml文件只是配個一個action。

滾上github看看完整的項目,找個好久,發現一個類似的項目應用了Strust2,也應用了css等資源文件。然后web.xml文件跟我的一模一樣,但是注意到Strust.xml文件有一個特別之處。就是以下這一行:

<constant name="struts.action.excludePattern" value="/static/.*?" /><!-- default locale -->
 
        

static文件就是放置css,js,圖片等資源的地方,代碼旁邊也注釋到。

媽蛋原來是這樣。

在Strust.xml中的<strust>標簽下加上這一行代碼就好了,(static換成根文件夾下放在資源文件的文件夾)

現在位於這個文件架下的所有資源文件都可以訪問了!

不過記得引用路徑最好是絕對路徑。這個已經是老問題了。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM