標籤: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換成根資料夾下放在資源檔的檔案夾)
現在位於這個檔案架下的所有資源檔都可以訪問了!
不過記得引用路徑最好是絕對路徑。這個已經是老問題了。