Struts2 無法導向到首頁There is no Action mapped for namespace / and action name

開發環境 

JDK5,發佈環境WAS6.1.0.21 

Struts2,Spring2,hibernate3.1 

問題描述 

1)struts.xml中沒有配置default-action-ref 

2)web.xml中使用s2的過濾器過濾/*,而不是*.action 

3)設置了welcomefile爲index.jsp,index.jsp再導向到Index.action。 

[09-10-30 15:42:37:982 CST] 00000037 Dispatcher    W com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn Could not find action or result 
There is no Action mapped for namespace / and action name . - [unknown location] 

發佈到WAS下總是提示不能導向的首頁的Index.action,應該 

http://server:port/appContext 

http://server:port/appContext/ 

這兩個urlpatten優先映射給s2的過濾器了。 

首先想到的解決辦法是將web.xml中的welcome-file設置爲Index.action,經測試這種方法是行不通的,welcome-file必須是一個實在的文件而不是一種映射,這點可能跟具體的容器實現策略相關的,在Tomcat或者WL下可能是生效的,但在WAS下是行不通的。經過研究和測試我最終找到了解決方案是自行開發一個過濾器名爲WelcomeFileFilter,此過濾器優先於s2的過濾器,攔截請求的URI,分析請求的URI是否相對於appContex是根目錄,是的話,則應答爲導向到一個具體的首頁如Index.action。否則的話繼續下一個過濾器。Filter的先後順序是從web.xml的從上到下開始執行的。固將此過濾器聲明於s2的過濾器之上並將其影響範圍設置爲/*,即可正常的在WAS下敲入首頁時導向到Index.action了。WAS對web.xml的書寫規範是比較苛刻的。 

附WelcomeFileFilter.java 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.commons.lang.StringUtils; 

public class WelcomeFileFilter implements Filter { 
public void destroy() { 
indexFile = null; 


private String indexFile; 

public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException, ServletException { 
HttpServletRequest httpServletRequest = (HttpServletRequest) request; 
HttpServletResponse httpServletResponse = (HttpServletResponse) response; 
//String servletPath = httpServletRequest.getServletPath(); 
String contextPath = httpServletRequest.getContextPath(); 
String requestURI = httpServletRequest.getRequestURI(); 
//System.out.println(requestURI); 
if (contextPath.equalsIgnoreCase(requestURI) || (contextPath+"/").equalsIgnoreCase(requestURI)) { 
String url = ""; 
if(indexFile.startsWith("/")){ 
url = contextPath + indexFile; 
}else{ 
url = contextPath +"/" +indexFile; 

httpServletResponse.sendRedirect(url); 
} else { 
chain.doFilter(request, response); 



public void init(FilterConfig arg0) throws ServletException { 
indexFile = "Home.action"; 
String x = arg0.getInitParameter("indexFile"); 
if (StringUtils.isNotBlank(x)) { 
indexFile = x; 




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