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

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

一個小小的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換成根資料夾下放在資源檔的檔案夾)

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

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

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.