1. 程式人生 > >struts2中直接訪問jsp頁面報錯解決方法!

struts2中直接訪問jsp頁面報錯解決方法!

問題描述:

The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Strut s tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]

操作:struts2中直接訪問jsp 

異常:The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. 


環境:struts2


原因:如果想要在jsp檔案中,採用 struts的tag,那麼jsp必須是通過action跳轉得到,也就是必須通過web.xml所配置的過濾器訪問檔案,否則會有異常。


解決辦法:
方案一:將web.xml 的過濾器,從 *.action 修改為: /* 。

方案二:web.xml 中新增一個filter 
 <filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>*.jsp</url-pattern> 
</filter-mapping> 
<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/action/*</url-pattern> 


</filter-mapping>

方案三:web.xml中加一段

 <jsp-config>   
     <taglib>   
          <taglib-uri>struts-tags</taglib-uri>      
          <taglib-location>/WEB-INF/struts2-core-2.0.11.jar/struts-2.0.dtd</taglib-location>      
     </taglib>     
     </jsp-config>

方案四:採用action訪問jsp頁面。

方案五:修改jsp頁面,不使用struts標籤。