實現一組功能的步驟:javascript
|
|
|
|
|
|
3,填空: css
多對多中的<many-to-many column="..">:通常能夠寫爲關聯對象的名稱加Id後綴。 html
將幾個JavaBean都寫好後,再一塊兒將其映射文件寫好大半,至於關聯關係能夠先寫完註釋,拷模板,將name和class屬性填好,將column屬性留至最後對照來寫. java
column屬性:先從最簡單的多對一關係開始寫(只有一行),再寫其對應的一對多… jquery
例如如下User,Role和Department: spring
User:數據庫
public class User {服務器 private Long id;網絡 private Department department;session private Set<Role> roles = new HashSet<Role>(); //用戶與角色多對多
private String loginName; // 登陸名 private String password; // 密碼 private String name; // 真實姓名 private String gender; // 性別 private String phoneNumber; // 電話號碼 private String email; // 電子郵件 private String description; // 說明 ……… } |
Role:
public class Role { private Long id; private String name; private String description;
private Set<User> users = new HashSet<User>(); //崗位和用戶多對多關係 ……. } |
Department:
public class Department { private Long id; private String name; private String description;
private Set<User> users = new HashSet<User>(); //部門和用戶一對多 private Set<Department> children= new HashSet<Department>(); //部門和下級部門一對多 private Department parent; //部門和上級部門多對一 …… } |
User.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"> <class name="User" table="itcast_user"> <id name="id"> <generator class="native"></generator> </id>
<property name="loginName" column="loginName"></property> <property name="password" column="password"></property> <property name="name" column="name"></property> <property name="gender" column="gender"></property> <property name="phoneNumber" column="phoneNumber"></property> <property name="email" column="email"></property> <property name="description" column="description"></property>
<!-- department屬性,表達的是本對象與Department的多對一關係 --> <many-to-one name="department" class="Department" column="departmentId"></many-to-one>
<!-- roles屬性,表達的是本對象與Role的多對多關係 --> <set name="roles" table="itcast_user_role"> <key column="userId"></key> <many-to-many class="Role" column="roleId"></many-to-many> </set>
</class> </hibernate-mapping> |
Role.hbm.xml:
<!-- users屬性,表達的是本對象與User的多對多關係 --> <set name="users" table="itcast_user_role"> <key column="roleId"></key> <many-to-many class="User" column="userId"></many-to-many> </set> |
Department.hbm.xml:
<!-- users屬性,表達的是本對象與User的一對多關係 --> <set name="users"> <key column="departmentId"></key> <one-to-many class="User"/> </set>
<!-- children屬性,表達的是本對象與Department(下級)的一對多 --> <set name="children"> <key column="parentId"></key> <one-to-many class="Department"/> </set>
<!-- parent屬性,表達的是本對象與Department(上級)的多對一關係 --> <many-to-one name="parent" class="Department" column="parentId"></many-to-one> |
運行TestSpring中testSessionFactory方法,測試成功則建表完畢.
TestSpring:
package cn.itcast.oa.test;
import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//測試SessionFactory @Test public void testSessionFactory() throws Exception{ SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory"); System.out.println(sf.openSession()); }
//測試事務管理 @Test public void testTx() throws Exception{ TestService testService = (TestService) ac.getBean("testService"); testService.saveTwoUsers(); }
//測試Action對象的管理 @Test public void testAction() throws Exception{ TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } } |
按照美工給的靜態頁面進行分析.再寫action以前,先設計好方法名等(最好帶有規律).
<result name="toList" type="redirectAction">/xxxAction_list</result> |
6)轉發只有一次請求,重定向包含兩次請求.
4,實現功能:
不要忘記在Action類上加註解 @Controller 和 @Scope("prototype").如:
DepartmentAction:
package cn.itcast.oa.view.action; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.itcast.oa.domain.Department; import cn.itcast.oa.service.DepartmentService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;
@Controller @Scope("prototype") public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
private Department model = new Department(); public Department getModel() { return model; } /* @Resource private DepartmentService departmentService;*/
/** * 列表 */ public String list() throws Exception { return "list"; } /** * 刪除 */ public String delete() throws Exception { return "toList"; } /** * 添加頁面 */ public String addUI() throws Exception { return "saveUI"; } /** * 添加 */ public String add() throws Exception { return "toList"; } /** * 修改頁面 */ public String editUI() throws Exception { return "saveUI"; } /** * 修改 */ public String edit() throws Exception { return "toList"; } } |
TIP:增刪改查所用的6個Action方法模板:
/** 列表 */ public String list() throws Exception { return "list"; } /** 刪除 */ public String delete() throws Exception { return "toList"; } /** 添加頁面 */ public String addUI() throws Exception { return "saveUI"; } /** 添加 */ public String add() throws Exception { return "toList"; } /** 修改頁面 */ public String editUI() throws Exception { return "saveUI"; } /** 修改 */ public String edit() throws Exception { return "toList"; } |
2, 初步建立JSP頁面(頁面上只寫些簡單東西,用於測試), 並配置struts.xml.如:
List.jsp:
SaveUI.jsp:
struts.xml:
<!-- 部門管理 --> <action name="department_*" class="departmentAction" method="{1}"> <result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result> <result name="toList" type="redirectAction">department_list</result> </action> |
Tips:class=" departmentAction"首字母是小寫, redirectAction要拼寫正確,jsp要放進WEB-INFO目錄下.
測試:啓動服務器,訪問action,觀察頁面是否成功跳轉.
在Action中注入相應Service(加上註解@Resource).
先利用Ctrl+1建立action要用到的Service接口(和其中的規範(Ctrl+1,再Alt+向左鍵 返回原來的類)),等寫完了action中的具體方法,再建立具體的ServiceImpl類,實現其中具體的功能.
在ServiceImpl類上加上註解@Service@Transactional,在ServiceImpl中注入相應Dao(加上註解@Resource).
記得在實現類上加註解@Repository.
先完成基本功能:把數據顯示出來,不用管樣式.完成基本功能的時候,要參照靜態頁面來完成,好比頁面列表上有添加和修改選項,因此就得完成這些功能.等到測試運行沒問題以後再套用靜態頁面:即將美工給的靜態頁面源碼和相關的資源文件(如等)拷過來.再仔細修改靜態頁面.如:
(部門管理有關上級部門的功能暫不實現(涉及樹狀結構))
列表頁面:
添加/修改頁面(區別是修改頁面有回顯數據):
原始代碼:
List.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%-- 顯示數據 --%> <html> <body>
<s:iterator value="departmentList"> <s:property value="id"/>, <s:property value="name"/>, <s:property value="description"/>
<%-- 使用s:a標籤,能夠幫咱們自動的加上前面的工程名稱和後面的擴展名 --%> <s:a action="department_delete?id=%{id}" onclick="return confirm('肯定要刪除嗎?')">刪除</s:a> <s:a action="department_editUI?id=%{id}">修改</s:a> <br/> </s:iterator>
<s:a action="department_addUI">添加</s:a>
</body> </html> |
迭代器的這種寫法是被容許的,不過departmentList要事先被put進集合域中.相應action中的代碼:
/** * 列表 */ public String list() throws Exception { List<Department> departmentList = departmentService.findAll(); ActionContext.getContext().put("departmentList", departmentList); return "list"; } |
SaveUI.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body>
<s:form action="department_%{id == null ? 'save' : '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>
</body> </html> |
若id有值(id是從List.jsp傳過來的),則這個表單會提交到DepartmentAction中的edit()方法,這個頁面就成了修改頁面.
而此時這個修改頁面回顯數據的功能就成了關鍵(用Struts2標籤回顯數據很方便,缺點是要本身修改樣式):
<s:textfield name="name"></s:textfield>隱藏了value="name"(不寫也會有,因此用Struts2標籤回顯數據很方便).
他會先從值棧的棧頂開始找對象的name屬性,若找不到就會從map域的key值開始找.因此若是想要更好地回顯數據,最好就要先把數據push進值棧棧頂.
對應DepartmentAction中的代碼:
/** * 修改頁面 */ public String editUI() throws Exception { //準備回顯的數據 Department department = departmentService.getById(model.getId()); ActionContext.getContext().getValueStack().push(department); return "saveUI"; } |
套用靜態頁面,即將頁面源碼複製過來後,修改頁面代碼.
在此要先將..替換成${pageContext.request.contextPath},而後再將
<script language="javascript" src="../script/DemoData.js" charset="utf-8"></script>
<script language="javascript" src="../script/DataShowManager.js" charset="utf-8"></script>
刪掉(這些是測試數據),將class改爲cssClass(Struts2標籤中,會將cssClass翻譯成class)最後再慢慢修改...
list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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="150px">部門名稱</td> <td width="150px">上級部門名稱</td> <td width="200px">職能說明</td> <td>相關操做</td> </tr> </thead>
<!--顯示數據列表--> <s:iterator value="departmentList"> <tbody id="TableData" class="dataContainer" datakey="departmentList"> <tr class="TableDetail1 template"> <td><s:property value="name"/> </td> <td> </td> <td><s:property value="description"/> </td> <td><s:a action="department_delete?id=%{id}" onclick="return confirm('肯定要刪除麼?')">刪除</s:a> <s:a action="department_editUI?id=%{id}">修改</s:a><br/> </td> </tr> </tbody> </s:iterator> </table>
<!-- 其餘功能超連接 --> <div id="TableTail"> <div id="TableTail_inside"> <s:a action="department_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> </div> </div> </div> <!--說明--> <div id="Description"> 說明:<br /> 1,列表頁面只顯示一層的(同級的)部門數據,默認顯示最頂級的部門列表。<br /> 2,點擊部門名稱,能夠查看此部門相應的下級部門列表。<br /> 3,刪除部門時,同時刪除此部門的全部下級部門。 </div> </body> </html> |
saveUI.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <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" /> </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="department_%{id == null ? 'save' : '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><select name="parentId" class="SelectStyle"> <option value="0" selected="selected">請選擇部門</option> <option value="7">┠總經理室</option> <option value="1">┠市場部</option> <option value="2"> ┠諮詢部</option> <option value="3"> ┠招生部</option> <option value="4">┠教學部</option> <option value="5">┠後勤部</option> </select> </td> </tr> <tr><td>部門名稱</td> <td><s:textfield name="name" cssClass="InputStyle"/> *</td> </tr> <tr><td>職能說明</td> <td><s:textarea name="description" cssClass="TextareaStyle"/></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> <div class="Description"> 說明:<br /> 1,上級部門的列表是有層次結構的(樹形)。<br/> 2,若是是修改:上級部門列表中不能顯示當前修改的部門及其子孫部門。由於不能選擇自已或自已的子部門做爲上級部門。<br /> </div> </body> </html> |
DepartmentAction:
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.Department; import cn.itcast.oa.service.DepartmentService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;
@Controller @Scope("prototype") public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
private Department model = new Department(); public Department getModel() { return model; }
@Resource private DepartmentService departmentService;
/** * 列表 */ public String list() throws Exception { List<Department> departmentList = departmentService.findAll(); ActionContext.getContext().put("departmentList", departmentList); return "list"; } /** * 刪除 */ public String delete() throws Exception { departmentService.delete(model.getId()); return "toList"; } /** * 添加頁面 */ public String addUI() throws Exception { return "saveUI"; } /** * 添加 */ public String save() throws Exception { departmentService.save(model); return "toList"; } /** * 修改頁面 */ public String editUI() throws Exception { //準備回顯的數據 Department department = departmentService.getById(model.getId()); ActionContext.getContext().getValueStack().push(department); return "saveUI"; } /** * 修改 */ public String edit() throws Exception { //獲取要修改的數據庫中的原始數據 Department department = departmentService.getById(model.getId()); //修改 department.setName(model.getName()); department.setDescription(model.getDescription()); //修改數據庫中的數據 departmentService.update(department); return "toList"; } } |
經常使用快捷鍵:
Alt + 方向鍵向左鍵 跳回剛剛打開的類
Alt + Shift + A 集體編輯?
Ctr + o 按一次,顯示當前類包含的的全部字段,方法等, 再按一次會附加顯示繼承過來的全部字段,方法等.
Ctrl + Shift +x 轉成大寫(先選中)
Alt + / 代碼提示
Shift + Enter 至關於先按End在按回車,即在當前行下面另起一個新行。
Ctrl + Shift + F 格式化代碼
Ctrl + Shift + O 自動導入包
Ctrl + / 註釋/取消註釋
Ctrl + Shift + T 打開類或接口的文件
Ctrl + D 刪除整行
Ctrl + 1 快速修復錯誤
Alt + ↑ 當前行的代碼移動到上面去
Alt + ↓ 當前行的代碼移動到下面去
Ctrl + Alt + ↓ 複製當前行到下面
Ctrl + F11 直接運行
F11 Debug模式運行
Ctrl + Shift + X 轉爲大寫
Ctrl + Shift + Y 轉爲小寫