Two actions in struts2 dynamically pass parameters, which are configured in struts. xml

Source: Internet
Author: User

1. For example:

When adding a department, click Save to go to the department list.

 

ADD teamAction: Adddepartmentaction

Show DepartmentAction: Listdepartmentaction

 

The Struts. xml configuration is as follows:

<Action name = "adddepartmentaction" class = "com. XK. Department. adddepartmentaction">
<Result name = "success" type = "Redirect"> listdepartmentaction. Action <result>
</Action>

<Action name = "listdepartmentaction" class = "com. XK. Department. listdepartmentaction">
<Result name = "success">/list. jsp </result>
</Action>
If you have other requirements, for example, after you click Save, you need to pass the ID (parentid) of the upper-level department to listdepartmentaction.

Modify the Struts. xml configuration:

<Action name = "adddepartmentaction" class = "adddepartmentaction">
<Result name = "success" type = "Redirect"> listdepartmentaction. Action? Parentid =$ {parentid} </result>
</Action>

<Action name = "listdepartmentaction" class = "listdepartmentaction">
<Result name = "success">/list. jsp </result>
</Action>

So how to obtain parentid =$ {parentid?

You need to configure a parentid attribute in adddepartmentaction, And the get () and set () methods must be available.

 

Package com. XK. Department;

Import javax. servlet. http. httpservletrequest;

Import org. Apache. struts2.servletactioncontext;

Import com. opensymphony. xwork2.actionsupport;
Import com. XK. OA. domain. Department;
Import com. XK. OA. Service. departmentservice;
Import com. XK. OA. util. requestutils;

@ Suppresswarnings ("serial ")
Public class adddepartmentaction extends actionsupport {

Private Department;
Private departmentservice;
Private long parentid;
 
Public long getparentid (){
Return parentid;
}

Public void setparentid (long parentid ){
This. parentid = parentid;
}

Public Department getdepartment (){
Return department;
}

Public void setdepartment (Department ){
This. Department = Department;
}

Public departmentservice getdepartmentservice (){
Return departmentservice;
}

Public void setdepartmentservice (departmentservice ){
This. departmentservice = departmentservice;
}

Public String execute () throws exception {

Httpservletrequest request = servletactioncontext. getrequest ();

This. parentid = long. parselong (request. getparameter ("parentid "));

Return success;
}
}

 

 

 

 

 

 

Related Article

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.