Method One: Add the AddUser () and Deluser () methods to the action name +!+ method name + suffix action class,
Packagecom.djoker.struts2;ImportOrg.apache.struts2.ServletActionContext;Importcom.opensymphony.xwork2.Action; Public classuseraction {PrivateString username; PrivateString password; Private intAge ; PrivateString address; PrivateString Email; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } PublicString Execute ()throwsException {System.out.println ("Username:" +username); System.out.println ("Password:" +password); System.out.println ("Age:" +Age ); System.out.println ("Address" +address); System.out.println ("Email:" +email); return"Success"; } PublicString AddUser ()throwsException {servletactioncontext.getrequest (). SetAttribute ("MSG", username); returnaction.success; } PublicString Deluser ()throwsException {servletactioncontext.getrequest (). SetAttribute ("MSG", username); returnaction.success; }}
Action configuration in Struts.xml (note that there is no detailed method in the configuration file, just a name is specified)
< Packagename= "User"extends= "Struts-default"> <Actionname= "User"class= "Com.djoker.struts2.UserAction"> <result>/success.jsp</result> <resultname= "Error">/error.jsp</result> </Action> </ Package>
How to invoke the different methods in the action in the JSP
<formAction= "User!adduser.action"Method= "POST"><!--form action= "User!deluser.action" method= "POST" -User name:<inputtype= "text"name= "username"><BR>Password:<inputtype= "Password"name= "Password"><BR>Age:<inputtype= "text"name= "Age"><BR>Address:<inputtype= "text"name= "Address"><BR>Email:<inputtype= "text"name= "Email"><BR> <inputtype= "Submit"value= "Login"> </form>
Method Two: Multiple method calls, create multiple actions to specify different methods (this method is not recommended, if there are multiple methods that need to write multiple actions) the configuration in the Struts.xml method
< Packagename= "User"extends= "Struts-default"> <Actionname= "AddUser"class= "Com.djoker.struts2.UserAction"Method= "AddUser"> <result>/success.jsp</result> <resultname= "Error">/error.jsp</result> </Action> <Actionname= "Deluser"class= "Com.djoker.struts2.UserAction"Method= "Deluser"> <result>/success.jsp</result> <resultname= "Error">/error.jsp</result> </Action> </ Package>
How to invoke in JSP
<formAction= "Adduser.action"Method= "POST"><!--form action= "Deluser.action" method= "POST" -User name:<inputtype= "text"name= "username"><BR>Password:<inputtype= "Password"name= "Password"><BR>Age:<inputtype= "text"name= "Age"><BR>Address:<inputtype= "text"name= "Address"><BR>Email:<inputtype= "text"name= "Email"><BR> <inputtype= "Submit"value= "Login"> </form>
Method Three: Use wildcard character configuration, you need to follow a certain specification Struts.xml in the configuration using wildcards
< Packagename= "User"extends= "Struts-default"> <Actionname= "*user"class= "Com.djoker.struts2.UserAction"Method= "{1}user"> <result>/success.jsp</result> <resultname= "Error">/error.jsp</result> </Action> </ Package>
The JSP needs to be called by this specification.
Struts2 Learning Note Eight: Dynamic invocation of methods in action