Struts2 无法导向到首页

接触Struts2不是很久,想尝试用一下,就在web.xml
welcome-file下设计直接跳转到指定的action

There is no Action mapped for namespace / and action name

直接输action没有问题,配置没有问题

查了一些资料,原来问题出现在
 <welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>


tomcat 会去检查有没有在相应的目录下有没有login.do这个文件.
解决方法一:在对应的目录下建立对应的文件 之后再跳转都随你了(测试不带后缀的不可以)

解决方法二:(因上面的解决方法 只在Tomcat服务器下适用,别的不一定适用)
创建一个Filter 过滤访问地址,通用
package com.lc.common;

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 = "login";
String x = arg0.getInitParameter("indexFile");
if (StringUtils.isNotBlank(x)) {
indexFile = x;
}
}

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<filter>
<filter-name>welcome</filter-name>
<filter-class>com.lc.common.WelcomeFileFilter</filter-class>
</filter>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>welcome</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>login.....</welcome-file>
</welcome-file-list>
</web-app>


这样处理的话 welcome-file-list 对应的file 写什么都无所谓

这里关联到Filter(过滤器) 再学习一下
http://guifan.iteye.com/blog/1631513

参考页面:
http://blog.ziki.cn/284.html
......
--------------------------------------------------------------------------
交流群:81552084
--------------------------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值