Dynamic invocation method of action in Struts2

Source: Internet
Author: User
Tags string find

In Struts2, the action does not have to be executed at the time of execution, we can specify which method the action executes, and here are three ways to specify which method the action executes:


1. The first method, through the method property in action, specifies how to execute, and we can configure the action in the Struts.xml configuration file with method= "" to specify which method to execute.

(1). Next attach an example, the first way to specify the execution method, first of all, copy a STRUTS2 environment has been set up a Web project, here we copy actiontest this project, and then paste, appear:

To modify the project name, click OK:

Then select the Actionmethod this project, right-click Properties, according to the arrow tip information to modify:

Change the Web Context-root to/actionmethod, click the OK button, click the Finish button of the pop-up box, and then delete the pages we don't need.


(2). First create a new Useraction.java class, placed under the Com.gk.user package, inherit from the Actionsupport class, there are 3 methods, a login () method, an Add () method, a Delete () method, The method used to specify the property of the method in action, the code is as follows:

Package Com.gk.user;import Com.opensymphony.xwork2.actionsupport;public class Useraction extends actionsupport{ Public String Login () {return "Success";} Public String Add () {return "Success";} Public String Delete () {return "Success";}}

(3). Then open the index.jsp page we need, change the code to Utf-8, and add three hyperlinks to this page, as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The value of the href attribute inside the hyperlink tag <a> is the action name, which is the name property of the action in the Struts.xml file.


(4). Next, create three JSP pages, change the code to Utf-8, when clicking on the user login, through action to forward to the User_login.jsp page, when clicking Add User, through action forward to User_ Add.jsp page, when clicking Delete User, through action forward to user_delete.jsp page, the code is as follows:

user_login.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


user_add.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

user_delete.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

(5). Next we can configure the Struts.xml file, open the Struts.xml file, and modify the code as follows:

<?xml Version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dt Ds/struts-2.0.dtd "> <struts><package name=" User "namespace="/"extends=" Struts-default "><action N Ame= "Userlogin" class= "com.gk.user.UserAction" method= "Login" ><result name= "Success" >/User_login.jsp< /result></action><action name= "Useradd" class= "Com.gk.user.UserAction" method= "Add" ><result name = "Success" >/user_add.jsp</result></action><action name= "Userdelete" class= " Com.gk.user.UserAction "method=" delete "><result name=" Success ">/user_delete.jsp</result></ Action></package></struts> 
The name of the first action tag, named Userlogin,class, is the Useraction class under the package, and the method is login, As long as there is a method under Useraction login and return a type of string can be forwarded to the specified view under the result child tag, if there is no method attribute, the default is to execute the Execute () method, followed by 2 action similar.


(6). Next deploy this app to the Tomcat server, turn on the Tomcat server, enter the address in the Address bar: http://localhost:8083/ActionMethod/index.jsp, the page is as follows:

When you click on the user login hyperlink, the interface appears, and the address bar becomes the Name property of the action tag in the Struts.xml file, and also the value of the href attribute of the hyperlink <a> tag, in fact, it jumps to the view under an action with a hyperlink:

Click Add Users and delete users as follows:


2. The second method, a dynamic method called DMI, allows us to dynamically specify which method the action executes in the URL address, by action+!+ the method name. (Note: As long as there is this method in the action object, and the return type is string, you can call the Struts.xml file configuration file without the need to configure the methods property):

(1) Then take the above project as an example, modify the code in it, add more than two methods in the Useraction.java class, respectively, the update () method and the Find () method, the return type is string, the code is as follows:

Package Com.gk.user;import Com.opensymphony.xwork2.actionsupport;public class Useraction extends actionsupport{ Public String Login () {return "Success";} Public String Add () {return "Success";} Public String Delete () {return "Success";} Public String Update () {return ' Update ';} Public String Find () {return ' Find ';}}


(2). Next, modify the Index.jsp page with the following code:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The value for the href attribute in the user link and query user link is Action+!+method, if the Name property of our action is Userupdate, the method is update, it can be written as userupdate!update, Then we can no longer struts.xml the file to configure the method property.


(3). The next new 2 JSP page, the code changed to Utf-8, when the user clicked to modify the user hyperlink, jump to the user_update.jsp page, when the user click the query user link, jump to user_find.jsp page, the code is as follows:

user_update.jsp page:

<%@ page Language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


user_find.jsp page:

<%@ page Language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


(4). Next open the Struts.xml file and modify it, which must include the following line in the Struts.xml file:

<constant name= "Struts.enable.DynamicMethodInvocation" value= "true" ></constant>

This property sets whether struts 2 supports dynamic method calls, or if there is no line to add, otherwise it will report an error, if you add this line, but you change the value to False, will also error!

Struts.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dt Ds/struts-2.0.dtd "><struts><constant name=" Struts.enable.DynamicMethodInvocation "value=" true "> </constant><package name= "User" namespace= "/" extends= "Struts-default" ><action name= "Userlogin" class= "com.gk.user.UserAction" method= "Login" ><result name= "Success" >/User_login.jsp</result>< /action><action name= "Useradd" class= "Com.gk.user.UserAction" method= "Add" ><result name= "Success" >/ User_add.jsp</result></action><action name= "Userdelete" class= "Com.gk.user.UserAction" method= " Delete "><result name=" Success ">/user_delete.jsp</result></action><action name=" Userupdate "class=" com.gk.user.UserAction "><result name=" Update ">/user_update.jsp</result></ Action><action name= "Userfind" class= "Com.gk.user.UseRaction "><result name=" Find ">/user_find.jsp</result></action></package></struts >

This article describes the last two action,name as the action name, class for the Useraction classes under the package, without specifying method methods, In the next two hyperlinks defined under index.jsp, you can jump to the corresponding page through the action request by adding an exclamation point to the action name, plus the method name defined under Useraction.


(5). Then redeploy, turn on the Tomcat server and run the following results:

Click the Modify user hyperlink and query user hyperlink as shown in:



The red text that changes to the address bar after the jump.


3. Let's summarize the following:

(1). the Execute method is not necessarily executed when the Action is executed .

(2). You can configure the Action in the configuration file by using method= to specify which method to execute , or at the URL dynamically specified in the address (the dynamic method calls DMI) (recommended), which generates too many action , so it is not recommended.


The above content is only for everyone to reference the study, I also learn from the teacher's knowledge, write well, please forgive me, if there are errors, please point out, thank you!





Dynamic invocation method of action in Struts2

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.