Struts 2.x学习----------------UI标签

学习记录:

在Struts 2.x里面本意是希望方便用户的开发,所以在许多的标签上都是用了操作模板,但是在实际的布局中会发现这些模板如果出现则会破坏布局要求
范例:观察表单问题
<s:form action="FormAction.action" method="post">
         <s:textfield key="name" label="用户名"/>
         <s:submit vaule="发送"/>
</s:form>
-----------------------------------------------------------生成代码
<form id="FormAction" name="FormAction" action="FormAction.action" method="post">
    <table class="wwFormTable">
        <tr>
            <td class="tdLabel"><label for="FormAction_name" class="label">用户名:</label></td>
            <td><input type="text" name="name" value="" id="FormAction_name"/></td>
        </tr>
        <tr>
            <td colspan="2"><div align="right"><input type="submit" id="FormAction_0" value="Submit" vaule="发送"/></div></td>
        </tr>
    </table>
</form>
这个时候如果使用了UI标签就必须承受这些生成代码所带来的问题,但是为了解决这种布局带来的混乱,所以取消掉使用的页面模板
范例:取消模板
<s:form action="FormAction.action" method="post" theme="simple">
         <s:textfield key="name" label="用户名" theme="simple" />
         <s:submit vaule="发送" theme="simple"/>
</s:form>
此时虽然取消了布局代码对页面的影响,但是对于前端工程师来讲依然无法知道此类代码,这些是标签,在直白界面下无法显示,如果说有美工要进行修饰,那么美工还必须自己搭建好服务器,设置好Struts,完全不靠谱
如果公正的来讲,UI标签在一些组件的生成上还是挺方便的,例如,现在要传递一个组部门信息,下网可以生成下拉列表框
范例:在Action保存一组部门信息
public class FormAction extends ActionSupport {
    public String execute() throws Exception{
        List<Dept> all=new ArrayList<Dept>();
        for(int i=0;i<5;i++){
            Dept dept=new Dept();
            dept.setDeptno(i);
            dept.setDname("技术部--"+i);
            all.add(dept);
        }
        ServletActionContext.getRequest().setAttribute("allDepts",all);
        return "form.show";
    }
}
如果此时页面中要想输出下拉列表框,有两种选择,一种是利用循环采用迭代的方式输出每一个下拉的列表项,另外一种是使用Struts 2.x标签生成
范例:利用标签生成
<s:select list="#request.allDepts" listKey="deptno" listValue="dname" id="dept" name="dept" theme="simple"/>
除了生成下拉列表框之外也可以生成复选框
范例:利用标签生成复选框
部门:<s:checkboxlist list="#request.allDepts" listKey="deptno" listValue="dname" id="dno" name="dno"/>
如果要想接收复选框的内容,可以在Action里面定义数组
范例:接收复选框
private Integer[] dno;//与复选框组件的参数名称相同
    public void setDno(Integer[] dno){
        this.dno=dno;
}
public String execute() throws Exception{
        System.out.println("-------------"+Arrays.toString(this.dno));
        List<Dept> all=new ArrayList<Dept>();
        for(int i=0;i<5;i++){
            Dept dept=new Dept();
            dept.setDeptno(i);
            dept.setDname("技术部--"+i);
            all.add(dept);
        }
        ServletActionContext.getRequest().setAttribute("allDepts",all);
        return "form.show";
}
此时发现不管是单值还是一个数组,都可以自动实现转型并且自动赋值

猜你喜欢

转载自blog.csdn.net/amuist_ting/article/details/80949242