SSH Action使用ModelDriver實現最簡單的增冊改查
阿新 • • 發佈:2019-02-08
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 implements ModelDriven<Role> {
private static final long serialVersionUID = 5242842204530086588L;
@Resource
private RoleService roleService;
private Role model = new Role();
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList" , roleList);
return "list";
}
public String addUI() throws Exception {
return "addUI";
}
public String add() throws Exception {
roleService.add(model);
return "toList";
}
public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
}
public String editUI() throws Exception {
//回顯資料
Role role = roleService.getById(model.getId());
ActionContext.getContext().getValueStack().push(role);
return "editUI";
}
public String edit() throws Exception {
//1.獲得實體
Role role = roleService.getById(model.getId());
//2.設值
role.setName(model.getName());
role.setDescription(model.getDescription());
//3.更新
roleService.update(role);
return "toList";
}
@Override
public Role getModel() {
return model;
}
public void setRole(Role role) {
this.model = role;
}
}