一個小小的Strust2例子css
而後發現css,js,圖片用不了,debugger下發現沒法訪問這些資源(404錯誤),媽的,那個例子明明能夠的,起碼從書上的圖片看。java
發現是web.xml中的過濾器的問題,代碼是這樣的:git
web.xmlgithub
<?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等資源的訪問也過濾掉了嗎?可是那個書上的例子是這樣寫的,你們都是這樣寫的。爲毛!!。媽蛋。web
strust.xml文件只是配個一個action。apache
滾上github看看完整的項目,找個很久,發現一個相似的項目應用了Strust2,也應用了css等資源文件。而後web.xml文件跟個人如出一轍,可是注意到Strust.xml文件有一個特別之處。就是如下這一行:app
<constant name="struts.action.excludePattern" value="/static/.*?" /><!-- default locale -->
static文件就是放置css,js,圖片等資源的地方,代碼旁邊也註釋到。jsp
媽蛋原來是這樣。url
在Strust.xml中的<strust>標籤下加上這一行代碼就行了,(static換成根文件夾下放在資源文件的文件夾)spa
如今位於這個文件架下的全部資源文件均可以訪問了!
不過記得引用路徑最好是絕對路徑。這個已是老問題了。