隨筆-199  評(píng)論-203  文章-11  trackbacks-0
          Action和jsp的開(kāi)發(fā)其實(shí)就是對(duì)Struts標(biāo)簽的運(yùn)用.掌握標(biāo)簽的熟練程度決定了開(kāi)發(fā)效率.初學(xué)者往往對(duì)某個(gè)數(shù)據(jù)表示或數(shù)據(jù)獲取,束手無(wú)策.一個(gè)簡(jiǎn)單的問(wèn)題浪費(fèi)一兩天時(shí)間也就不足為怪了.導(dǎo)致整個(gè)開(kāi)發(fā)進(jìn)度延后.外面的struts書籍介紹標(biāo)簽和數(shù)據(jù)傳輸原理都比較簡(jiǎn)單,下面我對(duì)標(biāo)簽技術(shù)和數(shù)據(jù)傳輸原理,進(jìn)行全方位多角度的剖析.希望對(duì)各位有所幫助.以此為模版,將大大提高開(kāi)發(fā)效率.以sample為機(jī)能名稱.
          ①畫面上有一text框,顯現(xiàn)內(nèi)容為某一數(shù)據(jù)表中的某一字段.那我們?cè)撊绾卧O(shè)置和得到此數(shù)據(jù)呢?
          SampleJsp:
          <html:text name = "sampleForm" property="name" />
          SampleForm.java: // form文件名必須和jsp中標(biāo)簽的name對(duì)應(yīng)
          String name; // 必須和jsp中該項(xiàng)目的property一樣
          public String getName() { return name; }
          public void setName(String name) { this.name = name;}
          變量和方法名,不可以順意.變量abcd,那方法名就是setAbcd和getAbcd.注意大小寫.
          jsp中的項(xiàng)目必然全部在form里面有所表示,當(dāng)然反過(guò)來(lái),form里的項(xiàng)目在jsp中不一定全部表示(可能有輔助動(dòng)作的對(duì)象或驗(yàn)證)
          SampleAction.java
          public ActionForward start(ActionMapping mapping,
          ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
          throws Exception {
          SampleForm form = (SampleForm) argForm;
          String name = ………………other codes for get name from db
          // set name
          form.setName(name);
          // now text will show the name
          }
          public ActionForward save(ActionMapping mapping,
          ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
          throws Exception {
          SampleForm form = (SampleForm) argForm;
          // get name
          String name = form.getName();
          ………………other codes for save name
          }
          jsp和form對(duì)應(yīng),action操作form,form其實(shí)起了傳輸數(shù)據(jù)的作用.這就是struts標(biāo)簽的核心原理.得到數(shù)據(jù)和設(shè)置數(shù)據(jù)沒(méi)問(wèn)題了,剩下的工作也就得心應(yīng)手了.

          ②再看一個(gè)處理標(biāo)簽的方法.畫面上是一個(gè)明細(xì)一覽表示(表).表示的是數(shù)據(jù)表user的相關(guān)數(shù)據(jù)(id,name).
          SampleJsp:
          <logic:present name="sampleForm" property="userList" >
          <logic:iterate id="user" name=" sampleForm " property="userList">
          <tr>
          <td><bean:write name="user" property="id" /></td>
          <td><bean:write name="user" property="name" /></td>
          </tr>
          </logic:iterate>
          </logic:present>

          logic:present是邏輯判斷,sampleForm中userList為空(無(wú)數(shù)據(jù)或null),下面的東東不顯示.
          logic:iterate是邏輯循環(huán),userList有幾條數(shù)據(jù),就循環(huán)幾次.

          <bean:write name="user" property="id" />是lable標(biāo)簽,顯示user這個(gè)對(duì)象(entity)的id屬性.或者說(shuō)顯示數(shù)據(jù)表user中的一條記錄中的id這個(gè)列.
          User.java(就是entity,因?yàn)楹蜆I(yè)務(wù)密切,高達(dá)不開(kāi)發(fā),切記切記不可順意修改.遇到設(shè)計(jì)有問(wèn)題,QA日本)
          String id;
          public String getId() { return id; }
          public void setId(String id) { this.id = id; }
          String name;
          public String getName () { return name; }
          public void setName (String name) { this.name = name; }
          看到這,是否覺(jué)得面熟啊,好象和FORM一樣,但有點(diǎn)不一樣,不一樣在哪里,看下去后,自己感悟吧.
          SampleForm.java:
          List userList;
          public List getUserList () { return userList; }
          public void setUserList (List userList) { this.userList = userList; }
          form只要這些,那你會(huì)問(wèn),id和name,struts如何能得到呢?你不是說(shuō)過(guò)jsp必須和form一樣對(duì)應(yīng)嗎?不錯(cuò),一一對(duì)應(yīng)是肯定的. UserList信息已經(jīng)包含了一切,還需要定義id和name嗎?至于struts如何得到數(shù)據(jù),那就看下面的action是如何處理的吧.
          SampleAction.java
          public ActionForward start(ActionMapping mapping,
          ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
          throws Exception {
          SampleForm form = (SampleForm) argForm;
          ArrayList userList = new ArrayList();
          User user = new User();
          user.setId(1);
          user.setName(“name1”);
          userList.add(user);

          User user = new User();
          user.setId(2);
          user.setName(“name2”);
          userList.add(user);

          // set userList
          form.setUserList(userList);
          // now table will show
          }
          一切搞定.是不是很簡(jiǎn)單,但估計(jì)你還是有點(diǎn)暈.你還是想問(wèn)我,id和name到底是如何設(shè)置的?
          Action設(shè)置了userList就夠了,它包含夠多的信息了. struts看見(jiàn)了你設(shè)置了userList.它就知道了這個(gè)list里面都user(entity),useruser(entity)里面不是有很多get,set方法嗎?

          再看下下面的東東.
          <logic:iterate id="user" name=" sampleForm " property="userList">
          <bean:write name="user" property="id" />
          id=”user”,和name="user" 對(duì)應(yīng)了,明白啥意思嗎?.就象循環(huán)指明索引一樣. property="id"就是要顯示的這個(gè)索引對(duì)應(yīng)的內(nèi)容.Struts就是這樣來(lái)認(rèn)id和name的.

          ③接下來(lái),看一個(gè)加強(qiáng)版的table例子,在顯示的明細(xì)一覽,每一行前面加一個(gè)radio框,讓用戶選擇哪個(gè)user.進(jìn)行刪除操作.
          SampleJsp:
          <logic:present name="sampleForm" property="userList" >
          <logic:iterate id="user" name=" sampleForm " property="userList">
          <tr>
          <td>
          <html:radio name="sampleForm" property="selectedUserId" value="/<%=((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
          </td>
          <td><bean:write name="user" property="id" /></td>
          <td><bean:write name="user" property="name" /></td>
          </tr>
          </logic:iterate>
          </logic:present>

          sampleForm.java:
          String selectedUserId;
          public String getSelectedUserId () { return selectedUserId; }
          public void setSelectedUserId(String selectedUserId) {
          this.selectedUserId = selectedUserId;
          }
          SampleAction.java
          public ActionForward delete(ActionMapping mapping,
          ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
          throws Exception {
          SampleForm form = (SampleForm) argForm;
          String selectedUserId = form.getSelectedUserId();
          // get user by selected id
          User user = getUser(selectedUserId);
          // delete user
          }
          radio框. propertys值對(duì)應(yīng)form里的對(duì)象.value值是該行radio對(duì)應(yīng)的user中的id(數(shù)據(jù)表中user的id是主鍵),那么當(dāng)用戶選中任何一個(gè)radio,struts通過(guò)form得到propertys值,就可以得到選中哪個(gè)user了,然后進(jìn)行相應(yīng)操作.
          設(shè)置哪個(gè)user被選中,一是通過(guò)用戶選擇,沒(méi)的說(shuō).二,通過(guò)程序控制,如果進(jìn)入初期畫面,我要讓user.id = ‘3’的radio被選中,只要在初期Action中form.selectedUserId(“3”);一切搞定,就一句話,進(jìn)入初期畫面時(shí), user.id = ‘3’的radio被選中了.

          注意以下標(biāo)簽
          <html:radio name="sampleForm" property="selectedUserId" value="<%= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
          下面發(fā)揮想象一下以下標(biāo)簽啥意思?
          <html:radio name="sampleForm" property="selectedUserId" value="<%= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getObject1().getObject1().getObject2()…………getObjectN().getId().toString() %>" />
          能看出來(lái)什么?
          User包含object1,object2包含object3,….objectN-1包含objectN,objectN有id屬性.
          看出來(lái)了嗎?靈活運(yùn)用,想象一下,各個(gè)entity和form,action該如何寫?

          ④接著介紹一下,checkbox是使用.畫面有一排checkbox,如何設(shè)置和得到數(shù)據(jù)呢?先看一個(gè)簡(jiǎn)單點(diǎn)的.
          <html:checkbox name=" sampleForm" property="chechbox1" value="true" />
          <html:checkbox name=" sampleForm" property="chechbox2" value="false" />
          <html:checkbox name=" sampleForm" property="chechbox3" value="true" />
          第二個(gè)框未選中,其他選中.form里面對(duì)應(yīng)三個(gè)String chechbox1,chechbox2, chechbox3;下面來(lái)個(gè)復(fù)雜點(diǎn)的,多選擇對(duì)話框multibox
          SampleJsp中:
          <logic:iterate name = "sampleForm" id="user" property="userList">
          <html:multibox property="selectedUsers">
          <bean:write name="user" property="id"/>
          </html:multibox>
          <bean:write name="user" property="name"/>
          </logic:iterate>

          SampleForm中:
          private String userList[] = new String[0];
          public String[] getUserList () { return userList;}
          public void setUserList(String[]userList) {this.userList = userList;}

          private String selectedUsers[] = new String[0];
          public String[] getSelectedUsers () {return selectedUsers;}
          public void setSelectedUsers (String[]selectedUsers) {this.selectedUsers = selectedUsers;}

          如果我們?cè)诔跗跁r(shí)在action里對(duì)bean賦值:
          userList = { User(”1”,”name1”), User(”2”, ”name2”), User(”3”,”name3”) }
          selectedUsers = {“1”,”3”}
          那畫面選中第一第三個(gè)選擇框.

          用戶修改選擇框,選擇了第二,第三個(gè),那么在action里取bean的值
          String selectedItems[] = new String[list.getSize()];
          selectedItems = form.getSelectedItems();
          for ( int i = 0 ; i < selectedItems.length ; ++i ){
          LOGGER.debug( "selected " + i + ": " + selectedItems[i]);
          }
          Selected 0 : 2
          Selected 1 : 3
          selectedUsers = {“2”,”3”}

          ⑤畫面上有一user表,每條數(shù)據(jù)前面有個(gè)button,對(duì)應(yīng)一條記錄,如何確定選中那條數(shù)據(jù)呢??
          SampleJsp:
          <logic:iterate id="user" indexId="buttonIndex" name="sampleForm" property="userList">
          <tr>
          <td>
          <html:submit property="button" indexed='false' >
          <bean:message key="label.button.selectUser"/>
          </td>
          <td><bean:write name="user" property="id" /></td>
          <td><bean:write name="user" property="name" /></td>
          </tr>
          <html:hidden name="sampleForm" property="selectUserIndex" value='<%= "" + buttonIndex %>'/>
          </logic:iterate>

          SampleAction.java
          int index = Integer.parseInt(form.getSelectUserIndex());
          通過(guò)一個(gè)隱藏變量,得到選中第幾條數(shù)據(jù),然后就能做相應(yīng)處理.

          ⑥上面都是通過(guò)form和jsp傳輸數(shù)據(jù)的.還有session也能讓jsp顯示數(shù)據(jù).但如果我做為設(shè)計(jì)者,是不提倡這樣做的.為什么就不說(shuō)了.但日本以前的設(shè)計(jì)很可能會(huì)用到session和jsp傳數(shù)據(jù).那我就有必要講一下如何用了?做為高達(dá)的設(shè)計(jì)者還是盡量不要用session和jsp溝通.
          有個(gè)下拉列表框,里面顯示所有用戶名稱.用session傳數(shù)據(jù).
          SampleJsp:
          <%pageContext.setAttribute("userList",(List) (FwThreadContext
          .getAttribute("AllUser")));
          %>
          <html:select property="selectedUser">
          <html:options collection="userList" property="id" labelProperty="name" />
          </html:select>

          SampleForm.java:
          String selectedUser;
          Form里只要一個(gè)selectedUser,表示選擇的user. 下拉列表框用session表示.
          在action等地方設(shè)置了session的內(nèi)容,那下拉列表框就能顯示內(nèi)容了.這里session名為AllUser, labelProperty="name"是下拉列表框顯示的東東, property="id"是下拉列表框每條數(shù)據(jù)隱藏的東東.通過(guò)property="selectedUser"里得到選中那條數(shù)據(jù)

          <html:text name="sampleForm" property="name"
          value="<%= (FwThreadContext.getAttribute("UserName")).toString() %>" />
          這里很簡(jiǎn)單就是把session名為UserName設(shè)置到Text框中.得的時(shí)候還是通過(guò)form中的name得到.


          標(biāo)簽寶典:
          1,lable
          <bean:write name="sampleForm" property="name" />
          2,text
          <html:text name="sampleForm " property="name" />
          3,button
          <html:submit property="button">
          <bean:message key="label.button.save" />
          </html:submit>
          <html:button property="button" onclick="javascript:openCalendar(date);">
          <bean:message key="label.button.date" />
          </html:button>
          4,select
          <html:select property="selectedUser">
          <html:options name="sampleForm" collection="userList" property="id" labelProperty="name" />
          </html:select>
          5,checkbox,multibox
          <html:checkbox name="sampleForm" property="chechbox1" value="true" />

          <logic:iterate name = "sampleForm" id="user" property="userList">
          <html:multibox property="selectedUsers">
          <bean:write name="user" property="id"/>
          </html:multibox>
          <bean:write name="user" property="name"/>
          </logic:iterate>

          6, 循環(huán)邏輯
          <logic:present name="sampleForm" property="userList" >
          <logic:iterate id="user" name=" sampleForm " property="userList">
          <tr>
          <td>
          <html:radio name="sampleForm" property="selectedUserId" value="<%= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
          </td>
          <td><bean:write name="user" property="id" /></td>
          <td><bean:write name="user" property="name" /></td>
          </tr>
          </logic:iterate>
          </logic:present>

          7,if邏輯
          <logic:equal name=" sampleForm " property="showAllFlg" value="true" >
          <html:submit property="button">
          <bean:message key="label.button.all"/>
          </html:submit>
          </logic:equal>
          <logic:equal name=" sampleForm " property=" showAllFlg " value="false" >
          <html:submit property="button">
          <bean:message key="label.button.noall"/>
          </html:submit>
          </logic:equal>
          posted on 2009-04-23 08:03 Werther 閱讀(1645) 評(píng)論(1)  編輯  收藏 所屬分類: 20.Struts

          評(píng)論:
          # re: struts 標(biāo)簽詳解 2009-04-23 13:31 | 000
          1.*的啊。我還以為2.0的呢,失望....  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 廉江市| 揭阳市| 孟州市| 麦盖提县| 营口市| 甘谷县| 临江市| 鄢陵县| 大荔县| 镇沅| 营口市| 榆树市| 讷河市| 博客| 二手房| 阜新| 汉阴县| 荔浦县| 枞阳县| 连山| 德令哈市| 丹巴县| 包头市| 永宁县| 乳山市| 宜兰县| 天等县| 高平市| 吴川市| 桃园县| 广丰县| 那曲县| 遂昌县| 大新县| 隆子县| 河津市| 绥棱县| 嘉祥县| 会东县| 天等县| 中西区|