论坛首页 Java企业应用论坛

Struts2几个常用标签的主要属性及示例(一)

浏览 7995 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-03-26   最后修改:2010-03-29

     以前做过一个struts2的项目,总结了用到的几个struts2常用标签的用法,以及响应的示例代码,每个标签总结了一下主要属性,页面代码,后台代码以及生成的html代码

 

1. checkbox

Checkbox tag用来生成html的一个input元素,类型为checkbox。这个标签常用来表示布尔型的变量。

 

Attributes

Description

name

对应action中的属性名称

fieldValue

选中时action变量被赋的值,当action的字段是布尔型的时候可以省略这个属性

value

只能为true或者false,来决定生成的checkbox是否被选中,尽量不用这个属性

label

Jsp页面为checkbox设定的显示内容

 

Jsp code:

<s:checkbox name="projectClosed" value="true" fieldValue="yes"/>

 

Page source code:

<input type="checkbox" name="projectClosed" value="yes" checked="checked" 

id="testTags_projectClosed"/>

<input type="hidden" name="__checkbox_projectClosed" value="yes" />

 

Action class:

private String projectClosed; //with getter/setter method

 

Result:

projectClosed = "yes";

checkbox如果显示在结果列表的一列,需要用checkbox的标签结合iterator标签,这时checkboxfieldValue属性用变量赋值。在action里要声明一个字符串型数组来取得这个checkbox group的值

 

2. checkboxlist

checkboxlist标签用来生成一组checkbox,这个标签不适合用在result table中。

 

Attributes

Description

name

对应action中的属性名称,属性的类型即是listKey的值所对应的类型

list

Required,这个属性的值必须为可迭代类型或者是数组类型,比如listsetarray。用以显示所有的选择项。如果list的值为Map,则mapkey对应option的值,mapvalue对应option的显示内容

listKey

生成的checkbox input元素的value属性

listValue

生成的checkboxinput元素的显示内容

 

Jsp code:

<s:checkboxlist list="#session.hobbyList" name="hobbyIds" listKey="hobbyId

listValue="hobbyName" ></s:checkboxlist>

 

Page source code:

<input type="checkbox" name="hobbyIds" value="1" id="hobbyIds-1" checked="checked"/>

<label for="hobbyIds-1" class="checkboxLabel">Football</label>

<input type="checkbox" name="hobbyIds" value="2" id="hobbyIds-2"/>

<label for="hobbyIds-2" class="checkboxLabel">Basketball</label> 

 

Prepare action

如果要显示的内容是从db中取得,那么list属性要在页面load之前赋值,这就需要有一个单独的action来取数据并给list属性赋值,下面是个演示:

public String execute() throws Exception {

        List<Hobby> hobbyList = new ArrayList<Hobby>();

        Hobby hobby_1 = new Hobby(new Long(1), "Football");

        Hobby hobby_2 = new Hobby(new Long(2), "Basketball");

 

        hobbyList.add(hobby_1);

        hobbyList.add(hobby_2); //this list load from database

 

        map.put("hobbyList", hobbyList);

        return SUCCESS; //return xxx.jsp

    }

 

Action class:

private Long[] hobbyIds; //with getter/setter method

public String execute() throws Exception {

for (Long hobbyId : hobbyIds) {

            //TODO:this checkbox is checked, and its value available

        }

}

 

Result:

这样在action里面就可以通过Long型的数组hobbyIds 来反映有哪些checkboxchecked状态,并做相应的操作。

3. combobox

combobox标签的用法类似checkboxlist,该标签生成的render代码显示为一个input元素以及一个select元素

 

Attributes

Description

name

对应action中的属性名称,属性的类型即是listKey的值所对应的类型,这个属性的值也即是生成的text fieldname的值。

list

Required,这个属性的值必须为可迭代类型或者是数组类型,比如listsetarray,用以显示所有的选择项。如果list的值为Map,则mapkey对应option的值,mapvalue对应option的显示内容

listKey

生成的select option元素的value属性,也即是选择某个option后,text field显示的内容

listValue

生成的selectoption元素的显示内容

headerKey

设置第一个选项的值

headerValue

设置第一个选项的显示内容

 

Jsp code:

<s:combobox list="#session.hobbyList" name="comboHobbyId" headerKey="-1" headerValue="--Please Select One--" listKey="hobbyId" listValue="hobbyName"></s:combobox>

 

Page source code:

<input type="text" name="comboHobbyId" value="" id="testTags_comboHobbyIds"/><br />

<select onChange="autoPopulate_testTags_comboHobbyIds(this);">

<option value="-1">--Please Select One--</option>

     <option value="1">Football</option>

     <option value="2">Basketball</option>

</select> 

 

Prepare action

如果要显示的内容是从db中取得,那么list属性要在页面load之前赋值,这就需要有一个单独的action来取数据并给list属性赋值:

public String execute() throws Exception {

        List<Hobby> hobbyList = new ArrayList<Hobby>();

        Hobby hobby_1 = new Hobby(new Long(1), "Football");

        Hobby hobby_2 = new Hobby(new Long(2), "Basketball");

 

        hobbyList.add(hobby_1);

        hobbyList.add(hobby_2); //this list load from database

 

        map.put("hobbyList", hobbyList);

        return SUCCESS; //return xxx.jsp

    }

 

Action class:

private String comboHobbyId; //with getter/setter method

 

Result:

假设选中第二个选项,则comboHobbyId的值为‘1’对应的hobbyName为“Football

 

---------------------------------------------------------

 后续章节:

Struts2几个常用标签的主要属性及示例(二)

Struts2几个常用标签的主要属性及示例(三)

Struts2几个常用标签的主要属性及示例(四)

Struts2几个常用标签的主要属性及示例(五)

 

   发表时间:2010-03-28  
打死我也不会用任何框架专有的标签,要是换一个框架的话,页面代码几乎要重写。
0 请登录后投票
   发表时间:2010-03-29  
说的非常好,谢谢分享经验。期待“(二)”的出现。
0 请登录后投票
   发表时间:2010-04-10  
fu013013 写道
打死我也不会用任何框架专有的标签,要是换一个框架的话,页面代码几乎要重写。

有时候框架自带的标签好用多了,像循环遍历,变量赋值都十分方便。
0 请登录后投票
   发表时间:2010-04-10  
LZ写的非常详细,长知识了,收藏了。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics