struts2标签库详解

1.分支判断

首先要引入struts2的标签库

<%@taglib uri="/struts-tags" prefix="s" %>
<!--if elseif else使用 内部是ognl的表达式-->
<s:if test="#age < 16">
  <s:property value="小孩"/>
</s:if>
<s:elseif test="#age >= 16 && #age < 18">
  <s:property value="未成年"/>
</s:elseif>
<s:else>
  <s:property value="已成年"/>
</s:else>

2.循环

使用<s:iterator>标签

 属性

        value是从ActionContext中获取的集合,不需要#

        var:是每次从集合中取值赋值的变量

        使用的时候需要加上#

动作类:

@Override
public String execute() throws Exception {
    ActionContext ac = ServletActionContext.getContext();
    // 在contextmap存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是第一次请求
    String[] arrStr = {"aa", "bb", "cc", "dd"};
    ac.put("arrStr" , arrStr);
    return super.execute();
}

<s:iterator value="arrStr" var="str">
  <s:property value="#str"/>
</s:iterator>
  • 字符串数组的循环
public class PersonAction2 extends ActionSupport implements ModelDriven<Person> {
    @Override
    public String execute() throws Exception {
        ActionContext ac = ServletActionContext.getContext();
        // 在contextmap存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是第一次请求
        String[] arrStr = {"aa", "bb", "cc", "dd"};
        ac.put("arrStr" , arrStr);
        return super.execute();
    }
   
    @Override
    public Person getModel() {
        return person;
    }
}

jsp界面

<h3>遍历字符串数组</h3>
<s:iterator value="arrStr" var="str">
  <s:property value="#str"/>
</s:iterator>
  • list循环
public class PersonAction2 extends ActionSupport implements ModelDriven<Person> {
    @Override
    public String execute() throws Exception {
        ActionContext ac = ServletActionContext.getContext();
        // 在contextmap存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是第一次请求
        List<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("张三");

        ac.put("list" , list);
        return super.execute();
    }


    @Override
    public Person getModel() {
        return person;
    }
}

jsp界面

<h3>遍历list</h3>
<s:iterator value="list" var="item">
  <s:property value="#item"/>
</s:iterator>
  • map遍历
public class PersonAction2 extends ActionSupport implements ModelDriven<Person> {
     public String execute() throws Exception {
        ActionContext ac = ServletActionContext.getContext();
        // 在contextmap存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是第一次请求
        HashMap<String, Object> map = new HashMap<>();
        map.put("key1", "xxx");
        map.put("key2", "xxx");
        map.put("key3", "xxx");
        map.put("key4", "xxx");
        map.put("key5", "xxx");
        ac.put("map" , map);
        return super.execute();
    }
    @Override
    public Person getModel() {
        return person;
    }
}

jsp界面:

<h3>遍历map</h3>
<s:iterator value="map" var="obj">
  <s:property value="#obj.key"/> ->>>>>>      <s:property value="#obj.value"/>
</s:iterator>
  • 循环对象集合
public class PersonAction2 extends ActionSupport implements ModelDriven<Person> {
    private String username;

    private Person person = new Person();

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    @Override
    public String execute() throws Exception {
        ActionContext ac = ServletActionContext.getContext();
        // 在contextmap存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是第一次请求
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Person person = new Person(i, "长萨", 1);
            list.add(person);
        }
        ac.put("personList", list);
        return super.execute();
    }

    @Override
    public Person getModel() {
        return person;
    }
}

jsp界面:

<h3>循环对象集合</h3>
<table border="1">
  <tr>
    <th>id</th>
    <th>姓名</th>
    <th>性别</th>
  </tr>
  <s:iterator value="personList" var="peronxx" status="status">
    <tr>
      <td><s:property value="#peronxx.personId"></s:property> </td>
      <td><s:property value="#peronxx.pName"></s:property> </td>
      <td><s:property value="#peronxx.gender"></s:property> </td>
    </tr>
  </s:iterator>
</table>

jsp里面status属性可以给循环设置参数

变量

       index:当前循环的索引号,从0开始

       count:当前循环的顺序号,从1开始

       first:是否是第一行

       last:是否是最后一行

       odd:是否是奇数

       even:是否是偶数

       begin:从数字几开始

       end:到数字几结束

       step:步长

<h3>循环对象集合</h3>
<table border="1">
  <tr>
    <th>id</th>
    <th>姓名</th>
    <th>性别</th>
     <th>索引</th>
     <th>序号</th>
    <th>是否首行</th>
    <th>是否未行</th>
    <th>是否奇数</th>
    <th>是否偶数</th>
  </tr>
  <s:iterator value="personList" var="peronxx" status="status">
    <tr bgcolor='<s:property value="#status.odd ? '#c3f3c3' : '#f3c3f3'"/>'>
      <td><s:property value="#peronxx.personId"></s:property> </td>
      <td><s:property value="#peronxx.pName"></s:property> </td>
      <td><s:property value="#peronxx.gender == 1 ? '男' : '女'"></s:property> </td

      <td><s:property value="#status.index"></s:property> </td>
      <td><s:property value="#status.count"></s:property> </td>
      <td><s:property value="#status.first"></s:property> </td>
      <td><s:property value="#status.last"></s:property> </td>
      <td><s:property value="#status.odd"></s:property> </td>
      <td><s:property value="#status.even"></s:property> </td>
    </tr>
  </s:iterator>
</table>

 

3.输出标签<s:property>

属性

     value:用于通过ognl表达式来取值

     default:如果value值是空就给一个默认值     escapehtml:是否被浏览器解析,默认是true,不解析,false是解析

<s:property value="#name1" default="空默认值"/>
<s:property value="'<a href>百度</a>'" escapeHtml="false"/>

4.日期输出标签<s:date>

属性

     name:取日期的ognl的表达式的值

     format:要展示的日期的格式

<h5>日期输出标签</h5>
<s:date name="#ctime" format="yyyy-MM-dd"/>

action动作类:

public class PersonAction2 extends ActionSupport implements ModelDriven<Person> {
    @Override
    public String execute() throws Exception {
        ActionContext ac = ServletActionContext.getContext();
        ac.put("name", "张三");
        ac.put("ctime", new Date());
        return super.execute();
    }

    @Override
    public Person getModel() {
        return person;
    }
}

5.页面动态包含标签<s:action>

个人见解,类似于页面的嵌套

属性

     name:要请求的action

     executeResult:是否展示action的执行结果,true展示,false不展示Action动作类(记得配置当前动作类):

public class PersonAction3 extends ActionSupport{
    @Override
    public String execute() throws Exception {
        System.out.println("我被包含");
        ServletActionContext.getContext().put("name", "我是被包含的结果");
        return super.execute();
    }
}

包含界面:

<h3>页面的包含</h3>
<s:action name="person3" executeResult="true"/>

被包含界面(就拿了个值然后被嵌套界面所包含):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <s:property value="#name"/>
  </body>
</html>

6.超链接标签<s:a/>

属性

     action:要链接的动作类的名称,该标签会在Action的之后面自动加上后缀。

     <s:param>是该标签内部的元素,param主要是给该链接赋予参数的,可以自动的对中文进行编码。

<s:a action="person2">
  I a internet
  <s:param name="username" value="'张三'"/>
</s:a>

到这里之后struts2算是告一段落了,接下来就是spring>hibernate了。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值