zoukankan      html  css  js  c++  java
  • Struts2--标签tag

      在Struts2框架中提供了一套标签库,可以与struts2无缝结合。

      

      数据标签a、action、bean、date、debug、i18n、include、param、property、push、set、text、url

      控制标签:if、elseif、else、append、generator、interator、merge、sort、subset

    简单实例

    property:用来取得值桟中的值。

    <s:push value="myBean">
        <!-- Example 1: -->
        <s:property value="myBeanProperty" />
     
        <!-- Example 2: 
            escapeHtml :表示是否跳过html,默认为true
        -->TextUtils
        <s:property value="myBeanProperty" default="a default value"  escapeHtml=“false”/>
    </s:push>
    View Code

    param:用来给其他标签传入参数

      |-name:参数的名字

      |-value:参数的值

      |-suppressEmptyParameters:是否压缩空参数

    set:用来声明一个在特定作用于的变量

      作用域有:application、session、request、page、action

      几个属性:

        |-scope:作用域,默认是action

        |-value:变量的值

        |-var:变量的名字

    *注意变量值的地方需要在双引号里面加单引号

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    <s:set var="i" value="1"></s:set>
    <s:property value="#i"/><br/>
    <s:set var="xx" value="#i" scope="session"/>
    <s:set var="a" value="'application范围的值'" scope="application"/>
    <s:set var="s" value="'session范围的值'" scope="session"/>
    <s:set var="r" value="'request范围的值'" scope="request"/>
    <s:set var="p" value="'page范围的值'" scope="page"/>
    <s:set var="ac" value="'action范围的值'" scope="action"></s:set>
    
    -------------------------<br/>
    <s:property value="#session.xx"/><br/>
    <s:property value="#ac" /><br/>
    <s:property value="#attr.p" /><br/>
    <s:property value="#request.r" /><br/>
    <s:property value="#session.s" /><br/>
    <s:property value="#application.a"/><br/>
    
    
    </html>
    View Code

    输出结果:

    bean:用来定义一个java bean

       |-name:bean的全路径类名字

      |-var:定义的javabean的名字

    package com.fuwh.model;
    
    public class Student {
    
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
    }
    View Code
    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <s:bean name="com.fuwh.model.Student" var="student">
            <s:param name="id" value="2"></s:param>
            <s:param name="name" value="'张三'"></s:param>
        </s:bean>
        -------------------------<br/>
        <s:property value="#student.id"/>
        <s:property value="#student.name"/>
    </body>
    </html>
    View Code

    执行结果:

    date:用不同的方式格式化日期

       |-fromat:指定日期显示格式

      |-name:需要被格式化的日期的值

      |-nice:是否nicely的打印日期

      |-timezone:格式化日期的时区

      |-var:引用值桟中的日期

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page import="java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <%
            request.setAttribute("date",new Date());
        %>
        -------------------------<br/>
        ${date }<br/>
        <s:date name="#request.date" format="yy-MM-dd hh:mm:ss"/>
    </body>
    </html>
    View Code

    输出结果:

    debug标签:可以输出值桟中的值

    <s:debug></s:debug>

    url和a标签

      url:创建一个url变量,可以在里面 用param标签来传入参数

      a:创建一个url

    includ:用来引入别的资源

    <s:include value="head.jsp"></s:include>

    控制标签

    if,elseif,else

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page import="java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <%
            int age=18;
            request.setAttribute("age", age);
        %>
        
        <s:if test="#request.age>18">
            已经成年
        </s:if>
        <s:elseif test="#request.age<18">
            还没成年
        </s:elseif>
        <s:else >
            刚好年芳18
        </s:else>
    </body>
    </html>
    View Code

    iterator:用来循环输出

      |-begin:循环输出的开始下标,默认0

      |-end:循环输出的结束下标

      |-status:代表下一个要被输出的

      |-step输出的每次跳跃多少

      |-value:被遍历的对象

      |-var:代表每个取出的变量

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page import="java.util.*" %>
    <%@ page import="com.fuwh.model.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <%
            List<Student> stuList=new ArrayList<Student>();
            stuList.add(new Student(1,"fuwh"));
            stuList.add(new Student(2, "zhangsan"));
            stuList.add(new Student(3, "lisi"));
            request.setAttribute("stuList", stuList);
        %>
        <table>
            <tr>
                <th>编号</th>
                <th>学号</th>
                <th>名字</th>
            </tr>
            <s:iterator value="#request.stuList" var="student" status="status">
            <tr>
                <td><s:property value="#status。index+1"/></td>
                <td><s:property value="#request.student.id"/></td>
                <td><s:property value="#request.student.name"/></td>
            </tr>
            </s:iterator>
        </table>
        
    </body>
    </html>
    View Code

    append:把输出的对象叠加起来

      |-var:代表叠加后的对象名

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page import="java.util.*" %>
    <%@ page import="com.fuwh.model.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <%
            List<Student> stuList=new ArrayList<Student>();
            stuList.add(new Student(1,"fuwh"));
            stuList.add(new Student(2, "zhangsan"));
            stuList.add(new Student(3, "lisi"));
            request.setAttribute("stuList", stuList);
            List<Student> stuList2=new ArrayList<Student>();
            stuList2.add(new Student(1,"fuwh"));
            stuList2.add(new Student(2, "zhangsan"));
            stuList2.add(new Student(3, "lisi"));
            request.setAttribute("stuList2", stuList2);
        %>
        <s:append var="stuList3">
            <s:param value="#request.stuList"></s:param>
            <s:param value="#request.stuList2"></s:param>
        </s:append>
        <table>
            <tr>
                <th>编号</th>
                <th>学号</th>
                <th>名字</th>
            </tr>
            <s:iterator value="stuList3" var="student" status="status">
            <tr>
                <td><s:property value="#status.index+1"/></td>
                <td><s:property value="id"/></td>
                <td><s:property value="name"/></td>
            </tr>
            </s:iterator>
        </table>
        
    </body>
    </html>
    View Code

    generator标签

      用来分割生成一个迭代输出器。

    Merge标签

      用来组合

    Sort标签

      排序标签

    Subset标签

      截取

    界面标签

      Form标签

      Text标签

      

      Radio标签

       Checkboxlist标签

      Select标签

    其他标签

  • 相关阅读:
    结语
    创建ejs模板的express工程
    浏览器控制台命令调试——console
    JS获取URL中参数值(QueryString)的4种方法分享
    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT
    javascript 获取页面的高度及滚动条的位置的代码
    javascript 页面各种高度宽度
    导出Excel之Epplus使用教程2(样式设置)
    索引 'GXHRCS.PK_A253' 或这类索引的分区处于不可用状态
    数据库操作
  • 原文地址:https://www.cnblogs.com/zerotomax/p/6340825.html
Copyright © 2011-2022 走看看