【新手】关于Struts2的运行过程Filter

u010252990 2016-09-09 10:42:13
新手初学Struts2了解了下Sturts2的运行过程之后就写了一个仿的过滤器,但是自己测试的时候怎么也用不了,进都进不去,有人帮我看看么。

Filter:

package com.yh.ccsu.filter;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class StrutsFilter implements Filter {

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// 1.强转
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;

// 2.操作

// 2.1得到请求资源路径
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length() + 1);

// System.out.println(path);

// 2.2 使用path去struts.xml文件中查找某一个<action name=path>的标签
SAXReader reader = new SAXReader();

try {
// 得到struts.xml的document对象
org.dom4j.Document document = reader.read(new File(this.getClass()
.getResource("struts.xml").getPath()));

// 查找<action name=hello>标签
Element actionElement = (Element) document
.selectSingleNode("//action[@name='" + path + "']");

if (actionElement != null) {
// 得到标签中的class属性和method属性
String className = actionElement.attributeValue("class");// 得到action的类名称
String methodName = actionElement.attributeValue("method");// 得到action的方法名称

// 2.3通过反射,得到class对象和method对象
Class actionClass = Class.forName(className);
Method method = actionClass.getDeclaredMethod(methodName);

// System.out.println(actionClass);
// System.out.println(method);

// 2.4让方法执行
String returnValue = (String) method.invoke(actionClass
.newInstance());// 让action类中的方法执行,并获取方法返回值
System.out.println(returnValue);

// 2.5
// 使用returnValue去action下查找其子元素result的name属性值,并与returnValue对比
Element resultElement = actionElement.element("result");
String nameValue = resultElement.attributeValue("name");
if (returnValue.equals(nameValue)) {
// 得到了要跳转的路径
String skipPath = resultElement.getText();

// 跳转
request.getRequestDispatcher(skipPath).forward(request,
response);
return;
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
// 3.放行
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}

}



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<filter>
<filter-name>struts</filter-name>
<filter-class>com.yh.ccsu.filter.StrutsFilter</filter-class>
</filter>

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

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
</xml-body>


index.jsp
里面就一句话:

<body>
<a href="${pageContext.request.contextPath}/hello">跳转</a>
</body>
...全文
204 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhou974738773 2016-09-12
  • 打赏
  • 举报
回复
应该是少导了一个包
G-old 2016-09-12
  • 打赏
  • 举报
回复
web.xml有问题吧?<xml-body>?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧