• struts2标签


    $用于i18n和struts2配置文件

    #用于取得ActionContext的值

    %将原本的文本属性解析为ognl,对于本来就是ognl的属性不起作用

    struts.xml简单配置

    <package name="tags" namespace="/" extends="struts-default">
            <action name="tags" class="com.ouc.wkp.action.TagsAction">
                <result>/tags.jsp</result>
            </action>
        </package>
    struts.xml
    package com.ouc.wkp.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class TagsAction extends ActionSupport {
        private String password;
    
        private String username;
    
        public TagsAction() {
        }
    
        public String execute() {
            this.addFieldError("fielderror.test", "wrong!");
            return SUCCESS;
        }
    
        public String getPassword() {
            return password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    }
    TagsAction.java

    然后下面的jsp展示了struts标签的用法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'tags.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        This is my tags.jsp
        <br> property:
        <s:property value="username" />
        <br /> property取值为字符串:
        <s:property value="'username'" />
        <br /> property设定默认值:
        <s:property value="admin" default="管理员" />
        <br /> property设定HTML:
        <s:property value="'<hr>'" />
        <br /> property设定HTML:
        <s:property value="'<hr>'" escape="false" />
        <br />
    
        <br /> set设定adminName值(默认为request和ActionContext):
        <s:set var="adminName" value="username"></s:set>
        <br /> set从request取值:
        <s:property value="#request.adminName" />
        <br /> set从ActionContext取值:
        <s:property value="#adminName" />
        <br /> set设定范围:
        <s:set var="adminPassword" value="password" scope="page"></s:set>
        <br /> set从相应范围取值:<%=pageContext.getAttribute("adminPassword")%><br />
    
        set设定scope,范围为session:
        <s:set var="adminPassword" value="password" scope="session"></s:set>
        <br /> set使用#取值:
        <s:property value="#adminPassword" />
        <br> set从相应范围取值:
        <s:property value="#session.adminPassword" />
        <br />
    
        <br /> bean定义bean,并使用param设定新的属性值
        <s:bean name="com.ouc.wkp.model.Dog">
            <s:param name="name" value="'MZ'"></s:param>
            <s:property value="name" />
            <br />
        </s:bean>
    
        bean 查看debug情况:
        <s:bean name="com.ouc.wkp.model.Dog" var="myDog">
            <s:param name="name" value="'oudy'"></s:param>
        </s:bean>
        拿出值:
        <s:property value="#myDog.name" />
        <br />
    
        <s:fielderror name="fielderror.test" theme="simple"></s:fielderror>
        <br /> if elseif else: age=
        <s:property value="#parameters.age[0]" />
        <br />
        <s:set var="age" value="#parameters.age[0]"></s:set>
        <s:if test="#age < 0">wrong age!</s:if>
        <s:elseif test="#parameters.age[0]<20">too young!</s:elseif>
        <s:else>yes!</s:else>
    
        <s:if test="#parameters.aaa==null">null</s:if>
        <s:debug></s:debug>
        <br /> 遍历集合
        <br />
        <s:iterator value="{1,2,3}">
            <s:property />|
         </s:iterator>
        <br /> 自定义变量
        <br />
        <s:iterator value="{'aaa','bbb','ccc'}" var="x">
            <s:property />
            <s:property value="#x.toUpperCase()" />
        </s:iterator>
        <br /> 使用status:
        <br />
        <s:iterator value="{'aaa','bbb','ccc'}" status="status">
            <s:property />
             遍历过的元素总数:<s:property value="#status.count" />|
             遍历过的元素索引:<s:property value="#status.index" />|
             当前是偶数?:<s:property value="#status.even" />
             当前是奇数?:<s:property value="#status.odd" />
             是第一个元素吗?:<s:property value="#status.first" />
             是最后一个元素吗?:<s:property value="#status.last" />
            <br />
            
            遍历Map 方法1
            <s:iterator value="#{1:'a',2:'b',3:'c'}">
                <s:property value="key" />|<s:property value="value" />
                <br />
            </s:iterator>
            方法2
            <s:iterator value="#{1:'a',2:'b',3:'c'}" var="x">
                <s:property value="#x.key" />|<s:property value="#x.value" />
                <br />
            </s:iterator>
    
        </s:iterator>
    
    </body>
    </html>
    tags.jsp

    ui标签感觉不太实用

  • 相关阅读:
    1208C Magic Grid
    jsc2019_qualE Card Collector
    jsc2019_qualD Classified
    jsc2019_qualC Cell Inversion
    牛客提高D6t3 分班问题
    牛客提高D6t2 破碎的序列
    牛客提高D6t1 积木大赛
    loj6259「CodePlus 2017 12 月赛」白金元首与独舞
    p4208 [JSOI2008]最小生成树计数
    p4111 [HEOI2015]小Z的房间[简述矩阵树定理]
  • 原文地址:https://www.cnblogs.com/wangkaipeng/p/5738520.html
Copyright © 2020-2023  润新知