项目技术组合:struts2.2.3+spring2.5+hibernate3.3+was6.1.19版本
was启动后访问项目报错,异常如下
There is no Action mapped for namespace / and action name . - [unknown location]
原因:
异常字面意思是找不到action,实际上是找不到欢迎文件,需要自定义过滤器来转发请求到action上,然后action再转发到欢迎文件上,问题解决
解决方法:
1在web.xml中配置过滤器,代码如下:
<filter>
<filter-name>welcomeFileFilter</filter-name>
<filter-class>com.cigna.esales.filter.WelcomeFileFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>welcomeFileFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2定义过滤器类,代码如下:
package com.cigna.esales.filter;
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 {
private String indexFile;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String contextPath = httpServletRequest.getContextPath();
String requestURI = httpServletRequest.getRequestURI();
//System.out.println("requestURI="+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 = "welcomeFile.action";
String x = arg0.getInitParameter("indexFile");
if (StringUtils.isNotBlank(x)) {
indexFile = x;
}
}
public void destroy() {
indexFile = null;
}
}
3在struts.xml中配置欢迎页面的请求处理action,代码如下:
<action name="welcomeFile">
<result>/index.jsp</result>
</action>