1.具体项目结构如下:
2.配置web.xml
<filter> <filter-name>ActionFilter</filter-name> <!-- 过滤器具体实现类 --> <filter-class>com.gxxy.action.web.filter.ActionFilter</filter-class> </filter> <filter-mapping> <filter-name>ActionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.actions.xml
<actions> <action name="list" class="com.gxxy.action.oa.web.ListAction" method="list"> <target name="listing" type="forword" >/WEB-INF/index.jsp</target> </action> </actions>
4.Filter具体实现类
--->加载actions.xml配置文件
/** * 加载配置文件 actions.xml * * @return */ private Document getDoucment() { //注意:Document 为org.w3c.dom.Document包中的; Document doucment = null; InputStream resourceAsStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream("actions.xml"); try { doucment = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(resourceAsStream); } catch (Exception e) { e.printStackTrace(); } return doucment; }
-->当过滤器初始化的时候,加载出actions.xml里的内容
/** * 初始化 */ private Map<String, ActionConfig> map = new HashMap<>(); public void init(FilterConfig arg0) throws ServletException { Document document = getDoucment(); NodeList elementsByTagName = document.getElementsByTagName("action"); //循环取出action标签中的内容 for (int i = 0; i < elementsByTagName.getLength(); i++) { // 解析action元素 Element action = (Element) elementsByTagName.item(i); String name = action.getAttribute("name"); String cls = action.getAttribute("class"); String method = action.getAttribute("method"); //将action中加载出的name,class,method 放到ActionConfig对象中 ActionConfig acticonfig = new ActionConfig(name, method, cls); //将Action对象添加到map中,key为action中的name map.put(name, acticonfig); // 解析target元素 NodeList target = document.getElementsByTagName("target"); Map<String, TargetConfig> tarmap = new HashMap<>(); //循环取出target标签中的内容 for (int j = 0; j < target.getLength(); j++) { Element tar = (Element) target.item(j); String tarname = tar.getAttribute("name"); String type = tar.getAttribute("type"); String textContent = tar.getTextContent(); //将action中target加载出的name,type及文本内容放到TargetConfig对象中 TargetConfig tarconfig = new TargetConfig(tarname, type, textContent); //将tarconfig对象添加到tarmap中,key为target中的tarname; tarmap.put(tarname, tarconfig); } acticonfig.setTar(tarmap); } }
-->具体的doFilter的实现
public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; ActionContext.request = req; ActionContext.response = resp; //获取到请求的URI String url = req.getRequestURI(); //将取到的URI用"/"分隔开 String[] urls = url.split("/"); //得到请求路径里"/"最后的元素 String cmd = urls[urls.length - 1]; /* * if ("list".equals(cmd)) { ListAction listAction = new ListAction(); * listAction.list(); } */ //public boolean containsKey(Object key)如果此映射包含对于指定键的映射关系,则返回 true。 //如果请求的内容在map中 if(map.containsKey(cmd)){ ActionConfig actionConfig = map.get(cmd); try { //通过反射根据类的全限定名称找到这个类 Class class1 = Class.forName(actionConfig.getCls()); //实例化这个类 Object interfaces = class1.newInstance(); //根据配置里的方法名通过反射找到类中的那个方法 Method method = class1.getMethod(actionConfig.getMethod()); //String invoke为具体实现方法里面的返回值 String invoke = (String) method.invoke(interfaces); //调用Action中的Target map属性 Map<String, TargetConfig> tarmap = actionConfig.getTar(); //如果方法的返回值在target标签中有 if(tarmap.containsKey(invoke)){ TargetConfig targetConfig = tarmap.get(invoke); //取出type String type = targetConfig.getType(); //取出标签的文本内容 String target = targetConfig.getTarget(); //如果type是"forward",那么请求转发,否则重定向 if("forword".equals(type)){ req.getRequestDispatcher(target).forward(req, resp); }else{ resp.sendRedirect(target); } } } catch (Exception e) { e.printStackTrace(); } } }
5.config包为Action对象和target对象包
--->ActinConfig对象的字段:
public class ActionConfig {
//action中的name private String name;
//action中的方法 private String method;
//action中的class private String cls;
//action中的target标签 private Map<String,TargetConfig> tar;
--->TargetConfig对象的字段
public class TargetConfig { //target标签中的name private String name; //target标签中的type private String type; //target标签中的文本内容 private String target;
6.context包中ActionContext的具体实现
public class ActionContext { public static HttpServletRequest request; public static HttpServletResponse response; }