专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » struts2标签:Struts核心标签 »正文

struts2标签:Struts核心标签

来源: 发布时间:星期四, 2009年3月19日 浏览:0次 评论:0
        常用Struts标签库有以下 5大类:         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:/>定义个新bean

        3.Logic Tag

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

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

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

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

        5.Template Tag

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

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

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

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

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

        <bean: id="er" name="user" property="erest" type="java.lang.String"/>定义个bean

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

        <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="erest" value="看书"/>看书
            
<html:multibox property="erest" value="游戏"/>游戏
            
<html:multibox property="erest" 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: id="erest" name="user" property="erest" type="java.lang.String"></bean:>
                
<logic:iterate id="er" collection="<%=StringUtil.Change2(erest)%>">
                    $
{er}
                
</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从数据库查出然后使用htmlvalue属性填充现在有了Struts标签就不需要那么麻烦了

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

public ActionForward upd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        UserForm userForm 
= (UserForm) form;
        
 userid = Integer.parseInt(request.getParameter("userid"));
        UserInfo user 
= biz.findUser(userid);
        Stringerest 
= StringUtil.Change2(user.getInterest);
        
//将用户信息填充到ActionForm
        userForm.User(user);
        userForm.Interest(erest);
        
 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="erest" value="看书"/>看书
            
<html:multibox property="erest" value="游戏"/>游戏
            
<html:multibox property="erest" value="睡觉"/>睡觉<p/>
            
<html:submit value="修改"/>
        
</html:form>





        如何样简单方便吧其实Struts标签还是有它好处 TAG: Struts STRUTS

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: