标签:private package action public return
先定义自己的struts2.xml文件
<?xml version="1.0" encoding="UTF-8"?> <struts> <dg></dg> <action name="login" class="com.ming.action.LoginAction"> <result name="success" type="chain">/success.jsp</result> <result name="login">/login.jsp</result> </action> </struts>
2.定义ResultBean对象
package com.ming.bean; public class ResultBean { private String resultName; private String resultType; private String resultText; public String getResultName() { return resultName; } public void setResultName(String resultName) { this.resultName = resultName; } public String getResultType() { return resultType; } public void setResultType(String resultType) { this.resultType = resultType; } public String getResultText() { return resultText; } public void setResultText(String resultText) { this.resultText = resultText; } }
3.定义ActionBean对象
package com.ming.bean;4.定义一个类读取XML文件
package com.ming.filter; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.ming.bean.ActionBean; import com.ming.bean.ResultBean; public class ReadXML { public static Map readXML(String path){ SAXReader reader=new SAXReader(); Map<String,ActionBean> actionMap=new HashMap<String, ActionBean>(); try { Document document=reader.read(new File(path)); Element strutsElement=document.getRootElement(); Iterator actions=strutsElement.elementIterator(); while(actions.hasNext()){ ActionBean actionBean=new ActionBean(); Element actionElement=(Element) actions.next(); String actionName=actionElement.attributeValue("name"); String actionClz=actionElement.attributeValue("class"); actionBean.setActionName(actionName); actionBean.setActionClz(actionClz); Map<String,ResultBean> resultMap=actionBean.getResultMap(); Iterator results=actionElement.elementIterator(); while(results.hasNext()){ ResultBean resultBean=new ResultBean(); Element resultElement=(Element) results.next(); String resultName=resultElement.attributeValue("name"); resultBean.setResultName(resultName); String resultType=resultElement.attributeValue("type"); resultBean.setResultType(resultType); String resultText=resultElement.getText(); resultBean.setResultText(resultText); resultMap.put(resultName, resultBean); } actionMap.put(actionName, actionBean); } } catch (DocumentException e) { System.out.println("读取XML文件失败!!"); e.printStackTrace(); } return actionMap; } }
5.实现struts2的核心类来实现action跳转功能
package com.ming.filter.core; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; 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.beanutils.BeanUtils; import com.ming.bean.ActionBean; import com.ming.bean.ResultBean; import com.ming.filter.ReadXML; public class Struts2Filter implements Filter { @Override public void destroy() { } Map actionMap; @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; // 1 获取请求路径 String path = request.getRequestURI(); System.out.println(path); int beginIndex = path.lastIndexOf("/") + 1; int endIndex = path.lastIndexOf("."); path = path.substring(beginIndex, endIndex); System.out.println(path); // 2 根据路径在struts.xml 中找到对应的类 Object object = actionMap.get(path); // 3 给对应的属性赋值 if (object != null) { ActionBean actionBean = (ActionBean) object; String actionClz = actionBean.getActionClz(); try { Class clz = Class.forName(actionClz); Object actionObject = clz.newInstance(); // 从jsp 获取提交到filter的所有的值(存储形式 k-v 例如:userName,"zhangsan") Map requestMap = request.getParameterMap(); // 自动完成 map到对象的对应的属性值的赋值 BeanUtils.populate(actionObject, requestMap); // 4 调用execute方法 Action action = (Action) actionObject; // 调用的实现类 execute方法 String returnResult = action.execute(); // 5 找返回值 Object resultObject = actionBean.getResultMap().get( returnResult); if (resultObject != null) { ResultBean resultBean = (ResultBean) resultObject; // 6 在 struts.xml 对应的jsp String result = resultBean.getResultText(); // 7 跳转 request.getRequestDispatcher(result).forward(request, response); return; } else { System.out.println("找不到result的值:" + returnResult); chain.doFilter(request, response); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("该路径不存在:" + path); chain.doFilter(request, response); } chain.doFilter(request, response); } // 在tomcat 启动的时候执行,完成struts配置信息初始化 @Override public void init(FilterConfig config) throws ServletException { String path = config.getServletContext().getRealPath( "WEB-INF/classes/struts.xml"); actionMap=ReadXML.readXML(path); } }
本文出自 “ming” 博客,请务必保留此出处http://8967938.blog.51cto.com/8957938/1696229
标签:private package action public return
原文地址:http://8967938.blog.51cto.com/8957938/1696229