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;
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值