<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                在以往的web開發中,如果要在表單顯示什么內容,我們就需要在Action中提前定義好表單顯示的所有屬性,以及一系列的get和set方法。如果實體類的屬性非常多,那么Action中也要定義相同的屬性。在Struts2中,ModelDriven模型驅動就提供了另一種方式,減少了重復性的代碼。 下面我們就來具體看看在項目中如何使用: 以用戶管理為例,除jsp頁面外,四個包action、dao、service、vo。 調用關系:action——>service——>dao ![](https://box.kancloud.cn/2016-04-27_57206b05c4636.jpg) 首先,無論是哪種方法,vo包中的User類都是一樣的。 ~~~ public class User { //用戶id private Integer uid; //用戶名 private String username; //密碼 private String password; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ~~~ 在Action中,如果不使用模型驅動,我們需要一一構造User對象的屬性,作為參數傳入調用。 使用模型驅動,就要實現ModelDriven這個接口,內部方法也很簡單。 ~~~ public abstract interface com.opensymphony.xwork2.ModelDriven { public abstract java.lang.Object getModel(); } ~~~ ~~~ public class UserAction { private int uid; private String username; private String password; public String login(){ //構造user對象 User user = new User(); user.setUId(id); user.setUsername(username); user.setPassword(password); User exitUser=userService.login(user); if(exitUser==null){ this.addActionError("登錄失敗"); return "login"; }else{ //用戶信息存入session、 ServletActionContext.getRequest().getSession().setAttribute("exitUser",exitUser); return "loginSuccess"; } } public int getUId() { return uid; } public void setId(int uid) { this.uid = uid; } //剩下的get和set方法省去 ...... ...... } ~~~ 而如果使用模型驅動,我們只需要讓Action類去實現ModelDriven接口。 ~~~ public class UserAction extends ActionSupport implements ModelDriven<User> { //模型驅動使用的對象 private User user=new User(); //MdoelDriven中的方法 public User getModel(){ return user; } //注入userservice private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } /** * 登錄 */ public String login(){ User exitUser=userService.login(user); if(exitUser==null){ this.addActionError("登錄失敗"); return "login"; }else{ //用戶信息存入session、 ServletActionContext.getRequest().getSession().setAttribute("exitUser",exitUser); return "loginSuccess"; } } } ~~~ 另外,模型驅動的好處還體現在**數據回顯**時的應用。 在Action中,模型驅動的實體是CategorySecond,因而在回顯jsp頁面數據的時候,不需要保存到ValueStack值棧中,而一級分類集合cList需要保存到值棧中,前臺才可以獲取到值。 ~~~ public class AdminCategorySecondAction extends ActionSupport implements ModelDriven<CategorySecond>{ // // //其余代碼省去 //編輯二級分類 public String edit(){ //查詢二級分類 categorySecond=categorySecondService.findByCid(categorySecond.getCsid()); //查詢一級分類 List<Category> cList=categoryService.findAll(); //保存到值棧中 ActionContext.getContext().getValueStack().set("cList", cList); return "editSuccess"; } } ~~~ 前臺的部分顯示如下: ~~~ <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 二級分類名稱: </td> <td class="ta_01" bgColor="#ffffff" > <input type="text" name="csname" value="<s:property value="model.csname"/>" id="userAction_save_do_logonName" class="bg"/> </td> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 所屬的一級分類: </td> <td class="ta_01" bgColor="#ffffff" > <select name="category.cid"> <s:iterator var="c" value="cList"> <option value="<s:property value="#c.cid"/>" <s:if test="#c.cid==model.category.cid">selected</s:if>><s:property value="#c.cname"/></option> </s:iterator> </select> </td> </tr> ~~~ 二級分類信息使用model來顯示數據,而一級分類信息接收action中傳來的cList。兩種方法都可以實現數據顯示,但模型驅動更為簡單方便。 小結: Struts2提供的兩種方式:屬性驅動和模型驅動。模型驅動可以提高代碼的重用性,使得操作簡便。但在小型項目中,表單比較少時,并不提倡使用模型驅動。還有一點,在上面的例子中也能看出,模型驅動只能對應一個對象,當表單數據來源比較復雜時,使用模型驅動也無法起到十分明顯的作用。
                  <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>

                              哎呀哎呀视频在线观看