Receiving parameters of the Action attribute in Struts2

Source: Internet
Author: User

Receiving parameters of the Action attribute in Struts2

In Struts2, there are three ways to pass and receive parameters for the Action property. The first is to add the member property receiving parameters for the Action, and the second is the domain model, it is to use the object domain to transmit and receive parameters, and the third is the ModelDriven receiving parameter. Next we will introduce these three methods.

 

1. Add a member property receiving parameter to the Action: Set the property in the Custom Action, and encapsulate the property using the Setters and Getters methods.

First, create a struts2 project named ActionAttrParamInput. Then, write the view and open the index. jsp page. The Code is as follows:

The index. jsp code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'index. jsp 'starting page

 

Create a user_success.jsp page and modify the Code as follows:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'user _ success. jsp 'starting page User Added
Username: $ {param. name}
Age: $ {param. age}

 

Create a UserAction class under the src directory and place it in the com. gk. action package. The Code is as follows:

 

package com.gk.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String add() {return "success";}}

 

 

Configure the struts. xml configuration file with the following code:

 

       
 
  
  
  
   
    /user_success.jsp
   
  
 


 

 

Then deploy the project to the Tomcat server and enable the Tomcat server. The running effect is as follows:

Click Add to go to another page:

If you do not need to submit a form, you can use a hyperlink to transmit parameters through a url, which may cause Chinese garbled characters, the solution is to open the server under the conf directory under the Tomcat installation directory. xml file, with a red arrow pointing to the sentence:

 

 

2. in the domain model, the attribute is first encapsulated into a class, that is, the domain, and then passed to the custom Action class as an attribute. Therefore, we can modify the code of the above project:

First, open the index. jsp page and modify the Code as follows:

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'index. jsp 'starting page

 

 

Open the user_success.jsp page and modify the Code as follows:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'user _ success. jsp 'starting page User Added
Username: $ {user. getName ()}
Age: $ {user. getAge ()}


Create a User class under the src directory, put it in the com. gk. bean package, and put the form content attribute to be entered in a JavaBean, that is, the field. The Code is as follows:

package com.gk.bean;public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}


Open the UserAction. java file and modify the code. The complete code is as follows:

Package com. gk. action; import com. gk. bean. user; import com. opensymphony. xwork2.ActionSupport; public class UserAction extends ActionSupport {private User user User; // declare the User class Object public user getUser () {return User;} public void setUser (user User user) {this. user = user;} public String add () {return "success ";}}

 

Do not modify the struts. xml configuration file.


The project running effect is as follows:

Click the Add button as follows:

In fact, this does not seem to be the property receiving parameter of the Action. It is like receiving form parameters. Next, we use another method to jump to the page by using hyperlinks.

 

 

3. ModelDriven receives parameters and enables Action to implement ModelDriven This interface must be generic when implementing the interface. Otherwise, it needs to be transformed and Its getModel () method is used to return the object model to obtain the passed parameters, we can also modify the project on it:

First, open the index. jsp page and modify it to 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+"/";%>
My JSP 'index. jsp 'starting page Use ModelDriven to receive parameters to add users

 

 

Open the user_success.jsp page and modify it to the following code. Use the tag inherent in struts2 to obtain the parameter:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags"  prefix="s"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'user _ success. jsp 'starting page User Added
Username:
Age:


Next, modify the UserAction class and the code is as follows:

Package com. gk. action; import com. gk. bean. User; import com. opensymphony. xwork2.ActionSupport; import com. opensymphony. xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven
 
  
{Private User user = new User (); // declare the User Class Object public String add () {return "success" ;}@ Overridepublic User getModel () {// TODO Auto-generated method stubreturn user ;}}
 


The User. java file and struts. xml configuration file do not need to be modified. After modification, run the file as follows:

Click Add User. The effect is as follows:

After a parameter is input to an Action, the ModelDriven object is automatically assigned a value to the corresponding property of the User object based on the parameter name, and the User object is generated and returned by getModel. Then we can use this object in Action. Note: (The passed parameter name must be consistent with the member attribute in the object model)

Struts2 focuses on the mvc c layer, that is, compiling custom actions to coordinate the connection between Model and View.

Working principle: the Filter class receives the parameters passed by the Client, and first creates a new User object, checks the Action, finds that the ModelDriven interface has been implemented, and then calls the getModel () method, call set and get for the returned Model to encapsulate the value of the User object.

 

The above is the property receiving parameter of Action in Struts2. There are three ways to pass and receive parameters. It is for your reference only. It is not well written. Please forgive me. If there is an error, please note that, thank you!

Zookeeper

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.