标签:
1.Action类
package com.xiaoxiong.myStruts.action; public class UserAction { public String create(){ System.out.println("create"); return "success"; } public String update(){ System.out.println("update"); return "s1"; } public String delete(){ System.out.println("delte"); return "s2"; } }
2.配置文件
<?xml version="1.0" encoding="UTF-8"?> <struts> <package name="xxx1" extends="struts-default" namespace="/user"> <!-- http://localhost:8080/xxx/user/create --> <action name="create" class="com.xiaoxiong.myStruts.action.UserAction" method="create"> <result name="success">/index.jsp</result> </action> <action name="update" class="com.xiaoxiong.myStruts.action.UserAction" method="update"> <result name="s1" type="redirect">/index.jsp</result> </action> <action name="delete" class="com.xiaoxiong.myStruts.action.UserAction" method="delete"> <result name="s2">/index.jsp</result> </action> </package> <package name="xxx2" extends="struts-default" namespace="/user2"> <!-- http://localhost:8080/xxx/user/create --> <action name="c" class="com.xiaoxiong.myStruts.action.UserAction" method="create"> <result name="success" type="redirect">/index.jsp</result> </action> <action name="u" class="com.xiaoxiong.myStruts.action.UserAction" method="update"> <result name="s1" type="redirect">/index.jsp</result> </action> <action name="d" class="com.xiaoxiong.myStruts.action.UserAction" method="delete"> <result name="s2">/index.jsp</result> </action> </package> </struts>
3.entity类
package com.xiaoxiong.myStruts.entity; import java.util.ArrayList; import java.util.List; public class Package { private String name; //包名 private String extes; //继承的包名 private String namespace; //根节点 private List<Action> actionList = new ArrayList<Action>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getExtes() { return extes; } public void setExtes(String extes) { this.extes = extes; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public List<Action> getActionList() { return actionList; } public void setActionList(List<Action> actionList) { this.actionList = actionList; } }
package com.xiaoxiong.myStruts.entity; import java.util.ArrayList; import java.util.List; public class Action { private String name; //action名 private String clz; //引用类 private String method; //方法集合 private List<Result> resultList = new ArrayList<Result>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClz() { return clz; } public void setClz(String clz) { this.clz = clz; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public List<Result> getResultList() { return resultList; } public void setResultList(List<Result> resultList) { this.resultList = resultList; } }
package com.xiaoxiong.myStruts.entity; public class Result { private String name; //返回的结果名 private String type; //跳转的方式 private String localtion; //跳转的对象 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getLocaltion() { return localtion; } public void setLocaltion(String localtion) { this.localtion = localtion; } }
4.编写Configuration对struts.xml文件进行解析
package com.xiaoxiong.myStruts.core; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.xiaoxiong.myStruts.entity.Action; import com.xiaoxiong.myStruts.entity.Package; import com.xiaoxiong.myStruts.entity.Result; /** * 读取struts.xml配置文件的信息 * @author user * */ @SuppressWarnings("all") public class Configuration { private List<Package> packageList = new ArrayList<Package>(); //构建URI与Action配置信息之间的对照关系 private Map uriAction = new HashMap(); public Configuration() throws ParserConfigurationException, SAXException, IOException{ //1.创建一个XML工厂类用于解析XML的工具 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //2.创建DocumentBuilder对象,这个对象用于构建XML文件 DocumentBuilder db = dbf.newDocumentBuilder(); //获取存放class的根路径 String xmlPath = this.getClass().getResource("/").getPath() + "struts.xml"; //3.利用DOM解析struts.xml文件,经过解析的文档被包装为Doucument(文档对象) //Doucument存储了完整的XML的DOM属性结构 Document doc = db.parse(new File(xmlPath)); //4.获取所有包的信息 NodeList packages = doc.getElementsByTagName("package"); //5.循环遍历包 for (int i = 0; i < packages.getLength(); i++) { //获取所有包的对象 Element pak = (Element) packages.item(i); //将读取到的信息封装为Package对象 Package newpak = new Package(); newpak.setName(pak.getAttribute("name")); //获取包名 newpak.setExtes(pak.getAttribute("extes")); //获取struts.xml中extends属性 newpak.setNamespace(pak.getAttribute("namespace")); System.out.println(pak.getAttribute("name") + ":" + pak.getAttribute("extends") + ":" + pak.getAttribute("namespace")); //获取每一个包的action对象 NodeList actions = doc.getElementsByTagName("action"); for (int j = 0; j < actions.getLength(); j++) { //获取所有action对象 Element action = (Element)actions.item(j); //将读取到的信息封装为Action对象 Action newAction = new Action(); newAction.setName(action.getAttribute("name")); newAction.setClz(action.getAttribute("class")); newAction.setMethod(action.getAttribute("method")); System.out.println("\t" + action.getAttribute("name") + ":" + action.getAttribute("class") + ":" + action.getAttribute("method")); System.out.println("action.getAction():"+newAction.getClz()); //获取results节点 NodeList results = action.getElementsByTagName("result"); for (int k = 0; k < results.getLength(); k++) { //获取所有result对象 Element result = (Element)results.item(k); //将读取到的信息封装到Result对象中 Result newResult = new Result(); newResult.setName(result.getAttribute("name")); newResult.setType(result.getAttribute("type")); newResult.setLocaltion(result.getTextContent()); newAction.getResultList().add(newResult); //getTextContent()方法用于获取标签的内容 System.out.println("\t\t" + result.getAttribute("name") + result.getAttribute("type") + ":" + result.getTextContent()); } //将aciont封装到Package中 newpak.getActionList().add(newAction); //构建URI与Action配置信息之间的对照关系 uriAction.put(newpak.getNamespace()+"/"+newAction.getName(), newAction); System.out.println("URI与Action配置信息之间的对照关系--》"+newpak.getNamespace()+"/"+newAction.getName()+":"+newAction); } //将封装好的package对象放入packages集合 packageList.add(newpak); } } public Map getUriAction() { return uriAction; } public void setUriAction(Map uriAction) { this.uriAction = uriAction; } public List<Package> getPackageList() { return packageList; } public void setPackageList(List<Package> packageList) { this.packageList = packageList; } public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { new Configuration(); } }
5.编写StrutsPrepareAndExecuteFilter对struts.xml中的配置进行处理
package com.xiaoxiong.myStruts.core; import java.io.IOException; import java.lang.reflect.Method; import java.util.List; 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 com.xiaoxiong.myStruts.entity.Action; import com.xiaoxiong.myStruts.entity.Result; public class StrutsPrepareAndExecuteFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub System.out.println("程序销毁"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException { /** * 获取HttpServletRequest,HttpServletResponse对象 */ HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; //URL:统一资源定位符 //URI:统一资源接口 //获取URI String uri = request.getRequestURI(); //Web.xml中定义的过滤其中是接收所有的类型数据 //对JSP文件不做拦截 if(uri.endsWith(".jsp")){ arg2.doFilter(arg0, arg1); return; } //获取工程名 String contextPath = request.getContextPath(); System.out.println("获取工程名"+contextPath); //获取action接口 String actionURI = uri.substring(contextPath.length()); System.out.println("$$$$$$$$$$$$$$actionURI"+actionURI); try { //加载struts.xml文件 Configuration conf = new Configuration(); System.out.println("返回加载文件结果:"+conf); //获取已配置的所有uri与action的对应信息 Map<String, Action> actionHM = conf.getUriAction(); System.out.println("actionHM.get(actionURI)"+actionHM.get(actionURI)); if(actionHM.get(actionURI) != null){ //获取与之匹配的Action Action action = actionHM.get(actionURI); // System.out.println("action结果为:"+action); //获取对应的Action类 String className = action.getClz(); System.out.println("========="+className); //获取执行的method对象 String methodName = action.getMethod(); //利用反射技术动态执行相应的方法 //获取action类对象 Class clz = Class.forName(className); //实例化Action对象 Object act = clz.newInstance(); //获取对应的Action中的执行方法 Method method = clz.getMethod(methodName); //执行配置好的Action方法 String ret = (String)method.invoke(act); System.out.println("方法执行的返回值:"+ret); //在配置的Result中查找对应的result是否存在 List<Result> resultList = action.getResultList(); for(Result r : resultList){ System.out.println("======22222r.getName():"+r.getName()+","+r.getLocaltion()); //查找匹配的结果 if(ret.equals(r.getName())){ if("redirect".equals(r.getType())){ //跳转的路径 System.out.println("跳转路径:"+contextPath+r.getLocaltion()); response.sendRedirect(contextPath+r.getLocaltion()); }else{ System.out.println("转发路径:"+r.getLocaltion()); request.getRequestDispatcher(r.getLocaltion()).forward(request, response); } } } }else{ System.out.println(actionURI+"未找到匹配的Action"); } } catch (Exception e) { e.printStackTrace(); } } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("程序初始化"); } }
6.访问http://localhost:8088/strutsModel/user/create
标签:
原文地址:http://www.cnblogs.com/rsdqc/p/5560428.html