SSH Action使用ModelDriver实现最简单的增册改查

本文介绍了一个基于Struts2框架的角色管理Action实现,通过RoleAction类实现了角色的增删改查功能。该Action利用Spring框架进行依赖注入,并通过RoleService接口与业务逻辑层交互,实现了角色列表展示、新增、编辑及删除等操作。
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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值