Struts dynamic method call Change Form Action property

Source: Internet
Author: User

One, dynamic method invocation (Dmi:dynamic methods invocation)

The ⒈STRUTS2 also provides this action, which contains multiple logical processes, so that multiple business logic can be processed in one action. For example, when a user submits the same form through a different submit button, the system handles different requests by different methods, and the same action is required to contain logic for multiple control processing.

⒉ dynamic method calls are:

①, changes the method property in the action in the Struts.xml.

②, changing the Action property in the form form to change the request logic for different commits.

③, use wildcard characters.

Second, simple example (change the Action property in form form):

①, first displays a form with two submit buttons, but each represents a different business. When you click Sign in, the user logs in, and when you click Register, the user registers.

   

②, User login:

   

③, User registration:

   

Detailed code (this example builds on a simple example based on struts2, so Struts2 's build is not shown here in detail, if there is a question for struts2 to see: http://www.cnblogs.com/demoMeng/p/5841976.html):

①, Login Registration page (INDEX.JSP): The way in which the Action property in the form form is changed in DMI, the following script fragment is critical, and the other Struts.xml files are configured as long as they are relevant.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><script src= "Js/jquery-1.7.2.js" ></script><title>index</title>
<script type= "Text/javascript" >$ (function () {$ ("Input:eq (3)"). Click (function () {///Get the form and change the action's property value $ ("#form"). attr ("Action", "Usercreate")); }); $("Input:eq (2)"). Click (function () {///Get the form and change the Action property value $ ("#form"). attr ("Action", "Userlogin")); }); });</script>Name:<input type= "text" name= "name"/><br><br>Password:<input type= "password" name= "password"/><br><br> <input type= "Submit" value= "Login" & gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp; <input type= "Submit" value= "Registration" > </form></body>

②, struts.xml: Configuration file

<struts>        <packageextends= "Struts-default" >            class= "action." Action "method=" Go ">                    <result name=" Login ">WEB-INF/jsp/index.jsp</result>            </action>            Class= "action. Action "method=" Test ">                    <result name=" Userlogin ">WEB-INF/jsp/userLogin.jsp</result>            </ Action>            Class= "action. Action "method=" "Create" >                    <result name= "Usercreate" >WEB-INF/jsp/userCreate.jsp</result>            </action>        </package >    </struts>

      

③, Action class:

      

 PackageAction;ImportCom.opensymphony.xwork2.ActionSupport; Public classActionextendsActionsupport {PrivateString name; PrivateString password;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }             PublicString Go () {System.out.println ("Go to login registration page!!" "); return"Login"; }         PublicString Test () {System.out.println ("User Login"); return"Userlogin"; }         PublicString Create () {System.out.println ("User Registration"); return"Usercreate"; }    }

    

This example is just one of the simple DMI methods, and does not include excessive business logic processing such as: The user is logged in correctly and is eligible. Just a simple example, specific actual combat needs to use the business needs to further modify the analysis and refinement, thank you for browsing.

     

Struts dynamic method call Change Form Action property

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.