一、分析
1,设计实体/表
设计实体 --> JavaBean --> hbm.xml --> 建表
2,分析有几个功能,对应几个请求。
3,实现功能:
1,写Action类,写Action中的方法,确定Service中的方法。
2,写Service方法,确定Dao中的方法。
3,写Dao方法。
4,写JSP
============================
请求数量 地址栏
转发 1 不变
重定向 2 变化
增删改查共4个功能,需要6个请求。
所以需要相应的6个Action方法,每个Action方法处理一种请求。
作用 方法名 返回值 对应的页面
----------------------------------------------------
列表 list() list list.jsp
删除 delete() toList
添加页面 addUI() addUI addUI.jsp
添加 add() toList
修改页面 editUI() editUI editUI.jsp
修改 edit() toList
<result name="toList" type="redirectAction">role_list</result>
role_* ---> {1}
role_list list
role_addUI addUI
role_delete delete
二、代码
1.Action层
1 package cn.itcast.oa.view.action; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.context.annotation.Scope; 8 import org.springframework.stereotype.Controller; 9 10 import cn.itcast.oa.domain.Role; 11 import cn.itcast.oa.service.RoleService; 12 13 import com.opensymphony.xwork2.ActionContext; 14 import com.opensymphony.xwork2.ActionSupport; 15 import com.opensymphony.xwork2.ModelDriven; 16 17 @Controller 18 @Scope("prototype") 19 public class RoleAction extends ActionSupport implements ModelDriven<Role> { 20 21 private static final long serialVersionUID = 1L; 22 23 @Resource 24 private RoleService roleService; 25 26 // private Long id; 27 // private String name; 28 // private String description; 29 30 private Role model = new Role(); 31 32 public Role getModel() { 33 return model; 34 } 35 36 /** 列表 */ 37 public String list() throws Exception { 38 List<Role> roleList = roleService.findAll(); 39 ActionContext.getContext().put("roleList", roleList); 40 return "list"; 41 } 42 43 /** 删除 */ 44 public String delete() throws Exception { 45 roleService.delete(model.getId()); 46 return "toList"; 47 } 48 49 /** 添加页面 */ 50 public String addUI() throws Exception { 51 return "saveUI"; 52 } 53 54 /** 添加 */ 55 public String add() throws Exception { 56 // // 封装到对象中 57 // Role role = new Role(); 58 // role.setName(model.getName()); 59 // role.setDescription(model.getDescription()); 60 // 61 // // 保存到数据库 62 // roleService.save(role); 63 64 roleService.save(model); 65 66 return "toList"; 67 } 68 69 /** 修改页面 */ 70 public String editUI() throws Exception { 71 // 准备回显的数据 72 Role role = roleService.getById(model.getId()); 73 ActionContext.getContext().getValueStack().push(role); 74 75 return "saveUI"; 76 } 77 78 /** 修改 */ 79 public String edit() throws Exception { 80 // 1,从数据库中获取原对象 81 Role role = roleService.getById(model.getId()); 82 83 // 2,设置要修改的属性 84 role.setName(model.getName()); 85 role.setDescription(model.getDescription()); 86 87 // 3,更新到数据库 88 roleService.update(role); 89 90 return "toList"; 91 } 92 93 // --- 94 95 // public Long getId() { 96 // return id; 97 // } 98 // 99 // public void setId(Long id) { 100 // this.id = id; 101 // } 102 // 103 // public String getName() { 104 // return name; 105 // } 106 // 107 // public void setName(String name) { 108 // this.name = name; 109 // } 110 // 111 // public String getDescription() { 112 // return description; 113 // } 114 // 115 // public void setDescription(String description) { 116 // this.description = description; 117 // } 118 }
2.Service层
1 package cn.itcast.oa.service.impl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import cn.itcast.oa.dao.RoleDao; 11 import cn.itcast.oa.domain.Role; 12 import cn.itcast.oa.service.RoleService; 13 14 @Service 15 @Transactional 16 public class RoleServiceImpl implements RoleService { 17 18 @Resource 19 private RoleDao roleDao; 20 21 public Role getById(Long id) { 22 return roleDao.getById(id); 23 } 24 25 public void delete(Long id) { 26 roleDao.delete(id); 27 } 28 29 public void save(Role role) { 30 roleDao.save(role); 31 } 32 33 public void update(Role role) { 34 roleDao.update(role); 35 } 36 37 public List<Role> findAll() { 38 return roleDao.findAll(); 39 } 40 41 }
3.Dao层
1 @Repository //这里写了@Repository,则父类BaseDaoImpl的sessionFactory可以注入 2 public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao { 3 4 }
4.View层
(1)saveUI.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>岗位设置</title> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7 <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> 8 <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> 9 <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> 10 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /> 11 <script type="text/javascript"> 12 </script> 13 </head> 14 <body> 15 16 <!-- 标题显示 --> 17 <div id="Title_bar"> 18 <div id="Title_bar_Head"> 19 <div id="Title_Head"></div> 20 <div id="Title"><!--页面标题--> 21 <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置 22 </div> 23 <div id="Title_End"></div> 24 </div> 25 </div> 26 27 <!--显示表单内容--> 28 <div id="MainArea"> 29 30 <s:form action="role_%{ id == null ? ‘add‘ : ‘edit‘ }"> 31 <s:hidden name="id"></s:hidden> 32 33 <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1"> 34 <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV> --> 35 </div> 36 37 <!-- 表单内容显示 --> 38 <div class="ItemBlockBorder"> 39 <div class="ItemBlock"> 40 <table cellpadding="0" cellspacing="0" class="mainForm"> 41 <tr> 42 <td width="100">岗位名称</td> 43 <td><s:textfield name="name" cssClass="InputStyle" /> *</td> 44 </tr> 45 <tr> 46 <td>岗位说明</td> 47 <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td> 48 </tr> 49 </table> 50 </div> 51 </div> 52 53 <!-- 表单操作 --> 54 <div id="InputDetailBar"> 55 <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/> 56 <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> 57 </div> 58 </s:form> 59 </div> 60 61 </body> 62 </html>
(2)list.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>My JSP ‘index.jsp‘ starting page</title> 6 </head> 7 <body> 8 9 <%-- 10 <s:iterator value="#roleList"> 11 <s:property value="id"/>, 12 <s:property value="%{name}"/>, 13 <s:property value="description"/>, 14 <s:a action="role_delete?id=%{id}" onclick="return confirm(‘确定要删除吗?‘)">删除</s:a>, 15 <s:a action="role_editUI?id=%{id}">修改</s:a> 16 <br/> 17 </s:iterator> 18 --%> 19 20 <s:iterator value="#roleList"> 21 ${id}, 22 ${name}, 23 ${description}, 24 <s:a action="role_delete?id=%{id}" onclick="return confirm(‘确定要删除吗?‘)">删除</s:a>, 25 <s:a action="role_editUI?id=%{id}">修改</s:a> 26 <br/> 27 </s:iterator> 28 29 <br/> 30 <s:a action="role_addUI">添加</s:a> 31 32 </body> 33 </html>
时间: 2024-10-12 14:07:22