随笔-10  评论-22  文章-1  trackbacks-0

常用的Struts标签库有以下五大类:

1.HTML Tag:

用来创建能够和Struts框架以及其它相应的HTML标签交互的HTML输入表单;

如:<html:form></html:form>,<html:text/>,<html:password/>,<html:radio/>,<html:checkbox/>,<htmlmultibox>

2.Bean Tag

该标签库包含的标签可以用来创建bean、访问bean和访问bean的属性。

 如:<bean:write/>:用于将bean的属性输送到jsp页面,<bean:define/>定义一个新的bean

3.Logic Tag

该标签库包含的标签可以用来进行逻辑判断、集合迭代和流程控制。

如:<logic:iterate/>:用来循环迭代,<logic:eaual/>:用来进行逻辑判断

4.Nested:增强对其他Struts标签的嵌套使用能力

该标签库建立在前三个标签库的基础上,具有前三个标签库的所有功能,只是允许标签间的嵌套。

5.Template Tag

随着Titles框架包的出现,此标记已开始减少使用

下面介绍几个最常用的标签:

<html:check box/>一般用于一个选项的复选框

<html:multibox/>一般用于多个选项的复选框

<bean:write name="user" property="username"/>等同于EL表达示:${user.username}

<bean:define id="inter" name="user" property="interest" type="java.lang.String"/>定义一个bean

<bean:message key=" " arg0=" "/> key 定义在资源文件中,args0[1,2]为参数

<logic:iterate name="list" id="user"> 等同于JSTL的:<c:foeach item=${list} var="user"/>

<logic:equal name="user" property="sex" value="0"/>等同于JSTL的:<c:when test=""/>

<logic:empty />标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理

<logic:empty name="listForm" property = "persons">
<div>集合persons为空!</div>
</logic:empty>

1.下面给一个表单的完整代码:

<body>
    
<center>
        
<html:form action="/user">
        用户名:
<html:text property="user.username"></html:text><p/>
        密码:
<html:text property="user.pwd"></html:text><p/>
        性别:
<html:radio property="user.sex" value=""></html:radio>
            
<html:radio property="user.sex" value=""></html:radio><p/>
        城市:
<html:select property="user.city">
            
<html:option value="">请选择</html:option>
            
<html:option value="武汉">武汉</html:option>
            
<html:option value="上海">上海</html:option>
            
<html:option value="北京">北京</html:option>
        
</html:select><p/>
        爱好:
<html:multibox property="interest" value="看书"/>看书
            
<html:multibox property="interest" value="游戏"/>游戏
            
<html:multibox property="interest" value="睡觉"/>睡觉<p/>
            
<html:submit/><html:cancel/>
        
</html:form>
    
</center>
    
</body>

使用html标签作为表单输入,可以方便的使用验证框架即:<html:errors property="username">

2.下面给一个显示数据的代码:

<table align="center" border="1" width="600">
        
<caption>用户注册信息</caption>
        
<tr>
            
<td>用户名</td>
            
<td>密码</td>
            
<td>性别</td>
            
<td>城市</td>
            
<td>爱好</td>
            
<td colspan="2" align="center">操作</td>
        
</tr>
        
<logic:iterate name="list" id="user">
        
<tr>
            
<td><bean:write name="user" property="username"/></td>
            
<td><bean:write name="user" property="pwd"/></td>
            
<td><bean:write name="user" property="sex"/></td>
            
<td><bean:write name="user" property="city"/></td>
            
<td>
                
<bean:define id="interest" name="user" property="interest" type="java.lang.String"></bean:define>
                
<logic:iterate id="inter" collection="<%=StringUtil.stringChange2(interest)%>">
                    $
{inter}
                
</logic:iterate>
            
</td>
            
<td><a href="del.do?userid=${user.userid}">删除</a></td>
            
<td><a href="upd.do?userid=${user.userid}">修改</a></td>
        
</tr>
        
</logic:iterate>
    
</table>

作为显示数据,Struts标签并不比Jstl与EL方便,因此,本人更习惯用后者

其实Struts标签的好处,并不是上面这些,而是它可利用ActionForm来填充数据

比如我们在做页面数据修改的时候,会让当前页面数据显示到显示修改页面,这样利于客户端修改

以前我们这样做的:根据id从数据库查出,然后使用html的value属性填充,现在有了Struts标签,就不需要那么麻烦了

直接在Action里填充ActionForm的数据就搞定了

public ActionForward upd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        UserForm userForm 
= (UserForm) form;
        
int userid = Integer.parseInt(request.getParameter("userid"));
        UserInfo user 
= biz.findUser(userid);
        String[]interest 
= StringUtil.stringChange2(user.getInterest());
        
//将用户信息填充到ActionForm
        userForm.setUser(user);
        userForm.setInterest(interest);
        
return mapping.findForward("upd");
    }
然后在修改页面显示,请往下看:
<html:form action="/doupd">
        用户名:
<html:text property="user.username"></html:text><p/>
        密码:
<html:text property="user.pwd"></html:text><p/>
        性别:
<html:radio property="user.sex" value=""></html:radio>
            
<html:radio property="user.sex" value=""></html:radio><p/>
        城市:
<html:select property="user.city">
            
<html:option value="">请选择</html:option>
            
<html:option value="武汉">武汉</html:option>
            
<html:option value="上海">上海</html:option>
            
<html:option value="北京">北京</html:option>
        
</html:select><p/>
        爱好:
<html:multibox property="interest" value="看书"/>看书
            
<html:multibox property="interest" value="游戏"/>游戏
            
<html:multibox property="interest" value="睡觉"/>睡觉<p/>
            
<html:submit value="修改"/>
        
</html:form>
怎么样,简单方便吧,其实Struts标签还是有它的好处的。
posted on 2009-03-14 23:47 独孤行 阅读(1573) 评论(1)  编辑  收藏 所属分类: Struts

评论:
# re: Struts核心标签(原创) 2009-03-15 13:05 | 阳衡锋
好像界面层的标签还是自己封装比较好,我现在的项目的,我就封装了几个标签。没有花多少功夫,基本上只要封装select radio checkbox 就可以了。其他的直接写html+el反而比较好。  回复  更多评论
  

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


网站导航: