Struts2 Learning Note Eight: Dynamic invocation of methods in action

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.