3. 实现功能
1. 写action, 写action中的方法, 确定service中的方法
2. 写service, 确定Dao中的方法
3. 写Dao方法
4. 写jsp
1. 写action里的list和delete方法, 以确实service里需要写的方法
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; @Controller @Scope("prototype") public class RoleAction extends ActionSupport { /*列表*/ @Resource private RoleService roleService; private Long id; public RoleService getRoleService() { return roleService; } public void setRoleService(RoleService roleService) { this.roleService = roleService; } public String list() throws Exception { List<Role> roleList=roleService.findAll(); ActionContext.getContext().put("roleList", roleList); return "list"; } /*删除*/ public String delete() throws Exception { roleService.delete(id); return "toList"; } /*添加页面*/ public String addUI() throws Exception { return "addUI"; } /*添加*/ public String add() throws Exception { return "toList"; } /*修改页面*/ public String editUI() throws Exception { return "editUI"; } /*修改*/ public String edit() throws Exception { return "toList"; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
2. 写service, 确定Dao中的方法
在创建RoleAction方法时候写RoleService的时候, 就提示新建的方法了, 然后因为RoleServiceImpl继承了RoleService, 所以在RoleServiceImpl里红色波浪线添加这两个方法即可.
创建RoleService.java
package cn.itcast.oa.service; import java.util.List; import cn.itcast.oa.domain.Role; public interface RoleService { //查询所有 List<Role> findAll(); void delete(Long id); }
创建RoleServiceImpl.java
package cn.itcast.oa.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.oa.dao.RoleDao; import cn.itcast.oa.domain.Role; import cn.itcast.oa.service.RoleService; @Service @Transactional public class RoleServiceImpl implements RoleService{ @Resource private RoleDao roleDao; @Override public List<Role> findAll() { return roleDao.findAll(); } @Override public void delete(Long id) { roleDao.delete(id); } }
3. Dao方法已经存在
4. 写jsp:
原文:http://www.cnblogs.com/wujixing/p/5498948.html