Struts2 无法导向到首页There is no Action mapped for namespace / and action name . ServletWebXMLJSPTomcat
程序员文章站
2022-03-02 16:57:49
...
开发环境
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;
}
}
}
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;
}
}
}
上一篇: CSS的三种选择器
下一篇: window.event的兼容写法