67,513
社区成员
发帖
与我相关
我的任务
分享
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 {
}
}
<?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>
<body>
<a href="${pageContext.request.contextPath}/hello">跳转</a>
</body>