Action和jsp的開發其實就是對Struts標簽的運用.掌握標簽的熟練程度決定了開發效率.初學者往往對某個數據表示或數據獲取,束手無策.一個簡單的問題浪費一兩天時間也就不足為怪了.導致整個開發進度延后.外面的struts書籍介紹標簽和數據傳輸原理都比較簡單,下面我對標簽技術和數據傳輸原理,進行全方位多角度的剖析.希望對各位有所幫助.以此為模版,將大大提高開發效率.以sample為機能名稱.
①畫面上有一text框,顯現內容為某一數據表中的某一字段.那我們該如何設置和得到此數據呢?
SampleJsp:
<html:text name = "sampleForm" property="name" />
SampleForm.java: // form文件名必須和jsp中標簽的name對應
String name; // 必須和jsp中該項目的property一樣
public String getName() { return name; }
public void setName(String name) { this.name = name;}
變量和方法名,不可以順意.變量abcd,那方法名就是setAbcd和getAbcd.注意大小寫.
jsp中的項目必然全部在form里面有所表示,當然反過來,form里的項目在jsp中不一定全部表示(可能有輔助動作的對象或驗證)
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對應,action操作form,form其實起了傳輸數據的作用.這就是struts標簽的核心原理.得到數據和設置數據沒問題了,剩下的工作也就得心應手了.
②再看一個處理標簽的方法.畫面上是一個明細一覽表示(表).表示的是數據表user的相關數據(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為空(無數據或null),下面的東東不顯示.
logic:iterate是邏輯循環,userList有幾條數據,就循環幾次.
<bean:write name="user" property="id" />是lable標簽,顯示user這個對象(entity)的id屬性.或者說顯示數據表user中的一條記錄中的id這個列.
User.java(就是entity,因為和業務密切,高達不開發,切記切記不可順意修改.遇到設計有問題,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一樣,但有點不一樣,不一樣在哪里,看下去后,自己感悟吧.
SampleForm.java:
List userList;
public List getUserList () { return userList; }
public void setUserList (List userList) { this.userList = userList; }
form只要這些,那你會問,id和name,struts如何能得到呢?你不是說過jsp必須和form一樣對應嗎?不錯,一一對應是肯定的. UserList信息已經包含了一切,還需要定義id和name嗎?至于struts如何得到數據,那就看下面的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
}
一切搞定.是不是很簡單,但估計你還是有點暈.你還是想問我,id和name到底是如何設置的?
Action設置了userList就夠了,它包含夠多的信息了. struts看見了你設置了userList.它就知道了這個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" 對應了,明白啥意思嗎?.就象循環指明索引一樣. property="id"就是要顯示的這個索引對應的內容.Struts就是這樣來認id和name的.
③接下來,看一個加強版的table例子,在顯示的明細一覽,每一行前面加一個radio框,讓用戶選擇哪個user.進行刪除操作.
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值對應form里的對象.value值是該行radio對應的user中的id(數據表中user的id是主鍵),那么當用戶選中任何一個radio,struts通過form得到propertys值,就可以得到選中哪個user了,然后進行相應操作.
設置哪個user被選中,一是通過用戶選擇,沒的說.二,通過程序控制,如果進入初期畫面,我要讓user.id = ‘3’的radio被選中,只要在初期Action中form.selectedUserId(“3”);一切搞定,就一句話,進入初期畫面時, user.id = ‘3’的radio被選中了.
注意以下標簽
<html:radio name="sampleForm" property="selectedUserId" value="<%= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
下面發揮想象一下以下標簽啥意思?
<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屬性.
看出來了嗎?靈活運用,想象一下,各個entity和form,action該如何寫?
④接著介紹一下,checkbox是使用.畫面有一排checkbox,如何設置和得到數據呢?先看一個簡單點的.
<html:checkbox name=" sampleForm" property="chechbox1" value="true" />
<html:checkbox name=" sampleForm" property="chechbox2" value="false" />
<html:checkbox name=" sampleForm" property="chechbox3" value="true" />
第二個框未選中,其他選中.form里面對應三個String chechbox1,chechbox2, chechbox3;下面來個復雜點的,多選擇對話框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;}
如果我們在初期時在action里對bean賦值:
userList = { User(”1”,”name1”), User(”2”, ”name2”), User(”3”,”name3”) }
selectedUsers = {“1”,”3”}
那畫面選中第一第三個選擇框.
用戶修改選擇框,選擇了第二,第三個,那么在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表,每條數據前面有個button,對應一條記錄,如何確定選中那條數據呢??
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());
通過一個隱藏變量,得到選中第幾條數據,然后就能做相應處理.
⑥上面都是通過form和jsp傳輸數據的.還有session也能讓jsp顯示數據.但如果我做為設計者,是不提倡這樣做的.為什么就不說了.但日本以前的設計很可能會用到session和jsp傳數據.那我就有必要講一下如何用了?做為高達的設計者還是盡量不要用session和jsp溝通.
有個下拉列表框,里面顯示所有用戶名稱.用session傳數據.
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里只要一個selectedUser,表示選擇的user. 下拉列表框用session表示.
在action等地方設置了session的內容,那下拉列表框就能顯示內容了.這里session名為AllUser, labelProperty="name"是下拉列表框顯示的東東, property="id"是下拉列表框每條數據隱藏的東東.通過property="selectedUser"里得到選中那條數據
<html:text name="sampleForm" property="name"
value="<%= (FwThreadContext.getAttribute("UserName")).toString() %>" />
這里很簡單就是把session名為UserName設置到Text框中.得的時候還是通過form中的name得到.
標簽寶典:
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, 循環邏輯
<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>