赛迪网 > IT技术 Java > Java大图列表
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

Struts标签之深入浅出

发布时间:2008.03.12 04:44     来源:赛迪网    作者:snowsea

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> 


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 选择正确应用服务器的七个标准 (03-10) · 用Java快速开发Linux GUI应用 (03-10)
· 一个用JAVA写的测算服务器响应速度程序 (03-10) · 如何设计出具有GUI特色的“纯”JAVA程序 (03-10)
· Java语言中列表对象的性能分析 (03-07) · 软件测试:软件测试的基础知识概要介绍 (03-07)
· 基于JDK5.0一些collection类的使用总结 (03-07) · Java开发者的十大戒律 (03-07)
· TomCat 多虚拟站点配置 (03-05) · Java入门:Java语言怎样调用外部应用程序 (03-05)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 推动产业升级 沪年内首推电子商务地方法规
· 中国域名成为全球顶级域名 促进社会信息化
· 签合同前的四问 谈八大厂商“云计算”理念
· 亚略特烟草解决方案 移民安置信息管理系统
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统