1. Defining properties of simple data types in action
Defines a simple type of property for the action that encapsulates the data requested by the client
Simple type: String, base type, and corresponding reference type
Just ensure that the parameter name requested by the client is the same as the property name of the action.
2. Defining the JavaBean object in action encapsulates client data2.1 Defining entity Classes
Public class Users { Private String uname; Private String upwd; Private Integer type; Public String Getuname () { return uname; } Public void setuname (String uname) { this. uname = uname; } Public String getupwd () { return upwd; } Public void setupwd (String upwd) { this. upwd = Upwd; } Public Integer GetType () { return type; } Public void setType (Integer type) { this. Type = type; } } |
2.2 Defining an action
The action defines the JavaBean object as a property
Public class UserAction2 { /*** * Encapsulates the data requested by the client using the JavaBean object as a property of the action * * If a property exists in the users class for the score type, there is an attribute in the Score class integer type num * How to write the parameter name? User.score.num (attribute chain), the struts2 frame will automatically be on each of the attribute chains * JavaBean types are instantiated (OGNL) * **/ private Users user; Public Users GetUser () { return user; } Public void setUser (Users user) { this. user = user; } Public String USERREG2 () { System. Out. println (User.getuname () + "= = =" +user.getupwd () + "= = =" +user.gettype ()); return Action. SUCCESS; } } |
2.3 Registering an action2.4 Defining UI pages
<!--use JavaBean as the Action property-- <form action="useraction2_userreg2.action " method="POST"> <div>uname: <input type="text" name="User.uname"/></div> <div>upwd : <input type="text" name="user.upwd "/></div> <div>type: <select name="User.type"> <option value="1"> Admin </option> <option value="2"> Users </option> </select> </div> <div> <input type="Submit" value=" register "/></div> </form> |
Summary: The STRUTS2 framework automatically obtains parameters for client requests, automatic type conversions, and automatic encapsulation of business objects as needed.
3 Implementing Interface Modeldriven
Modeldriven: The interface uses an OGNL expression to complete the encapsulation of the object execution, and the return value of the Getmodel method is the object that is accessed by default.
3.1 Defining an action
Public class UserAction3 implements modeldriven<users> { Define the properties of an action and instantiate it at the same time Private Users user=new users ();
/** * This method encapsulates the parameters requested by the client into the users object, placing the users object in the * In the value stack of the action. (Ongl) * * If the Modeldriven interface is implemented, * Request parameters are encapsulated by default in the return value object with the Getmodel method * When getting the desired execution, the default is obtained by the return value object of the Getmodel method * * Benefits: Simplified access to parameters (name of client parameter, simplified form of El Representation) * ***/ @Override Public Users Getmodel () { System. out. println ("---------------" +user); return user; } Public String UserReg3 () { System. out. println (User.getuname () + "-------" +user.getupwd () + "-------" +user.gettype ()); return Action. SUCCESS; } } |
3.2 Defining UI pages
<!--encapsulates client data by implementing the Modeldirven interface-- <form action="useraction3_userreg3.action " method="POST"> <div>uname: <input type="text" name="uname"/></div> <div>upwd : <input type="text" name="upwd"/></div> <div>type: <select name="type"> <option value="1"> Admin </option> <option value="2"> Users </option> </select> </div> <div> <input type="Submit" value=" register "/></div> </form> |
Struts encapsulates client data to action