<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 建立路由 struts.xml ``` <!-- 編輯數據 --> <action name="edit" class="teacher.Edit"> <result name="success">/jsp/teacher/edit.jsp</result> <result name="error">/jsp/error.jsp</result> </action> ``` # C層 新建 teacher.Edit ``` package teacher; import entity.Teacher; public class Edit { private int id; // 關健字 private Teacher teacher; // 教師實體 public int getId() { return id; } public void setId(int id) { this.id = id; } // teacher會被V層讀取,但并可能由V層傳入,所以,只需要get函數. public Teacher getTeacher() { return teacher; } // 該execute方法將被自動調用, 方法的返回類型必須為String public String execute() { return "success"; } } ``` # V層 > 所有的V層文件均建立在WebContent文件夾中 /jsp/teacher/edit.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>教師管理--編輯</title> </head> <body> <form action="update" method="post"> 姓名:<input type="text" name="name" /> <br /> 用戶名:<input type="text" name="username"> <br /> 性別:<select name="sex"> <option value="0">男</option> <option value="1">女</option> </select> <br /> 郵箱:<input type="text" name="email" /><br /> 密碼:<input type="password" name="password" /><br /> <button type="submit">submit</button> </form> </body> </html> ``` ## 測試 使用如下URL進行測試: [http://localhost:8080/javaee/teacher/edit?id=1](http://localhost:8080/javaee/teacher/edit?id=1) 或: [http://localhost:8080/javaee/teacher/edit?id=1](http://localhost:8080/javaee/teacher/edit?id=1) # M層 我們要編輯一個用戶的前提,要是獲取這個用戶的原始信息.我們在M層中,新建`getTeacherById(int id)`方法。 我們仍然將這個方法寫在entity.Teacher這個實體類中 ``` /** * 通過ID獲取Teacher實體 * @param id * @return */ static public Teacher getTeacherById(int id) { // 實例化Teacher Teacher teacher = new Teacher(); // 創建會話(這里的session也是會話的意思,我們以前接觸的http中的session,處理的是用戶與服務器的對話) Session session = MysqlJavaee.getCurrentSession(); // 開啟事務(使用緩沖池進行數據庫的連接) Transaction transaction = session.beginTransaction(); // 在這里,必須使用try catch finally語句。來確定會話正常關閉. // 否則,當操作數據庫產生錯誤時,你可能需要重啟mysql服務 try { // 使用Teacher.class來獲取到Teacher的類名(包括包名) teacher = (Teacher) session.get(Teacher.class, id); // 提交事務 transaction.commit(); // 捕獲異常 } catch (HibernateException e) { // 如果事務執行異常,則回滾事務 if (null != transaction) { transaction.rollback(); } // 打印異常 e.printStackTrace(); } finally { // 如果session處于開啟狀態,則關閉session if (session.isOpen()) { // 關閉會話 session.close(); } } return teacher; } ``` ## 測試 M層中的每個方法,都對應一個測試的信息 以后我們進行測試時,進行如下規定: > 每個M層中的方法,都需要在test目錄下建立同名包,同名類,同名方法。 比如,我們上面將`getTeacherById(int id)`寫在了src目錄中的entity.Teacher中。那么,我們在test目錄中,也需要建立entity.Teacher同名類,及`getTeacherById()`同名方法。 ``` package entity; import org.junit.Test; public class TestTeacher { // 用注解的方法,來說明此方法是一個可以用來做單元測試的方法 @Test public void testInit() { // 初例化 entity.Teacher teacher = new entity.Teacher(1, "張三", "zhangsan", "zhangsan@yunzhiclub.com", false, "123321"); // 打印對象信息 System.out.println(teacher.toString()); } @Test public void getTeacherById() { int id = 1; Teacher teacher = Teacher.getTeacherById(id); System.out.println(teacher.toString()); // 打印Teacher類的 類名(包括包名) System.out.println(Teacher.class); } } ``` 結果: ``` Teacher [id=1, name=張三, username=zhangsan, email=zhangsan@yunzhiclub.com, sex=true, password=123] class entity.Teacher ``` ## C與M對接 ``` // 該execute方法將被自動調用, 方法的返回類型必須為String public String execute() { // 獲取要編輯的教師 teacher = Teacher.getTeacherById(id); System.out.println(teacher.toString()); return "success"; } ``` ## V與C對接 1. 引入struts標簽 2. 顯示數據 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>教師管理--編輯</title> </head> <body> <form action="update" method="post"> <input type="hidden" name="id" value="<s:property value="teacher.id" />" /> 姓名:<input type="text" name="name" value="<s:property value="teacher.name" />"/> <br /> 用戶名:<input type="text" name="username" value="<s:property value="teacher.username" />"> <br /> 性別:<select name="sex"> <option value="0">男</option> <option value="1">女</option> </select> <br /> 郵箱:<input type="text" name="email" value="<s:property value="teacher.email" />" /><br /> 密碼:<input type="password" name="password" /><br /> <button type="submit">submit</button> </form> </body> </html> ``` ## 測試 [http://localhost:8080/javaee/teacher/edit?id=1](http://localhost:8080/javaee/teacher/edit?id=1) ![編輯](./image/4.4.1.png) ## 判斷性別 我們使用在前面應用到的<s:if>標簽來進行性別的判斷,并選中. ``` <select name="sex"> <option value="0">男</option> <option value="1" <s:if test="teacher.sex">selected</s:if>>女</option> </select> ``` 此時,再刷新網頁,如果在數據庫中,我們存的性別值為1,則"女"會自動選中。 > 本節參考官方文檔:hibernate:[#objectstate-loading) , struts:[if](https://struts.apache.org/docs/if.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看