• Strut2开发经验总结


    1、如何在html静态页面中使用struts

    tomcat目录/conf/web.xml 文件中,找到


    <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

    在其下面加入:

    <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    这样tomcat就会把后缀名为html的文件当成jsp来解析。

    2、Struts2通用标签

    Struts2框架为我们提供了很多标签,这些标签总体上可以分为两类:通用标签和UI标签。而较常使用的又是通用标签,所以文章的内容也是围绕通用标签展开的。通用标签分为两类:数据标签和控制标签。数据标签用于访问值栈中数据,控制标签用于控制呈现页面时数据执行流程。如果需要使用Struts 2框架的标签库,需要在页面的首部引入以下代码:<%@ taglib uri="/struts-tags" prefix="s"%>下面将分别对这两种标签进行说明。

    数据标签

    数据标签主要用于访问ActionContext和值栈中数据,数据标签包括:

    • property
    • set
    • push
    • param
    • bean
    • action
    • include
    • url
    • a
    • i18n
    • text
    • date
    • debug

    1、property标签

    property标签用于输出值栈中对象的属性值,value属性就是要输出的值。如果没有执行value属性,那么默认就会输出栈顶对象

    property标签的属性包括:

    名称是否必需说明
    default 如果value属性是null,则输出default的值
    value 进行求值的表达式

    2、set标签

    set标签是将一个值赋给指定范围的变量。如下前面的OGNL表达式文章中就多次使用了set标签。set标签的属性包括:

    名称是否必需说明
    name 变量的名字
    value 指定一个表达式,将计算的结果赋给变量。如果没有执行value属性,默认将栈顶对象赋给变量
    scope 变量的范围,有application、session、request、page和action(默认值)

    注意的是,在使用默认scope的值,即action范围的时候,value属性值会保存在request和OgnlContext(OGNL上下文)中。比如下面的例子:

    action:

    package com.rhwayfun.struts2.action;
    
    import com.rhwayfun.struts2.bean.User;
    
    public class SetTagAction {
    
        private User user;
        public String execute(){
            user = new User();
            user.setName("刘备");
            return "success";
        }
    
        public User getUser() {
            return user;
        }
    }
    
    •  
       1 package com.rhwayfun.struts2.action;
       2 
       3 import com.rhwayfun.struts2.bean.User;
       4 
       5 public class SetTagAction {
       6 
       7     private User user;
       8     public String execute(){
       9         user = new User();
      10         user.setName("刘备");
      11         return "success";
      12     }
      13 
      14     public User getUser() {
      15         return user;
      16     }
      17 }

    jsp:

    <%@ 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>set标签测试</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
      </head>
    
      <body>
        <p>action范围中获取值</p>
        <p>
            <s:set name="name" value="user.name"></s:set>
            <s:property value="#name"/>
        </p>
        <p>request范围中获取</p>
        <p>
            <s:property value="#request.name"/>
        </p>
        <p>session范围中获取</p>
        <p>
            <s:set name="name" value="user.name" scope="session"></s:set>
            <s:property value="#session.name"/>
        </p>
      </body>
    </html>
    
    •  1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
       2 <%@ taglib uri="/struts-tags" prefix="s" %>
       3 <%
       4 String path = request.getContextPath();
       5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
       6 %>
       7 
       8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
       9 <html>
      10   <head>
      11     <base href="<%=basePath%>">
      12     <title>set标签测试</title>
      13     <meta http-equiv="pragma" content="no-cache">
      14     <meta http-equiv="cache-control" content="no-cache">
      15     <meta http-equiv="expires" content="0">    
      16   </head>
      17 
      18   <body>
      19     <p>action范围中获取值</p>
      20     <p>
      21         <s:set name="name" value="user.name"></s:set>
      22         <s:property value="#name"/>
      23     </p>
      24     <p>request范围中获取</p>
      25     <p>
      26         <s:property value="#request.name"/>
      27     </p>
      28     <p>session范围中获取</p>
      29     <p>
      30         <s:set name="name" value="user.name" scope="session"></s:set>
      31         <s:property value="#session.name"/>
      32     </p>
      33   </body>
      34 </html>

    最后的测试结果: 
    结果

    3、push标签

    push标签的作用是将一个对象放入值栈中,所以push对象位于栈顶,但是push标签结束之后,栈顶的元素将会被删除,因此需要访问push的对象需要在push标签的内部访问。push标签只有属性value,这也是一个必需提供的属性。那么具体在开发中push标签有什么作用呢?答案是简化对属性的访问。比如需要访问一个User对象的name属性和age属性,正常在jsp页面使用Struts 2标签库进行访问的方式是#request.user.name#request.user.age(假设是request范围),而使用push标签,访问name属性的方式是:

    <s:push value="#request.user">
        姓名:<s:property value="name"/>
        年龄:<s:property value="age"/>
    </s:push>
    1 <s:push value="#request.user">
    2     姓名:<s:property value="name"/>
    3     年龄:<s:property value="age"/>
    4 </s:push>

    4、param标签

    param标签通常是作为其他标签的子标签,用于为其他标签提供参数。param标签有两个非必需属性name和value。其中的name属性是要设置参数的名字,value则是该参数的值。使用param标签有两种方式:<param name="username" value="zhangsan"/><param name="username">zhangsan</param>。在第一种方式中,value属性并不会作为name属性的值,而是表达式对待,因此如果zhangsan属性是空的话,将得到null ,所以需要把value改成%{'zhangsan'}。第二种访问方式是没有什么问题的。所以这种方式也是推荐的写法。

    5、bean标签

    bean标签相当于jsp中的useBean标签,bean标签通常用于实例化一个JavaBean对象,一个bean标签内部可以有多个param标签,用于为bean实例设置属性值(要求有set方法)。bean标签有两个属性:name和id。其中的name属性是要实例化的bean的完整类名,id用于指定一个名字,用于引用放入OgnlContext中的JavaBean对象。这里需要注意的是,出了bean标签,放入栈顶的实例(用bean标签创建的JavaBean对象会放入栈顶)会被移除。但是如果指定了id属性,在bean标签的外部就可以访问了。此时需要使用#符号。因此如果需要创建在bean实例可以在bean标签的外部也可以访问到,就需要指定id属性。

    6、action标签

    通过指定action的名字和可选的命名空间,action标签允许在jsp页面直接访问action。如果将action标签的executeResult属性设置为true,那么action对应的输出结果也会包含在本页面中。由于这种方式并不常用,而且在实际开发中使用的场合比较少,这里就不详细说明了。

    7、include标签

    该标签类似于jsp中<jsp:include></jsp:include>标签。在标签的内部可以包含多个param标签,用于向被包含的页面传递参数。include标签只有一个value属性,用于指定被包含的jsp或者Servlet。比如下面的代码:

    includeTag.jsp

    <%@ 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>include标签测试</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
      </head>
    
      <body>
        <p>使用include标签包含另一个页面,并传递参数</p>
        <p>
            <s:include value="includeJsp.jsp">
                <s:param name="username">刘备</s:param>
                <s:param name="age">52</s:param>
            </s:include>
        </p>
      </body>
    </html>
    
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     <title>include标签测试</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16   </head>
    17 
    18   <body>
    19     <p>使用include标签包含另一个页面,并传递参数</p>
    20     <p>
    21         <s:include value="includeJsp.jsp">
    22             <s:param name="username">刘备</s:param>
    23             <s:param name="age">52</s:param>
    24         </s:include>
    25     </p>
    26   </body>
    27 </html>

    includeJsp.jsp:

    <%@ 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>includeJsp.jsp</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
      </head>
    
      <body>
        姓名:${param.username }<br>
       年龄:${param.age }
      </body>
    </html>
    
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     <title>includeJsp.jsp</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16   </head>
    17 
    18   <body>
    19     姓名:${param.username }<br>
    20    年龄:${param.age }
    21   </body>
    22 </html>

    最后的访问includeTag.jsp,可以看到如下的结果:

    结果

    8、其他标签

    其中的url标签用于创建一个url,在标签内部可以提供url附加的参数信息。i18n标签和text标签主要用于国际化的支持。i18n标签把一个资源包放入值栈中,text标签用于从资源包中获取消息。date标签主要用于格式化日期的输出。

    控制标签

    控制标签用于在呈现页面的时候控制程序的执行流程。常用的控制标签包括:

    • if/elseif/else
    • iterator
    • generator

    1、if/elseif/else 标签

    下面是这个标签使用的一个例子:

    <%@ 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>if/elseif/else标签</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
      </head>
    
      <body>
        <s:set name="score" value="75"></s:set>
            成绩等级:
        <s:if test="#score < 60">不及格</s:if>
        <s:elseif test="#score < 80">良好</s:elseif>
        <s:else>优秀</s:else>
      </body>
    </html>
    
    <%@ 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>if/elseif/else标签</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
      </head>
    
      <body>
        <s:set name="score" value="75"></s:set>
            成绩等级:
        <s:if test="#score < 60">不及格</s:if>
        <s:elseif test="#score < 80">良好</s:elseif>
        <s:else>优秀</s:else>
      </body>
    </html>
    

      

    2、iterator标签

    iterator标签用于迭代一个集合的元素,在迭代的过程中,会暂时把迭代的对象压入栈顶,这样在标签的内部可以访问对象的属性值了。访问结束之后,会把栈顶的迭代对象移除,并把下一次迭代的对象压入栈顶。这样一直到所有的对象迭代完毕。iterator标签有三个属性:id、value和status。id属性几乎不使用,value属性用于指定迭代的集合,status属性可以获取迭代中的一个状态信息(比如索引值、是否是奇数、偶数等),其所有的方法如下:

    • public int getCount():得到迭代元素的总数
    • public int getIndex():得到当前迭代元素的索引
    • public boolean isEven():判断当前迭代的元素的顺序是否是偶数
    • public boolean isOdd():判断当前迭代的元素的顺序是否是奇数
    • public boolean isFirst():判断当前迭代的元素是否是第一个元素
    • public boolean isLast():判断当前迭代的元素是否是最后一个元素

    比如下面的例子:

    <%@ 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>iterator标签测试</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
    
      </head>
    
      <body>
        <s:iterator value="{'刘备','张飞','关羽'}" status="s">
            <p>当前元素:<s:property /></p>
            <p>当前元素总数:<s:property value="#s.count"/></p>
            <p>当前元素索引:<s:property value="#s.index"/></p>
            <p>当前元素是否为偶数:<s:property value="#s.even"/></p>
            <p>当前元素是否是奇数:<s:property value="#s.odd"/></p>
            <p>当前元素是否是第一个数:<s:property value="#s.first"/></p>
            <p>当前元素是否是最后一个数:<s:property value="#s.last"/></p>
            <hr>
        </s:iterator>
      </body>
    </html>
    
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     <title>iterator标签测试</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16 
    17   </head>
    18 
    19   <body>
    20     <s:iterator value="{'刘备','张飞','关羽'}" status="s">
    21         <p>当前元素:<s:property /></p>
    22         <p>当前元素总数:<s:property value="#s.count"/></p>
    23         <p>当前元素索引:<s:property value="#s.index"/></p>
    24         <p>当前元素是否为偶数:<s:property value="#s.even"/></p>
    25         <p>当前元素是否是奇数:<s:property value="#s.odd"/></p>
    26         <p>当前元素是否是第一个数:<s:property value="#s.first"/></p>
    27         <p>当前元素是否是最后一个数:<s:property value="#s.last"/></p>
    28         <hr>
    29     </s:iterator>
    30   </body>
    31 </html>

    3、generator标签

    generator标签主要是根绝分隔符对元素进行处理,该标签一般和iterator标签使用,在generator标签的内部可以使用iterator标签对处理之后的元素进行迭代,下面是一个例子:

    <%@ 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>generator标签</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">
      </head>
    
      <body>
        <s:generator separator="," val="'刘备,张飞,关羽'">
            <s:iterator>
                <s:property/>
            </s:iterator>
        </s:generator>
      </body>
    </html>
    
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     <title>generator标签</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17   </head>
    18 
    19   <body>
    20     <s:generator separator="," val="'刘备,张飞,关羽'">
    21         <s:iterator>
    22             <s:property/>
    23         </s:iterator>
    24     </s:generator>
    25   </body>
    26 </html>

    generator标签中上述的separator属性和val属性是必需的,如果指定了id属性,那么将以id属性的值作为key,将生成的迭代器保存在pageContext对象中。










    <------------------------------持续更新中---------------------------------->
  • 相关阅读:
    ArcGIS Engine要素渲染和专题图制作(转)
    网络换行符替换为word换行符
    栅格数据处理 RasterDataset RasterLayer Raster RasterBandCollection
    ISurfaceOp 接口生成等高线
    小女孩动画
    pytest 知识点学习
    pytest+python 项目初步实践及生成allure报告
    docker安装python项目
    windows 10 专业版 安装docker desktop
    selenium+python自动化测试--解决无法启动IE浏览器及报错问题
  • 原文地址:https://www.cnblogs.com/JOEH60/p/7650064.html
Copyright © 2020-2023  润新知