1.昨天我们分析了岗位管理的Dao设计和功能示意图以及请求个数和页面的分析,下面给出一组功能的步骤:
1,充分了解需求,包括所有的细节,需要知道要做一个什么样的功能2,设计实体/表
正向工程:设计实体、映射文件 建表反向工程:设计表 映射文件、实体
3,分析功能
分析到每个请求的粒度。得到的结果是我们需要处理多少种请求,其中每种请求对应一个Action方法。
4,实现功能:
1,创建Action,并定义出其中的方法。2,实现Action方法,并创建出所用到的新的Service方法。3,实现Service方法,并创建出所用到的新的Dao方法。4,实现Dao方法。5,创建并完成JSP页面。
5,测试、运行
2.根据步骤我们来实现岗位管理的功能,首先创建RoleAction并定义好其中分析的方法,代码如下(以及完善过的代码,注释为没完善前的代码,注意注解和方法细节):
package com.icss.oa.view.action; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.icss.oa.domain.Role; import com.icss.oa.service.RoleService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("serial") @Controller @Scope("prototype") public class RoleAction extends ActionSupport implements ModelDriven<Role> { @Resource private RoleService roleService ; private Role role=new Role(); public Role getModel() { // TODO Auto-generated method stub return role; } //列表方法 public String list() throws Exception { List<Role> roleList = roleService.findAll(); ActionContext.getContext().put("roleList", roleList); return "list"; } //删除方法 public String delete() throws Exception { roleService.delete(role.getId()); return "toList"; } //增加页面方法 public String addUI() throws Exception { return "addUI"; } //增加方法 public String add() throws Exception { //为页面参数设值 // Role role=new Role(); //role.setName(role.getName()); //role.setDescription(role.getDescription()); //保存到数据库 roleService.save(role); return "toList"; } //修改页面方法 public String editUI() throws Exception { //根据id得到role对象的一条信息并显示 Role role1 = roleService.getById(role.getId()); //在edit页面显示数据 //this.name=role.getName(); //this.description=role.getDescription(); ActionContext.getContext().getValueStack().push(role1); return "editUI"; } //修改方法 public String edit() throws Exception { //设置需要修改的值 Role role2= roleService.getById(role.getId()); role2.setName(role.getName()); role2.setDescription(role.getDescription()); //update到数据库中 roleService.update(role2); return "toList"; } }
3.配置struts.xml文件,代码如下:(这里需要注意的就是转发和重定向了)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置扩展名为action --> <constant name="struts.action.extension" value="action" /> <constant name="struts.ui.theme" value="simple"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="test" class="com.icss.spring.TestAction"> <result name="success">/success.jsp</result> </action> <action name="roleAction_*" class="roleAction" method="{1}"> <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result> <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result> <result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result> <result name="toList" type="redirectAction">roleAction_list</result> </action> </package> </struts>
4.下面就跟RoleAction来写RoleService和RoleServiceImpl,因为在BaseDao中的方法包含了RoleAction的方法,直接在Service层调用BaseDao的方法即可,代码如下:
RoleService
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><pre name="code" class="java">package com.icss.oa.service; import java.util.List; import com.icss.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); }
RoleServiceImpl
package com.icss.oa.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.icss.oa.dao.RoleDao; import com.icss.oa.domain.Role; import com.icss.oa.service.RoleService; @Service @Transactional public class RoleServiceImpl implements RoleService { @Resource private RoleDao roleDao ; 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) { // TODO Auto-generated method stub return roleDao.getById(id); } public void update(Role role) { roleDao.update(role); } }
5.写出页面,实现功能,导入CSS js image jsp文件,只需要在jsp页面中循环显示,添加几个href就行了(注意引入struts2的标签,显示用的是EL和OGNL表达式,注意路径的写法):
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>岗位列表</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/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="${pageContext.request.contextPath}/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="roleAction_delete?id=%{id}" onclick="return delConfirm()">删除</s:a> <s:a action="roleAction_editUI?id=%{id}">修改</s:a> <a href="setPrivilegeUI.html">设置权限</a> </td> </tr> </s:iterator> </tbody> </table> <!-- 其他功能超链接 --> <div id="TableTail"> <div id="TableTail_inside"> <s:a action="roleAction_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> </div> </div> </div> </body> </html>
addUi.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>岗位设置</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/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="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置 </div> <div id="Title_End"></div> </div> </div> <!--显示表单内容--> <div id="MainArea"> <s:form action="roleAction_add"> <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="${pageContext.request.contextPath}/style/images/save.png"/> <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> </div> </s:form> </div> </body> </html>
editUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>岗位设置</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/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="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置 </div> <div id="Title_End"></div> </div> </div> <!--显示表单内容--> <div id="MainArea"> <s:form action="roleAction_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="${pageContext.request.contextPath}/style/images/save.png"/> <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> </div> </s:form> </div> </body> </html>
6,打开浏览器输入 http://localhost:8080/OA/roleAction_list.action得到最基本的页面:
7.最简单的Role管理功能实现了,后期还有许多需要改进的地方,明天继续再说,最后给出一些资料给大家分享,给出目前OA的工程。
http://download.csdn.net/detail/dq3wrr/7886067点击打开链接
时间: 2024-10-06 18:16:29