Struts2 两个Action 动态传参数,在struts.xml中配置



Struts2 两个Action 动态传参数,在struts.xml中配置

1.比如:

添加一个部门时,点击保存后,转向部门列表。

 

添加部门Action:addDepartmentAction

显示部门Action:listDepartmentAction

 

那struts.xml的配置为:

<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>
如果有其他要求,比如点击保存后,要把上级部门的id(parentId),传给listDepartmentAction。

那struts.xml的配置改为:

<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>
那么parentId=${parentId}怎么获得?

要在addDepartmentAction里要配置个属性parentId,而且要有get()、set()方法

 

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 department;
private DepartmentService 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 department) {
this.department = department;
}

public DepartmentService getDepartmentService() {
return departmentService;
}

public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}

public String execute() throws Exception {

HttpServletRequest request = ServletActionContext.getRequest();
this.parentId = Long.parseLong( request.getParameter(“parentId”));

return SUCCESS;
}
}