【学习背景】
最近的学习又接触到了Struts2,虽然之前有过项目实践,但那毕竟是第一次学习,很多东西都只是有个印
象,并不明白也没有建立其它联系。这一次的学习,对modelDriven有了进一步的认识,也了解到除了modelDriven之
外的,action与jsp页面参数传递的其它两种方式,下面一一总结一下。
【学习积累】
Struts2中Action接收参数的方法主要有以下三种:
1. 使用Action的属性接收参数(最原始的方式)
a. 定义:在Action类中定义属性,创建get和set方法
b. 接收:通过属性接收参数
c. 发送:使用属性名传递参数
如对角色的添加,Action中的代码如下:
package cn.itcast.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport{
@Resource
private RoleService roleService;
private String name;
private String description;
/** 添加 */
public String add() throws Exception {
//1. 封装到对象中
Role role=new Role();
role.setName(name);
role.setDescription(description);
//2. 保存到数据库中
roleService.save(role);
return "toList";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
jsp页面代码:
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div>
这种方式不好的地方在于,如果实体类的属性非常多,那么Action就会产生相同属性的定义。不得不说,代码太
多也很麻烦。
2. 使用DomainModel接收参数
a. 定义:在Action中定义Model类的对象(不需要new),创建该对象的get和set方法
b. 接收:通过对象的属性接收参数
c. 发送:使用对象的属性传递参数
Action代码:
package cn.itcast.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport{
@Resource
private RoleService roleService;
Private Role role;
/** 添加 */
public String add() throws Exception {
roleService.save(role);
return "toList";
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role= role;
}
}
}
jsp页面代码:
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="role.name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="role.description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div>
这样的方式,action中的代码简单了许多,用整个实体对象代替了对实体各个属性的定义。而jsp页面反倒又复
杂了些,仅用属性名不能满足,而需要加上实体对象。
于是,出现了第三种方式,模型驱动。
3. 使用ModelDriven接收参数(现在用的较多的方式)
a. 定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须要new),通过getModel
方法返回该对象
b. 接收:通过对象的属性接收参数
c. 发送:直接使用属性名传递参数
同样地,角色添加,action代码:
package cn.itcast.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
public class RoleAction extends A ctionSupport implements ModelDriven<Role>{
@Resource
private RoleService roleService;
private Role model = new Role();
public Role getModel() {
return model;
}
/** 添加 */
public String add() throws Exception {
roleService.save(model); //仅一行代码便可实现添加操作
return "toList";
}
}
jsp页面代码:
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div>
可见,第三种方式是比较好的,action和jsp写起来都简单了。
【学习总结】
学习就是个不断重复的过程。之前只知道modelDriven模式,掌握得还不算好。通过这一次的学习,知道了Model
Driven方式应该怎样实现,也清楚了其它两种方式的实现。通过对比,发现modelDriven方法的优势所在。