Define your own Struts2.xml file first
<?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. Defining Resultbean Objects
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. Defining Actionbean Objects
Package Com.ming.bean;
Import Java.util.HashMap;
Import Java.util.Map;
public class Actionbean {
Private String ActionName;
Private String Actionclz;
Private map<string,resultbean> map=new hashmap<string,resultbean> ();
Public String Getactionname () {
return actionname;
}
public void Setactionname (String actionname) {
This.actionname = ActionName;
}
Public String Getactionclz () {
return ACTIONCLZ;
}
public void Setactionclz (String actionclz) {
This.actionclz = ACTIONCLZ;
}
Public map<string, Resultbean> Getmap () {
return map;
}
public void Setmap (map<string, resultbean> Map) {
This.map = map;
}
}
4. Define a class to read an XML file
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 ("Failed to read XML file!! "); E.printstacktrace ();} Return actionmap;}}
5. Implement the STRUTS2 core class to implement the action jump function
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 {@Overridepublic void destroy () {}Map actionMap; @Overridepublic void dofilter (servletrequest arg0, servletresponse Arg1,filterchain chain) throws ioexception, servletexception {httpservletrequest request = (HttpServletRequest) arg0; httpservletresponse response = (httpservletresponse) arg1;// 1 GET request Path 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 finds the corresponding class in struts.xml based on the path object object = Actionmap.get (PATH);// 3 assigns a value to the corresponding property if (object != null) {actionbean actionbean = (Actionbean) object; String actionclz = actionbean.getactionclz (); try {class clz = class.forname (ACTIONCLZ); Object actionobject = clz.newinstance ();// gets all the values submitted to the filter from jsp (storage form k-v Example: UserName, "Zhangsan") Map requestmap = request.getparametermap ();// automatically completes assignment of map to the object's corresponding property value Beanutils.populate (Actionobject, requestmap);// 4 Call the Execute Method action action = (action) actionObject;// Call implementation class execute method string Returnresult = action.execute ();// 5 Find the return value object resultobject = Actionbean.getresultmap (). Get (Returnresult);if (resultobject != null) {ResultBean resultbean = (Resultbean) resultObject;// 6 in struts.xml corresponding jspstring Result = resultbean.getresulttext ();// 7 jump Request.getrequestdispatcher (Result). Forward ( Request,response); return;} else {system.out.println ("Value of result not found:" + 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 ();}} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("The path does not exist:" + path); Chain.dofilter (request, response);} Chain.dofilter (request, response);} Execution at tomcat startup, completion of struts configuration information initialization @overridepublic void init (FILTERCONFIG&NBSP;CONFIG) throws servletexception {string path = config.getservletcontext (). GetRealPath (" Web-inf/classes/struts.xml "); Actionmap=readxml.readxml (path);}}
This article is from the "Ming" blog, make sure to keep this source http://8967938.blog.51cto.com/8957938/1696229
Imitation STRUTS2 implementation action jump