sturts2.23在was6.1下启动报错

项目技术组合: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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值