The Struts dispatcher cannot be found in struts2

使用Struts2.0在訪問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.

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 Struts.xml

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <include file="struts-default.xml"/>
  <package name="ygn.action" extends="struts-default">
    <action name="HelloWorld" class="ygn.action.HelloWorld">
      <result>HelloWorld.jsp</result>
    </action>
  </package>
</struts>
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Say Hello</title>
</head>
<body>
  <h3>Say "Hello" to :</h3>
  <s:form action="HelloWorld">
    Name:<s:textfield name="name"/>
    <s:submit/>
  </s:form>
</body>
</html>
異常分析:以上的配置及文件中,如果採用 http://ip:port/SayHello.jsp,那麼會出現前面所提到的異常。如果採用http://ip:port/SayHello.action 進行訪問,那麼正常。
原因:如果想要在jsp文件中,採用 struts的tag,那麼必須通過web.xml所配置的過濾器訪問文件,否則會有異常,即 之前所出現的異常。
解決方案:
方案一:
	  採用 http://ip:port/SayHello.action 訪問


方案二:
	 將web.xml 的過濾器,從 *.action 修改爲: /*


方案三:
	 修改SayHello.jsp 文件,不使用 struts 的標籤。
建議解決方案:
使用*.action或*.do的好處是可以區分普通servlet和sturts中的action,故而最好不要用/*,遇到此問題時,可以加多個filter-mapping。
如  
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章