随笔-199  评论-203  文章-11  trackbacks-0
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>
posted on 2009-04-23 08:03 Werther 阅读(1625) 评论(1)  编辑  收藏 所属分类: 20.Struts

评论:
# re: struts 标签详解 2009-04-23 13:31 | 000
1.*的啊。我还以为2.0的呢,失望....  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: