3. 实现功能
1. 写action, 写action中的方法, 确定service中的方法
2. 写service, 确定Dao中的方法
3. 写Dao方法
4. 写jsp
1. 完善action里的方法, 以确实service里需要写的方法, 因为action里要使用service, 所以先创建RoleService.java和RoleServiceImpl.java ,先不写方法, 通过action里调用方法的方式新建service里的方法
1) action里创建roleService实例, 加@Resource, 别忘了在RoleServiceImpl上加上@Service和@Transactional
2) action的方法里, 都通过service调用方法findAll(), delete(),save(),update(), getById()
3) 使用ModelDriven,
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>{ /*列表*/ @Resource private RoleService roleService; private Role model = new Role(); public Role getModel() { return model; } 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(model.getId()); return "toList"; } /*添加页面*/ public String addUI() throws Exception { return "saveUI"; } /*添加*/ public String add() throws Exception { roleService.save(model); return "toList"; } /*修改页面*/ public String editUI() throws Exception { Role role = roleService.getById(model.getId()); ActionContext.getContext().getValueStack().push(role); return "saveUI"; } /*修改*/ 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"; } }
2. 写service的实现类RoleServiceImpl, 调用roleDao的方法实现, 注意在roleDao前加@Resource
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(); } public void delete(Long id) { roleDao.delete(id); } public void save(Role role) { roleDao.save(role); } public Role getById(Long id) { return roleDao.getById(id); } public void update(Role role) { roleDao.update(role); } }
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); void save(Role role); Role getById(Long id); void update(Role role); }
3. DAO功能都已实现完成
4. 写jsp文件, list.jsp, saveUI.jsp
list.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'list.jsp' starting page</title> </head> <body> <s:iterator value="#roleList"> ${id}, ${name} ${description} <s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a> <s:a action="role_editUI?id=%{id}">修改</s:a> <br/> </s:iterator> <br/> <s:a action="role_addUI">添加</s:a> </body> </html>
addUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'addUI.jsp' starting page</title> </head> <body> <s:form action="role_add"> <s:textfield name="name"></s:textfield><br/> <s:textarea name="description"></s:textarea><br/> <s:submit value="提交"></s:submit> </s:form> </body> </html>
editUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'editUI.jsp' starting page</title> </head> <body> <s:form action="role_edit"> <s:hidden name="id"></s:hidden> <s:textfield name="name"></s:textfield><br/> <s:textarea name="description"></s:textarea><br/> <s:submit value="提交"></s:submit> </s:form> <s:debug></s:debug> </body> </html>
整合成saveUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%String path = request.getContextPath();%> <html> <head> <title>岗位设置</title> <script language="javascript" type="text/javascript" src=<%=path%>/script/jquery.js"></script> <script language="javascript" type="text/javascript" src=<%=path%>/script/pageCommon.js"></script> <script language="javascript" type="text/javascript" src=<%=path%>/script/PageUtils.js"></script> <link type="text/css" rel="stylesheet" href="<%=path%>/style/blue/pageCommon.css"/> <script type="text/javascript"> </script> </head> <body> <!-- 标题显示 --> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="<%=path%>/style/images/title_arrow.gif"/> 岗位设置 </div> <div id="Title_End"></div> </div> </div> <!--显示表单内容--> <div id="MainArea"> <s:form action="role_%{ id == null ? 'add' : 'edit' }"> <s:hidden name="id"></s:hidden> <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1"> <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV> --> </div> <!-- 表单内容显示 --> <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> <!-- 表单操作 --> <div id="InputDetailBar"> <input type="image" src="<%=path%>/style/images/save.png"/> <a href="javascript:history.go(-1);"><img src="<%=path%>/style/images/goBack.png"/></a> </div> </s:form> </div> </body> </html>
list.jsp也做的漂亮点:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%String path = request.getContextPath();%> <html> <head> <title>岗位列表</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <script language="javascript" type="text/javascript" src=<%=path%>/script/jquery.js"></script> <script language="javascript" type="text/javascript" src=<%=path%>/script/pageCommon.js"></script> <script language="javascript" type="text/javascript" src=<%=path%>/script/PageUtils.js"></script> <link type="text/css" rel="stylesheet" href="<%=path%>/style/blue/pageCommon.css"/> <script type="text/javascript"></script> </head> <body> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="<%=path%>/style/images/title_arrow.gif"/> 岗位管理 </div> <div id="Title_End"></div> </div> </div> <div id="MainArea"> <table cellspacing="0" cellpadding="0" class="TableStyle"> <!-- 表头--> <thead> <tr align="CENTER" valign="MIDDLE" id="TableTitle"> <td width="200px">岗位名称</td> <td width="300px">岗位说明</td> <td>相关操作</td> </tr> </thead> <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey="roleList"> <s:iterator value="#roleList"> <tr class="TableDetail1 template"> <td>${name} </td> <td>${description} </td> <td> <s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a> <s:a action="role_editUI?id=%{id}">修改</s:a> <s:a action="role_setPrivilegeUI?id=%{id}">设置权限</s:a> </td> </tr> </s:iterator> </tbody> </table> <!-- 其他功能超链接 --> <div id="TableTail"> <div id="TableTail_inside"> <s:a action="role_addUI"><img src="<%=path%>/style/images/createNew.png" /></s:a> </div> </div> </div> </body> </html>