1. Action receives the value passed by JSP:
A. Define a user class with the name PWD attribute and add the corresponding get and set methods.
B. <! -- Method 1 common Java class -->
In useraction
Add: private user, and add the corresponding get and set methods.
<! -- Method 2: modeldriven <t> interface -->
In useraction, @ override public user GetModel () {return user ;}
<! -- Method 3: directly write attributes in useraction>
private String name; private String pwd; public String methodxxx() { User user=new User(); ...xxx; }
Note: The following JSP automatically receives the new user () object when the value is obtained. Therefore, in methodxxx, the attribute must be determined and the user cannot be empty.
C. Add a form in the JSP Interface
<Form action = "user_register" method = "Post"> <label style = "color: red; ">$ {errormsg} </label> <br/> register the username <input type =" text "name =" user. name "/> <br/> Registration password <input type =" password "name =" user. PWD "/> <br/> <input type =" Submit "value =" register "/> </form>
After the form is submitted, the user can be directly used in the register of useraction.
The framework directly stores the data in the user
If the class in the action is not defined, the default action in the framework is called.
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
--------------------------------- The above content is in the G: codespace/struts2 Code Range Test --------------------------
2.Accept in JSP for passing action values:
All three methods are processed in action.
(1) decoupling: indirect use
Public String execute () {// obtain the context actioncontext AC = actioncontext of the action. getcontext (); // ① obtain the Map <string, Object> request = (Map <string, Object>) AC. get ("request"); Map <string, Object> session = ac. getsession (); Map <string, Object> application = ac. getapplication (); // store the value request. put ("reqvalue", "this is the value set by request coupling method"); Session. put ("sessionvalue", "this is the value set through session decoupling"); application. put ("applicationvalue", "this is the value set through application decoupling"); Return "success ";}
(2) Implement requestaware, sessionaware, and applicationaware.
Public class testaction02 implements requestaware, sessionaware, applicationaware {private Map <string, Object> request; private Map <string, Object> session; private Map <string, Object> application; public String execute () {// 2. stored value request. put ("reqvalue", "this is the value set through request decoding coupling requestaware"); Session. put ("sessionvalue", "this is the value set through session decoding coupled with sessionaware"); application. put ("applicationvalue", "this is the value set through the application solution coupling applicationaware method"); Return "success" ;}@ override public void setapplication (Map <string, Object> Application) {This. application = Application ;}@ override public void setsession (Map <string, Object> session) {This. session = session;} @ override public void setrequest (Map <string, Object> request) {This. request = request ;}}
(3) Direct Coupling
Public String execute () {// coupling: Use httpservletrequest request = servletactioncontext directly. getrequest (); httpsession session = request. getsession (); servletcontext application = servletactioncontext. getservletcontext (); // 2. stored value request. setattribute ("reqvalue", "this is the value set by request coupling"); Session. setattribute ("sessionvalue", "this is the value set through session coupling"); application. setattribute ("applicationvalue", "this is the value set through application coupling"); Return "success ";}
------ JSP for the same implementation of the above three values
JSP value Interface
<H1> request value: $ {requestscope. reqvalue} <br/> request value: s tag --- <s: property value = "# request. reqvalue "/> <br/> session value: $ {sessionscope. sessionvalue} <br/> session value: s tag --- <s: property value = "# session. sessionvalue "/> <br/>...
(4) I don't know how to say ------------------------ here it is to be improved.
ValuestackImplemented by the ognl framework, you can simply regard it as a list)
Stack Context(The storage method is map type): Stack context, which contains a series of objects, including
Values stored in valuestack, such as request, session, ATTR, application, and map can be directly obtained, while values in stack must be prefixed with # (request, session, Application)
Public String execute () {actioncontext AC = actioncontext. getcontext (); // vs pushes values to value stack contents. valuestack Vs = ac is implemented by the ognl framework. getvaluestack (); User. setname ("Zhang San"); User. setpwd ("ABC");. push (User); User U1 = new user (); u1.setname (""); u1.setpwd ("2345");. push (U1); // here, the value is passed into the context of the stack context (the storage method is map type) stack, which contains a series of object Map <string, object> request = (Map <string, Object>) AC. get ("request"); // 2. stored value request. put ("reqvalue", "this is the value set by request coupling"); Request. put ("user", user); Return "success ";}
JSP value Interface
Note: The S: Property tag can call the class method.