最近写程序时遇见一个需求,在一个action完成后,在最后的return时得跳转到另外一个action中,并且需要改变URL, 所以只能用redirect, 但需要传递几个参数到另外一action中。于是想到了struts2注解中params传参,但在网上找了好多没没有一个比较详细的例子和具体的代码。现在总结一下具体用法:
用法如下:
params={"key1", "value1", "key2", "value2"})
key为action中一个 带set 方法的变量 , value是需要传递的值。value的写法是:%{value}
代码如下:
成员变量:
private boolean flagTaskDetail;
private int taskId;
成员变量对应的get和set方法:
public boolean isFlagTaskDetail() {
return flagTaskDetail;
}
public void setFlagTaskDetail(boolean flagTaskDetail) {
this.flagTaskDetail = flagTaskDetail;
}
public int getTaskId() {
return taskId;
}
public void setTaskId(int taskId) {
this.taskId = taskId;
}
params用法:
@Action(value="updateTaskInfo", results = {
@Result(name = "taskDetail", type = "redirect", location = "showTaskDetailInfo",
params={"flagTaskDetail", "%{flagTaskDetail}", "taskId", "%{taskId}"}),
@Result(name = "taskList", type = "chain", location = "onSearchAndListingTasks"),
@Result(name = "error", type = "chain", location = "onSearchAndListingTasks")})
在另外一个action中直接使用这两个变量即可。