zoukankan      html  css  js  c++  java
  • 标签属性struts2.X心得14struts标签详细讲解

    废话就不多说了,开始。。。

        Property标签

        (1)、说明

        用于输出指定的值

        (2)、属性

        default:可选属性,如果输出的值为null,则表现该属性指定的值。

        ecape:选属性,指定是不是格式化为html代码

        vlue:选属性,指定需要输出的属性值,如果没有指定该属性,则默认输出ValueStack栈顶的值。

        

     

        (3)、例1(默认值)

        

    testOgnlTag.jsp

    利用property标签获得栈顶的值:

    <br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testProperty1.action">测试</a>

    OgnlTagAction

    public String testProperty1(){

    return "ognlTag";

    }

    successOgnlTag.jsp中:

    利用property标签获得栈顶的值:<br>

    <s:property/> //没有value属性,所以应当输出的是栈顶元素

    结果:

    cn.itcast.struts2.action.ognltag.OgnlTagAction@5a82b2

        (4)、例2(default和value)

        

    testOgnlTag.jsp

    测试property中的default的值:<br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testDefault.action">测试</a><br>

    OgnlTagAction

    public String testDefault() {

           ServletActionContext.getRequest().setAttribute("request_username",

                  "username");

           return "ognlDefault";

    }

    successOgnlTag.jsp

    propertyvalue没有值的情况下,输出default的值:<br>

    <s:property value="#request.request_username11" default="default value"/>

    <s:property value="#request.request_username" default="default value"/>

    说明因为在后台request中的key值为.request_username而页面上的输出为.request_username11不对应所以应当输出default的值。

     (5)、例3(escape)

        

    testOgnlTag.jsp中:

    测试property中的escape的值:<br>

    <s:property value="%{'<hr>hr的剖析'}"/>

    说明:因为escapse默认的值为true,也就是说<hr>hr的剖析会看成一个字符串处理。

    <s:property value="%{'<hr>hr的剖析'}" escape=”false”/>

    说明:因为如果escapsefalse,则把字符串中符合html标签的语法,会看成html标签来停止处理。

        (6)、例4(输出栈顶String的值)

        

    testOgnlTag.jsp中:

    取出栈顶的值:<br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testString.action">测试</a><br>

    OgnlTagAction

    public String testString(){

           ActionContext.getContext().getValueStack().push("msg");

           return "ognlString";

    }

    successOgnlTag

    取出栈顶的值:<br>

    <s:property/>

        2 Debug标签

        (1)、说明

        利用debug标签可以输出OGNLContext所有的值

        (2)、例子

        

    testOgnlTag.jsp:

    利用debug标签输出ognl的所有的值:<br>

    <a

           href="${pageContext.request.contextPath}/ognl/ognlTagAction_testDebug.action">测试</a><br>

    OgnlTagAction

    public String testDebug(){

           return "ognlDebug";

    }

    successOgnlTag.jsp中:

    利用debug标签输出OgnlContext中所有的值:<br>

    <s:debug></s:debug>//利用这个标签可以输出ognlcontext中所有的值

        Set标签

        (1)、说明

        用于将某个指定的值放入到指定的范围

        (2)、属性

        var:变量的名字,name、id与var表达的含意是一样的。Var已取代了name,id;

        Scope:指定变量被放置的范围。该属性可以接受application,session,request,page或 Action。如果没有设置该属性,则默认会放在action中。

        Value:赋值给变量的值。如果没有设置该属性,则将ValueStack栈顶的值赋给变量。

        (3)、例1

        

    testOgnlTag.jsp

    测试set标签:<br>

    <a

           href="${pageContext.request.contextPath}/ognl/ognlTagAction_testSet.action">测试</a><br>

    OgnlTagAction

    public String testSet() {

           ServletActionContext.getRequest().setAttribute("request_username",

                         "username");

           return "ognlSet";

    }

    successOgnlTag.jsp

    测试set标签:<br>

    <s:set value="#request.request_username" var="aaaaa" scope="request"></s:set>

    <s:property value="#request.aaaaa"/>

    说明:这样的情况,aaaaakey值会出现在request域中。

    <s:set value="#request.request_username" var="aaaaa"></s:set>

    <s:property value="#request.aaaaa"/>

    说明:这种写法aaaaa会作为一个key值存在ognlmap中。所以利用

    <s:property value="#aaaaa"/>也能输出值。

     

        Push标签

        (1)、说明

        把对象放入到栈顶,不能放入到其他的范围,当标签结束时,会从栈顶删除。

        (2)、例子

        

    testOgnlTag.jsp

    push方法把一个对象放入到栈顶:<br>

    <a

           href="${pageContext.request.contextPath}/ognl/ognlTagAction_testPUSH.action">测试</a><br>

    OgnlTagAction

    public String testPUSH(){

           ServletActionContext.getRequest().setAttribute("request_username", "username");

           return "ognlPUSH";

    }

    successOgnlTag.jsp

    利用push方法把对象放入到栈顶:<br>

    <s:push value="#request.request_username">

           <s:property/>

          <s:debug></s:debug>//注意debug标签的位置,这个位置是准确的

    </s:push>

    <s:debug></s:debug>//这个位置的标签是错误的。因为s:push标签已结束,所以栈顶元素已被删除掉了。

        Bean标签

        (1)、说明

        实例化一个符合javabean规范的class,标签体内可以包括几个param元素,可用于调用set方法,给class的属性赋值。

        (2)、属性

        Name:要被实例化的class的名字,符合javabean规范。

        Var:  赋值给变量的值。放置在request作用域中。如果没有设置该属性,对象被设置到栈顶。

        (3)、例子

        

    testOgnlTag.jsp

    利用bean标签实例化一个person对象:<br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testBean.action">测试</a><br>

    OgnlTagAction

    public String testBean(){

           ServletActionContext.getRequest().setAttribute("pid", 1);

           ServletActionContext.getRequest().setAttribute("request_username", "username");

           ServletActionContext.getRequest().setAttribute("pname", "username");

           ServletActionContext.getRequest().setAttribute("comment", "person");

           return "ognlBean";

    }

    successOgnlTag.jsp

    给一个person赋值,值来自于后台:<br>

                 <s:bean name="cn.itcast.struts2.valuestack.bean.Person" var="myperson">

                        <s:param name="pid" value="#request.pid"></s:param>

                        <s:param name="pname" value="#request.pname"></s:param>

                        <s:param name="comment" value="#request.comment"></s:param>

                        <!--因为person在栈顶,所以输出栈顶-->

                        <s:property value="pid"/>

                        <s:property value="pname"/>

                        <s:property value="comment"/>

                        <s:debug></s:debug>

                 </s:bean>

                  <s:property/>//值不再是Person对象

                  <!—

                         前提条件:不加var=”myperson”属性由于bean标签在栈顶,所以当bean标签结束的时候,

                        栈顶的person对象就被删除掉了。所以在bean标签的外部输出栈顶的person值是不行的。

                 -->

        

    <s:property/>

                 <!--

                        bean标签中加var属性值为myperson,debug标签再观察其值的分布情况

                        map中出现了"myperson":person对象

                 -->

                 <s:property value="#person.pid"/><br>

                 <s:property value="#person.pname"/><br>

                 <s:property value="#person.comment"/><br>

           说明:当加上var=”myperson”属性的时候,myperson对象出现在了map中,这个时候就可以在bean标签的外部获得person的属性了。

        6 Action

        标签

        (1)、说明

        通过指定命名空间和action的名称,可以直接调用后台的action.

        (2)、属性

        Name: Aciton的名字

        Namespace: Action所在的命名空间(action的名称后不加.action)

        executeResult: Action的result是不是需要被执行,默认值为false,不执行

        (3)、例子

        

    testOgnlTag.jsp

    通过action标签访问后台的action:<br>

    <s:action name="ognlTagAction_testBean" namespace="/ognl" executeResult="true"></s:action>

    说明

           如果executeResult的属性值为false,会执行到相应的action,但是action跳转后的页面

           将不再执行。如果executeResult的属性值为true,则会在当前页面包括跳转后的页面

           值。

        Iterator标签

        (1)、说明

        该标签用于对集合停止迭代。这里的集合包括:list,set和数组

        (2)、属性

        Value:可选属性,指定被迭代的集合。如果没有设置该属性,则使用对象栈顶的集合。

        Var:可选属性,引用变量的名称

         Status:可选属性,该属性指定迭代时的IteratorStatus实例。该实例包括如下的方法:

          int getCount() 返回当前迭代的元素个数

          int getIndex() 返回当前迭代元素的索引

          boolean isEven() 返回当前迭代元素的索引是不是是偶数

          boolean isOdd()  返回当前迭代元素的索引是不是是奇数

          boolean isFirst()  返回当前迭代元素是不是为第一个元素

          boolean isLast()  返回当前迭代元素是不是为最后一个元素

        (3)、例1(push)

        

    testOgnlTag.jsp

    通过iterator标签把后台的list集合遍历(list通过push方法压入到栈顶)<br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList1.action">测试</a><br>

    OgnlTagAction

    /*

            * 把生成的list利用push方法压入到栈顶

            */

           public String testList1(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  List<Person> person1List = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                 

                  for(int i=11;i<15;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         person1List.add(person);

                  }

                 

        

    /*

                   * 说明:

                   *          1、执行完三次压栈以后,对象栈的栈顶存放的元素为String类型的aaa;

                   *                 而这个时候如果页面用<s:iterator value="top"></s:iterator>

                   *                         top代表对象栈的栈顶的元素

                   *                  停止迭代的时候,针对的元素是aa

      2、执行第一次压栈和第二次压栈,对象栈的栈顶存放的元素为person1List;

                   *                  页面上用

                   *                         <s:iterator value="top"></s:iterator>

                   *                         top代表对象栈的栈顶的元素

                   *                  停止迭代的时候,针对的元素是person1List;

                   *           3、执行第一次压栈,对象栈的栈顶存放的元素为personList;

                   *                  页面上用

                   *                         <s:iterator value="top"></s:iterator>

                   *                         top代表对象栈的栈顶的元素

                   *                  停止迭代的时候,针对的元素是personList;                 

                   */         

                  //personList压入到栈顶

                  ActionContext.getContext().getValueStack().push(personList);

                  /*

                   * perosn1List压入到栈顶

                   */

                  ActionContext.getContext().getValueStack().push(person1List);

                  /*

                   * aaa压入到栈顶

                   */

                  ActionContext.getContext().getValueStack().push("aaa");

                  return "ognlList1";

           }

     

    successOgnlTag.jsp

    iterator标签迭代后台list中的数据(list通过push方法压入到栈顶 ):<br>

     <!--

          因为iterator是一个迭代器标签,value的属性值top代表栈顶的元素

     -->

    <s:iterator value="top">

      <s:property value="pname"/>

      <s:property/>//可以输出对象栈栈顶的元素

      <s:debug></s:debug>//输出OgnlContext的结构

    </s:iterator><br>

           <s:debug></s:debug>//iterator迭代完之后观察OgnlContext的结构

    说明:当停止迭代的时候,当前的被迭代的对象会被放入到栈顶,所以在property元素输出的时候不用加#号。当iterator标签结束的时候,栈顶的对象会被删除掉。

        (4)、例2(action属性)

        

    testOgnlTag.jsp

    通过iterator标签把后台的list集合遍历(list作为action中的属性)<br>

    <a href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList2.action">测试</a><br>

    OgnlTagAction

    private List<Person> pList;

       public List<Person> getPList() {

          return pList;

       }

       public void setPList(List<Person> list) {

          pList = list;

       }

       public String testList2(){

       ServletActionContext.getRequest().setAttribute("request_username", "username");//successOgnlTag.jsp

    <s:push value="#request.request_username">标签需要用到后台的这个操作。

          this.pList = new ArrayList<Person>();

          for(int i=0;i<10;i++){

             Person person = new Person();

             person.setPid(i);

             person.setPname("person"+i);

             person.setComment("person"+i);

             pList.add(person);

          }

          return "ognlList2";

       }

    说明:通过代码可以看出来,这个例子中把pList作为action的属性,然后给其赋值。

    successOgnlTag.jsp

    iterator标签迭代后台list中的数据(list作为action中的属性 ):<br>

           <s:iterator value="pList">

             <s:property value="pname"/>

             <s:property/>

             <s:debug></s:debug>

           </s:iterator><br>

             <s:debug></s:debug>

    说明:因为OgnlTagAction在对象栈,所以value中的pList可以不加#号。从第一个debug标签可以看出,这个标签把当前正在迭代的对象临时放入到了栈顶。如果iterator元素结束迭代时,栈顶的对象就消失了。所以第一次的debug和第二次的

    Debug内容是不一样的。

        (5)、例3(Map中)

        

    testOgnlTag.jsp

    通过iterator标签把后台的list集合遍历(list通过ActionContext.getContext().put放入OgnlContextMap)<br>

     <a

           href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList3.action">测试</a><br>

    OgnlTagAction

    /*

            * personList通过put方法放入到OnglContextmap

            */

           public String testList3(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ActionContext.getContext().put("personList", personList);

                  return "ognlList3";

           }

    successOgnlTag.jsp

    iterator标签迭代后台list中的数据(list经过ActionContext.getContext().put方法放入到OgnlContext ):<br>

                 <s:iterator value="personList">

                      <s:property value="pname"/>

                      <s:property/>

                      <s:debug></s:debug>

                 </s:iterator><br>

                         <s:debug></s:debug>

    说明:用iterator停止迭代,迭代的集合如果来自map,value的值可以加#也可以不加#,这点需要注意。Debug的现象和例2一样。

        每日一道理
    水仙亭亭玉立,兰花典雅幽香,牡丹雍容华贵,梨花洁白无暇……美丽的花朵总能得到世人的羡慕与赞叹,殊不知,它从一粒小小的种子到最后开花,要历经无数的艰辛与坎坷!我们的成长也是如此。只有做辛勤的“织梦者”,我们的梦想才会成真!

        (6)、例4(begin,end,step)

        

    testOgnlTag.jsp

    利用iteratorbeginendstep属性控制数据的表现:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList4.action">测试</a><br>

    OgnlTagAction

    /*

            * 在页面上通过iteratorbegin,end,step属性控制页面的表现

            */

           public String testList4(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ServletActionContext.getRequest().setAttribute("first", 2);

                  ServletActionContext.getRequest().setAttribute("end", 8);

                  ServletActionContext.getRequest().setAttribute("step", 2);

                  ActionContext.getContext().put("personList", personList);

                  return "ognlList4";

           }

    successOgnlTag.jsp

    iterator标签属性begin,end,step控制数据的表现:<br>

                 <s:iterator value="personList" begin="%{#request.first}" end="%{#request.end}" step="%{#request.step}">

                      <s:property value="pname"/><br>

                      <s:property/><br>

                 </s:iterator><br>

    说明:begin属性为开始位置,end属性为结束位置,step为步长。这里要注意begin的值是通过ognl表达式传递过来的。

        (7)、例5(status)

        

    testOgnlTag.jsp

    测试iteratorstatus属性:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList5.action">测试</a><br>

    OgnlTagAction

    /*

            * 测试iteratorstatus属性

            */

           public String testList5(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ActionContext.getContext().put("personList", personList);

                  return "ognlList5";

           }

    successOgnlTag.jsp

    测试iteratorstatus属性的值:<br>

                  <!--

                       如果提供status 每次迭代时候将生成一个IteratorStatus实例并放入堆栈中

                       debug标签查看堆栈,在map中,存在一个st,类型为org.apache.struts2.views.jsp.IteratorStatus

                       st对应一个IteratorStatus对象,有如下属性

                              int getCount() 返回当前迭代的元素个数

                                int getIndex() 返回当前迭代元素的索引

                                boolean isEven() 返回当前迭代元素的索引是不是是偶数

                                boolean isOdd()  返回当前迭代元素的索引是不是是奇数

                                boolean isFirst()  返回当前迭代元素是不是为第一个元素

                                boolean isLast()  返回当前迭代元素是不是为最后一个元素-->

                   <table border="1">

                        <s:iterator value="personList" var="person" status="st">

                               <tr>

                                      <td><s:property value="#st.count"/></td>

                                      <td><s:property value="#st.getIndex()"/></td>

                                      <td><s:property value="#person.pname"/></td>

                               </tr>

                        </s:iterator>

                  </table>

     

                  <br>

        

    说明:上述标明status属性,就会在栈中的map中有一个key值为st,而st对应的值为org.apache.struts2.views.jsp.IteratorStatus的对象。在迭代每一个元素的时候,都存在这个对象,我们可以根据这个对象获得到该对象的属性。从而得到遍历中的相关信息。

     (8)、

        

    testOgnlTag.jsp

    利用iteratorstatus属性完成隔行变色:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testList6.action">测试</a><br>

    OgnlTagAction

    /*

            *    利用iteratorstatus属性完成表格的隔行变色

            */

           public String testList6(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ActionContext.getContext().put("personList", personList);

                  return "ognlList6";

           }

    successOgnlTag.jsp

    <style type="text/css">

                 .odd{

                        background-color:red;

                 }

                 .even{

                        background-color:blue;

                 }

      </style>

        例6(奇偶行变色)

        

    利用iteratorstatus属性隔行变色:<br>

                  <table border="1">

                        <s:iterator value="personList" var="person" status="st">

                               <!--

                                      value的值可以跟双目表达式

                                -->

                               <tr class="<s:property value="#st.even?'even':'odd'"/>">

                                      <td><s:property value="#st.count"/></td>

                                      <td><s:property value="#st.getIndex()"/></td>

                                      <td><s:property value="#st.isEven()"/></td>

                                      <td><s:property value="#st.isOdd()"/></td>

                                      <td><s:property value="#st.isFirst()"/></td>

                                      <td><s:property value="#st.isLast()"/></td>

                                      <td><s:property value="#person.pid"/></td>

                                      <td><s:property value="#person.pname"/></td>

                               </tr>

                        </s:iterator>

                  </table>

    说明:<s:property value="#st.even?'even':'odd'"/>这是一个双目表达式。

         

        8   If/elseif/else标签

        (1)、说明

        基本的流程控制标签。If标签可以单独使用,也可以结合elseif或else标签使用。

        (2)、属性

        test:后面跟判断表达式。

        (3)、例子

        

    testOgnlTag.jsp

    测试if/elseif/else标签的使用:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testIF.action">测试</a><br>

    OgnlTagAction

    /*

            * 测试if标签

            */

           public String testIF(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

                  for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ActionContext.getContext().put("personList", personList);

                  return "ognlIF";

           }

    successOgnlTag.jsp

    测试if/elseif/else标签:<br>

                 <table border="1">

                        <s:iterator value="personList" var="person" status="st">

                               <!--

                                      value的值可以跟双目表达式

                               -->

                               <tr class="<s:property value="#st.even?'even':'odd'"/>">

                                      <td><s:property value="#person.pid"/></td>

                                      <td><s:property value="#person.pname"/></td>

                                      <td>

                                             <s:if test="#person.pid<3">这个id号小于3</s:if>

                                             <s:elseif test="#person.pid<5">这个id号大于等于3小于5</s:elseif>

                                             <s:else>这个id号大于等于5</s:else>

                                      </td>

                               </tr>

                        </s:iterator>

                 </table>

        url标签

        (1)、说明

        该标签用于创建url,可以通过”param”标签提供request参数。

        (2)、属性

        Value:

          如果没有值,就用当前的action,使用value后必须加.action.

        Action:

          用来生成url的action.如果没有使用则用value;

        Namespace:

          命名空间

        Var:

          引用变量的名称。

        

    testOgnlTag.jsp

    测试url标签的使用:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testURL.action">测试</a><br>

    OgnlTagAction

    /*

            * 测试标签url

            */

           public String testURL(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  return "ognlURL";

           }

        (3)、例子

        

    successOgnlTag.jsp

    测试url标签的使用:<br>

                 <!--

                      作用:直接输出url地址

                   -->

                 <s:url action="ognlTagAction.action" namespace="/ognl" var="myurl">

                      <s:param name="pid" value="12"></s:param>

                      <s:param name="pname" value="%{'zd'}"></s:param>

                 </s:url>

                 <s:debug></s:debug>

                 <a href="<s:property value="#myurl"/>">test</a>

    说明:如果url标签不写var属性,则url标签输出url的路径。如果写上var变量,

    则会在OgnlContext中出现相应的key值:myurl。所以在程序的最后一行引用了

    #myurl变量从而得到路径。Param是传递的参数。这里需要注意:

    value="%{'zd'}可以,但是value=”zd”是不行的。后面的写法会去栈顶查找zd

    所以s:url标签就相当于声明一个url,然后在外部使用。

        10  Ognl操作集合

        

    Java代码

    OGNL表达式

    list.get(0)

    List[0]

    array[0]

    array[0]

    ((User)list.get(0)).getName()

    list[0].name

    Array.length

    Array.length

    List.size()

    List.size

    List.isEmpty()

    List.isEmpty

        

    testOgnlTag.jsp

    测试集合的长度:<br>

    <a

    href="${pageContext.request.contextPath}/ognl/ognlTagAction_testListLength.action">测试</a><br>

    OgnlTagAction

    /*

            * 测试集合的长度

            */

           public String testListLength(){

                  ServletActionContext.getRequest().setAttribute("request_username", "username");

                  List<Person> personList = new ArrayList<Person>();

        

    for(int i=0;i<10;i++){

                         Person person = new Person();

                         person.setPid(i);

                         person.setPname("person"+i);

                         person.setComment("person"+i);

                         personList.add(person);

                  }

                  ActionContext.getContext().put("personList", personList);

                  return "ognlListLength";

           }

    successOgnlTag.jsp

    测试集合的长度:<br>

    <s:property value="#personList.size"/>

    直接写一个集合:<br>

    <s:iterator value="{1,2,3,4}">

           <s:property/>

    </s:iterator>

        11  Ognl操作Map

        

        

    Java代码

    Ognl表达式

    map.get(“foo”)

    Map[‘foo’]

    Map.get(new Integer(1));

    Map[1]

    User user = (User)map.get(“user”);

    Return user.getName()

    Map[‘user’].name

    Map.size()

    Map.size

    Map.isEmpty()

    Map.isEmpty

    Map.get(“foo”)

    Map.foo

    Map map = new HashMap();

    Map.put(“foo”,”bar”);

    Map.put(“1”,”2”);

    Return map;

    #{“foo”:”bar”,”1”:”2”}

    Map map = new HashMap();

    Map.put(new Integer(1),”a”);

    Map.put(new Integer(2),”b”);

    Map.put(new Integer(3),”c”);

    #{1:”a”,2:”b”,3:”c”}

        

    testOgnlTag.jsp

    写一个map集合:<br>

                  <s:iterator value="#{1:'a',2:'b',3:'c'}">

                         <!--

                                获得map中的keyvalue

                          -->

                         <s:property value="value"/>

                         <s:property value="key"/>

                         <s:debug></s:debug>

                  </s:iterator>

                  <br>

                  <s:iterator value="#{1:'a',2:'b',3:'c'}" var="map">

                         <!--

                                获得map中的keyvalue

                          -->

                         <s:property value="value"/><br>

                         <s:property value="key"/><br>

                         <s:property value="#map.value"/><br>

                  </s:iterator>

        12.  UI标签

        说明

        UI标签在html文件中表现为一个表单元素。

        使用struts2的ui标签有如下好处:

        可以停止表单的回显

        对页面停止布局和排版

        标签的属性可以被赋值为一个静态的值,或者一个OGNL表达式。

      Form标签

     说明

        id属性为s:form的唯一标识。可以用document.getElementById()获得。

        name属性为s:form的名字,可以用document.getElementsById()获得。

        在默认情况下,s:form将表现表格的形式。

        Textfield标签

     说明

        实际上相当于在表格中多了一行,在这行中多了两列。其变化从上述图中可以很明显的看出。

        Password标签

        说明

        如果不加showPassword属性,则密码不会表现,把showPassword属性的值设置为true,就能表现密码。

        Hidden标签

     说明

        Hidden标签并没有加tr和td

        Submit标签

        (1)、情况一

       a.说明一

        这种情况为submit的type属性为submit类型。

        (2)、情况二:

       b.说明二

        这种情况submit的type属性为button.

        (3)、情况三

       c.说明三

        该type类型为image。

        (4)、综合

        以上三种情况说明,当type为submit、button、image都能完成提交。

          Reset标签

      Textarea标签

        Checkbox标签

        如果集合为list

        <s:checkboxlist name="list" list="{'Java','.Net','RoR','PHP'}" value="{'Java','.Net'}"/>

        生成如下html代码:

        <input type="checkbox" name="list" value="Java" checked="checked"/><label>Java</label>

        <input type="checkbox" name="list" value=".Net" checked="checked"/><label>.Net</label>

        <input type="checkbox" name="list" value="RoR"/><label>RoR</label>

        <input type="checkbox" name="list" value="PHP"/><label>PHP</label>

        如果集合为MAP

        <s:checkboxlist name="map" list="#{1:'瑜珈用品',2:'户外用品',3:'球类',4:'自行车'}" listKey="key" listValue="value" value="{1,2,3}"/>

        生成如下html代码:

        <input type="checkbox" name="map" value="1" checked="checked"/><label>瑜珈用品</label>

        <input type="checkbox" name="map" value="2" checked="checked"/><label>户外用品</label>

        <input type="checkbox" name="map" value="3" checked="checked"/><label>球类</label>

        <input type="checkbox" name="map" value="4"/><label>自行车</label>

        Checkboxlist标签

        如果集合里存放的是javabean

     <%

      Person person1 = new Person(1,"第一个");

      Person person2 = new Person(2,"第二个");

      List<Person> list = new ArrayList<Person>();

      list.add(person1);

      list.add(person2);

      request.setAttribute("persons",list);

      %>

        <s:checkboxlist name="beans" list="#request.persons" listKey="personid" listValue="name"/>

        Personid和name为Person的属性

        生成如下html代码:

        <input type="checkbox" name=“beans" value="1"/><label>第一个</label>

        <input type="checkbox" name=“beans" value="2"/><label>第二个</label>

        (1)、 集合为list

        (2)、集合为map

        listKey相当于<input type=”checkbox”>中的value,listValue相当于label的表现值。

        (3)、List中是javabean

        13标签回显

        BackValueAction:

        private String username = "aaa";

        private String password = "bbb";

        public String getUsername() {

               return username;

        }

        public void setUsername(String username) {

               this.username = username;

        }

        public String getPassword() {

               return password;

        }

        public void setPassword(String password) {

               this.password = password;

        }

         

        Backvalue.jsp

        
     

        14.radio标签的使用和checkboxlist复选框相同。

        如果集合里存放的是javabean(personid和name为Person的属性)

        < s:radio name="beans" list="#request.persons" listKey="personid" listValue="name"/>

        生成如下html代码:

        <input type="radio" name="beans" id="beans1" value="1"/><label>第一个</label>

        <input type="radio" name="beans" id="beans2" value="2"/><label>第二个</label>

        如果集合为MAP

        <s:radio name="map" list="#{1:'瑜珈用品',2:'户外用品',3:'球类',4:'自行车'}" listKey="key" listValue="value“ value="1"/>

        生成如下html代码:

        <input type="radio" name="map" id="map1" value="1"/><label for="map1">瑜珈用品</label>

        <input type="radio" name="map" id="map2" value="2"/><label for="map2">户外用品</label>

        <input type="radio" name="map" id="map3" value="3"/><label for="map3">球类</label>

        <input type="radio" name="map" id="map4" value="4"/><label for="map4">自行车</label>

        如果集合为list

        <s:radio name="list" list="{'Java','.Net'}" value="'Java'"/>

        生成如下html代码:

        <input type="radio" name="list" checked="checked" value="Java"/><label>Java</label>

        <input type="radio" name="list" value=".Net"/><label>.Net</label>

        15. 表单标签_select下拉选择框

        <s:select name="list" list="{'Java','.Net'}" value="'Java'"/>

        <select name="list" id="list">

        <option value="Java" selected="selected">Java</option>

        <option value=".Net">.Net</option>

        </select>

        <s:select name="beans" list="#request.persons" listKey="personid" listValue="name"/>

        <select name="beans" id="beans">

        <option value="1">第一个</option>

        <option value="2">第二个</option>

        </select>

        <s:select name="map" list="#{1:'瑜珈用品',2:'户外用品',3:'球类',4:'自行车'}" listKey="key" listValue="value" value="1"/>

        <select name="map" id="map">

        <option value="1" selected="selected">瑜珈用品</option>

        <option value="2">户外用品</option>

        <option value="3">球类</option>

        <option value="4">自行车</option>

        </select>

        16. <s:token />标签防止重复提交

        <s:token />标签防止重复提交,用法如下:

        第一步:在表单中加入<s:token />

        <s:form action="helloworld_other" method="post" namespace="/test">

      <s:textfield name="person.name"/><s:token/><s:submit/>

      </s:form>

        第二步:

        <action name="helloworld_*" class="cn.csdn.action.HelloWorldAction" method="{1}">

           <interceptor-ref name="defaultStack" />

            <interceptor-ref name="token" />

            <result name="invalid.token">/WEB-INF/page/message.jsp</result> 

            <result>/WEB-INF/page/result.jsp</result>

        </action>

        以上配置加入了“token”拦截器和“invalid.token”结果,因为“token”拦截器在会话的token与请求的:

        严重: ParametersInterceptor - [setParameters]: Unexpected Extoken不一致时,将会直接返回“invalid.token”结果。

        在debug状态,控制台出现下面信息,是因为Action中并没有struts.token和struts.token.name属性,我们不用关心这个错误ception caught setting 'struts.token' on 'class xxx: Error setting expression 'struts.token' with value '[Ljava.lang.String;@39f16f'

        严重: ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'struts.token.name'

    文章结束给大家分享下程序员的一些笑话语录: 一个程序员对自己的未来很迷茫,于是去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝说"我的孩子,你去问Lippman,他现在领导的程序员的队伍可能是地球上最大的"
    于是他去问Lippman。
    Lippman说"程序员的未来就是驾驭程序员"
    这个程序员对这个未来不满意,于是他又去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝说"我的孩子,你去问Gates,他现在所拥有的财产可能是地球上最多的"
    于是他去问Gates。
    Gates说"程序员的未来就是榨取程序员"
    这个程序员对这个未来不满意,于是他又去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"
    于是他去问侯捷。
    侯捷说"程序员的未来就是诱惑程序员"
    这个程序员对这个未来不满意,于是他又去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝摇摇头"唉,我的孩子,你还是别当程序员了")

  • 相关阅读:
    Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾
    Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾
    atitit.极光消息推送服务器端开发实现推送  jpush v3. 总结o7p
    atitit.极光消息推送服务器端开发实现推送  jpush v3. 总结o7p
    Atitit.文件搜索工具 attilax 总结
    Atitit.文件搜索工具 attilax 总结
    Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package
    Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package
    Atitit..状态机与词法分析  通用分词器 分词引擎的设计与实现 attilax总结
    Atitit..状态机与词法分析  通用分词器 分词引擎的设计与实现 attilax总结
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3086027.html
Copyright © 2011-2022 走看看