Struts2框架之标签库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ip_JL/article/details/82807827

标签库:

分支判断:

动作类部分代码:

public String execute() throws Exception {
        
        ActionContext context = ServletActionContext.getContext();
        context.put("age", 20);
        return super.execute();
    }

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<!-- test的值依然是使用ognl表达式
 -->
    <s:if test="#age < 16">
        <s:property value="'小孩'"/>
    </s:if>
    <s:elseif test="#age < 18 && #age >= 16">
        <s:property value="'未成年'"/>
    </s:elseif>
    <s:else>
        <s:property value="'成年人'"/>
    </s:else>
</body>
</html>

访问结果:

循环:

动作类部分代码:

public String execute() throws Exception {
        
        ActionContext context = ServletActionContext.getContext();
        String[] attStr = {"1", "张三", "2"};
        context.put("attStr", attStr);
        return super.execute();
    }

success.jsp部分代码:

<h4>循环</h4>
    <!-- 
        value: 从ActionContext中获取的集合/数组变量名
        var: 自定义的遍历变量名
     -->
    <s:iterator value="attStr" var="str">
        <s:property value="#str"/>
    </s:iterator>

访问结果:

动作类代码:

public String execute() throws Exception {
        
        ActionContext context = ServletActionContext.getContext();
        List<String> strList = new ArrayList<String>();
        strList.add("2");
        strList.add("王五");
        strList.add("1");
        context.put("strList", strList);
        return super.execute();
    }

success.jsp部分代码:

<!-- 从集合中取值 -->
    <s:iterator value="strList" var="str">
        <s:property value="#str"/>
    </s:iterator>

访问结果:

动作类部分代码:

Map<String, Object> map = new HashMap<String, Object>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        context.put("map", map);

success.jsp部分代码:

<hr>
    <!-- 从map取值 -->
    <s:iterator value="map" var="mapObj">
        <s:property value="#mapObj.key"/>--><s:property value="#mapObj.value"/><br>
    </s:iterator>

访问结果:

status属性可以给循环设置参数:

    变量:

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

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

        first: 是否第一行

        last: 是否最后一行

        odd: 是否奇数

        even: 是否偶数

        begin: 从数字几开始

        end: 到数字几结束

        step: 步长

动作类部分代码:

List<Person> personList = new ArrayList<Person>();
        for(int i = 0; i < 10; i++){
            Person person = new Person();
            person.setPersonId(i);
            person.setGender(1);
            person.setPersonName("赵六"+i);
            personList.add(person);
        }
        context.put("personList", personList);

success.jsp部分代码:

<!-- 从对象集合中取值 -->
    <hr>
    <table border="1">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>gender</th>
            <th>索引</th>
            <th>序号</th>
            <th>是否首行</th>
            <th>是否尾行</th>
            <th>奇数</th>
            <th>偶数</th>
        </tr>
        <s:iterator value="personList" var="person" status="status">
            <tr bgcolor="${status.odd?'#c3f3c3':'#f3c3f3'}">
                <td><s:property value="#person.personId"/></td>
                <td><s:property value="#person.personName"/></td>
                <td><s:property value="#person.gender == 1?'男':'女'"/></td>
                <td><s:property value="#status.index"/></td>
                <td><s:property value="#status.count"/></td>
                <td><s:property value="#status.first"/></td>
                <td><s:property value="#status.last"/></td>
                <td><s:property value="#status.odd"/></td>
                <td><s:property value="#status.even"/></td>
            </tr>
        </s:iterator>
    </table>

访问结果:

普通输出:

动作类部分代码:

public String execute() throws Exception {
        
        ActionContext context = ServletActionContext.getContext();
        context.put("name", "郑十");
        return super.execute();
    }

success.jsp部分代码:

<!-- 普通输出标签 -->
    <s:property value="name"/>
    <s:property value="name1" default="空值"/>
    <s:property value="'<a href>郑十</a>'" escapeHtml="false"/>

访问结果:

日期输出:

<s:date>

    属性:

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

        format: 要展示的日期的格式

动作类部分代码:

context.put("ctime", new Date());

success.jsp部分代码:

<!-- 日期输出标签 -->
    <s:date name="#ctime" format="yyyy-MM-dd HH:mm:ss:SSS"/>

访问结果:

页面动态包含:

<s:action>

    属性:

        name: 要请求的action

        executeResult: 是否展示action的执行结果, true 展示, false不展示

动作类PersonAction1代码:

package com.rl.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

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

success.jsp部分代码:

<!-- 页面的包含 -->
    <s:action name="person1" executeResult="true"></s:action>

success.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <s:property value="#name"/>
</body>
</html>

struts.xml部分代码:

<action name="person1" class="com.rl.action.PersonAction1">
            <result name="success">/success1.jsp</result>
        </action>

访问结果:

超链接标签:

<s:a>

    属性:

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

        <s:param>

            是给a链接赋值参数, 可以自动对中文进行编码

success.jsp部分代码:

<!-- 超链接a标签 -->
    <s:a action="person/person">我是a链接
        <s:param name="username" value="'张三'"></s:param>
    </s:a>

动作类部分代码:

public String execute() throws Exception {
        
        System.out.println(username);
        return super.execute();
    }

访问结果:

链接的属性: 对中文做了编码, 在控制台输出会自动解码

http://localhost:8080/struts2_18/person/person.action;jsessionid=E635CF8ABA8156335F588B03FDDB3FBD?username=%E5%BC%A0%E4%B8%89

控制台输出:

猜你喜欢

转载自blog.csdn.net/ip_JL/article/details/82807827