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,对应一条记录,如何确定选中那条数据呢??
Sam
struts标签的使用
需积分: 3 158 浏览量
2008-07-10
11:25:47
上传
评论
收藏 23KB RAR 举报
song4123
- 粉丝: 4
- 资源: 75
最新资源
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
- 222222222222
- 16张相机标定图片,可复现本文畸变矫正
- dbeaver-ce-23.3.1-x86_64-setup.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈