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;
 }
}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值