• Struts系列教材 (十二)- 标签


    JSTL标准标签库c:forEach类似的,struts也提供了一个s:iterator用于遍历一个集合中的数据

    步骤1:先运行,看到效果,再学习
    步骤2:模仿和排错
    步骤3:效果
    步骤4:为ProductAction增加list方法
    步骤5:struts.xml
    步骤6:list.jsp
    步骤7:测试

    步骤 1 : 先运行,看到效果,再学习

    老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

    步骤 2 : 模仿和排错

    在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
    模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
    采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

    推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
    这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
    这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

    步骤 3 : 效果

    访问测试地址:

    http://127.0.0.1:8080/struts/listProduct


    这是通过s:iterator标签遍历一个集合的结果

    效果

    步骤 4 : 为ProductAction增加list方法

    1. 为ProductAction增加一个products属性,类型是List,并提供getter setter 
    2. 为ProductAction增加一个list()方法,为products添加3个product对象,并返回“list"

    package com.how2java.action;

      

    import java.util.ArrayList;

    import java.util.List;

      

    import com.how2java.bean.Product;

      

    public class ProductAction {

        private Product product;

        private List<Product> products;

      

        public List<Product> getProducts() {

            return products;

        }

      

        public void setProducts(List<Product> products) {

            this.products = products;

        }

      

        public String show() {

            product = new Product();

            product.setName("iphone7");

            return "show";

        }

      

        public String add() {

            return "show";

        }

      

        public String list() {

      

            products=new ArrayList();

      

            Product p1 = new Product();

            p1.setId(1);

            p1.setName("product1");

            Product p2 = new Product();

            p2.setId(2);

            p2.setName("product2");

            Product p3 = new Product();

            p3.setId(3);

            p3.setName("product3");

      

            products.add(p1);

            products.add(p2);

            products.add(p3);

      

            return "list";

      

        }

      

        public Product getProduct() {

            return product;

        }

      

        public void setProduct(Product product) {

            this.product = product;

        }

    }

    步骤 5 : struts.xml

    配置路径listProduct,并返回list.jsp

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>

      <constant name="struts.i18n.encoding" value="UTF-8"></constant>

       

      <package name="basicstruts" extends="struts-default">

       

      <action name="showProduct" class="com.how2java.action.ProductAction" method="show">

        <result name="show">show.jsp</result>

      </action>    

      <action name="addProduct" class="com.how2java.action.ProductAction" method="add">

        <result name="show">show.jsp</result>

      </action>    

      <action name="listProduct" class="com.how2java.action.ProductAction" method="list">

        <result name="list">list.jsp</result>

      </action>    

    </package>

    </struts>

    步骤 6 : list.jsp

    使用s:iterator标签进行遍历
    value  表示集合
    var  表示遍历出来的元素
    st  表示遍历出来的元素状态
    st.index 当前行号 基0
    st.count 当前行号 基1
    st.first 是否是第一个元素
    st.last 是否是最后一个元素
    st.odd 是否是奇数
    st.even 是否是偶数

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8" isELIgnored="false"%>

    <%@ taglib prefix="s" uri="/struts-tags"%>

    <style>

    table {

        border-collapse: collapse;

    }

    td {

        border: 1px solid gray;

    }

    </style>

    <table align="center">

        <tr>

            <td>id</td>

            <td>name</td>

            <td>st.index</td>

            <td>st.count</td>

            <td>st.first</td>

            <td>st.last</td>

            <td>st.odd</td>

            <td>st.even</td>

        </tr>

        <s:iterator value="products" var="p" status="st">

            <tr>

                <td>${p.id}</td>

                <td>${p.name}</td>

                <td>${st.index}</td>

                <td>${st.count}</td>

                <td>${st.first}</td>

                <td>${st.last}</td>

                <td>${st.odd}</td>

                <td>${st.even}</td>

            </tr>

        </s:iterator>

    </table>

    步骤 7 : 测试

    重启tomcat,访问网页

    http://127.0.0.1:8080/struts/listProduct

    测试

    关注我,分享更多Java全栈技术
    更多Java全栈技术内容,点击了解: https://how2j.cn/k/struts/struts-iterator/65.html

  • 相关阅读:
    软件架构设计箴言理解
    解决vs2010没有NET Framework 2.0和3.5
    .NET连接数据库实例
    CSS选择器大全
    SilverLight全屏代码
    SilverLight数字时钟 > 我的博客有SilverLight了
    comboBox设置为只读(只选)
    WinForm 单例模式实例
    标识种子和标识增量
    jQuery固定DIV实现“返回顶部”
  • 原文地址:https://www.cnblogs.com/Lanht/p/12789379.html
Copyright © 2020-2023  润新知