0000337: struts標(biāo)簽之淺入深出 OBS
原創(chuàng)
fy198392
2005-12-23 16:42:54
查看評論 Action和jsp的開發(fā)其實(shí)就是對Struts標(biāo)簽的運(yùn)用.掌握標(biāo)簽的熟練程度決定了開發(fā)效率.初學(xué)者往往對某個(gè)數(shù)據(jù)表示或數(shù)據(jù)獲取,束手無策.一個(gè)簡單的問題浪費(fèi)一兩天時(shí)間也就不足為怪了.導(dǎo)致整個(gè)開發(fā)進(jìn)度延后.外面的struts書籍介紹標(biāo)簽和數(shù)據(jù)傳輸原理都比較簡單,下面我對標(biāo)簽技術(shù)和數(shù)據(jù)傳輸原理,進(jìn)行全方位多角度的剖析.希望對各位有所幫助.以此為模版,將大大提高開發(fā)效率.以sample為機(jī)能名稱.
①畫面上有一text框,顯現(xiàn)內(nèi)容為某一數(shù)據(jù)表中的某一字段.那我們該如何設(shè)置和得到此數(shù)據(jù)呢?
SampleJsp:
<html:text name = "sampleForm" property="name" />
SampleForm.java: // form文件名必須和jsp中標(biāo)簽的name對應(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)然反過來,form里的項(xiàng)目在jsp中不一定全部表示(可能有輔助動作的對象或驗(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對應(yīng),action操作form,form其實(shí)起了傳輸數(shù)據(jù)的作用.這就是struts標(biāo)簽的核心原理.得到數(shù)據(jù)和設(shè)置數(shù)據(jù)沒問題了,剩下的工作也就得心應(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為空(無數(shù)據(jù)或null),下面的東東不顯示.
logic:iterate是邏輯循環(huán),userList有幾條數(shù)據(jù),就循環(huán)幾次.
<bean:write name="user" property="id" />是lable標(biāo)簽,顯示user這個(gè)對象(entity)的id屬性.或者說顯示數(shù)據(jù)表user中的一條記錄中的id這個(gè)列.
User.java(就是entity,因?yàn)楹蜆I(yè)務(wù)密切,高達(dá)不開發(fā),切記切記不可順意修改.遇到設(shè)計(jì)有問題,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; }
看到這,是否覺得面熟啊,好象和FORM一樣,但有點(diǎn)不一樣,不一樣在哪里,看下去后,自己感悟吧.
SampleForm.java:
List userList;
public List getUserList () { return userList; }
public void setUserList (List userList) { this.userList = userList; }
form只要這些,那你會問,id和name,struts如何能得到呢?你不是說過jsp必須和form一樣對應(yīng)嗎?不錯(cuò),一一對應(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
}
一切搞定.是不是很簡單,但估計(jì)你還是有點(diǎn)暈.你還是想問我,id和name到底是如何設(shè)置的?
Action設(shè)置了userList就夠了,它包含夠多的信息了. struts看見了你設(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" 對應(yīng)了,明白啥意思嗎?.就象循環(huán)指明索引一樣. property="id"就是要顯示的這個(gè)索引對應(yīng)的內(nèi)容.Struts就是這樣來認(rèn)id和name的.
③接下來,看一個(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值對應(yīng)form里的對象.value值是該行radio對應(yīng)的user中的id(數(shù)據(jù)表中user的id是主鍵),那么當(dāng)用戶選中任何一個(gè)radio,struts通過form得到propertys值,就可以得到選中哪個(gè)user了,然后進(jìn)行相應(yīng)操作.
設(shè)置哪個(gè)user被選中,一是通過用戶選擇,沒的說.二,通過程序控制,如果進(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() %>" />
能看出來什么?
User包含object1,object2包含object3,….objectN-1包含objectN,objectN有id屬性.
看出來了嗎?靈活運(yùn)用,想象一下,各個(gè)entity和form,action該如何寫?
④接著介紹一下,checkbox是使用.畫面有一排checkbox,如何設(shè)置和得到數(shù)據(jù)呢?先看一個(gè)簡單點(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里面對應(yīng)三個(gè)String chechbox1,chechbox2, chechbox3;下面來個(gè)復(fù)雜點(diǎn)的,多選擇對話框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;}
如果我們在初期時(shí)在action里對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,對應(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());
通過一個(gè)隱藏變量,得到選中第幾條數(shù)據(jù),然后就能做相應(yīng)處理.
⑥上面都是通過form和jsp傳輸數(shù)據(jù)的.還有session也能讓jsp顯示數(shù)據(jù).但如果我做為設(shè)計(jì)者,是不提倡這樣做的.為什么就不說了.但日本以前的設(shè)計(jì)很可能會用到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ù)隱藏的東東.通過property="selectedUser"里得到選中那條數(shù)據(jù)
<html:text name="sampleForm" property="name"
value="<%= (FwThreadContext.getAttribute("UserName")).toString() %>" />
這里很簡單就是把session名為UserName設(shè)置到Text框中.得的時(shí)候還是通過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>
標(biāo)簽:
struts標(biāo)簽之淺入深出[發(fā)起辯論]