異常信息: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.
環境:tomcat 6.0 struts2.21
web.xml的配置:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepar eAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
默認是進行.action的過濾,以前都是使用freemarker作為顯示端的,沒出錯,現在用jsp來顯示點東西卻出現了這個錯誤
Test.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>測試</title> </head> <body> <s:form action = "testAction.action"> <s:textfield name = "stuMap['one'].stuNum" label = "第一個用戶名"/> <s:password name = "stuMap['one'].password" label = "第一個密碼"></s:password> <s:textfield name = "stuMap['two'].stuNum" label = "第二個用戶名"/> <s:password name = "stuMap['two'].password" label = "第二個密碼"></s:password> <s:submit name = "send" value = "提交" theme = "simple"></s:submit> </s:form> </body> </html>
struts2的標簽已經引入了,查詢后歸納了以下幾種方法:
1.將web.xml下的struts2過濾器的過濾方式從.action改為/*; 不過這種方式自己感覺不太好,應該這樣默認就將所有的進行了過濾,可能會對有些應用帶來麻煩,例如
fck的配置
2. 修改Test.jsp 文件,不使用 struts 的標簽
3.就是在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>