关键字: html radio框
在显示的明细一览,每一行前面加一个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该如何写?
<html: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());
通过一个隐藏变量,得到选中第几条数据,然后就能做相应处理.
Session—> 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得到.