zoukankan      html  css  js  c++  java
  • 从值栈获取List集合

    -------------------siwuxie095

       

       

       

       

       

       

       

       

    从值栈获取 List 集合

       

       

    1、具体步骤

       

    1)在 Action 中向值栈放 List 集合

       

    2)在 JSP 页面中从值栈获取 List 集合

       

       

       

    2、具体实现

       

    1)编写实体类

       

    User.java:

       

    package com.siwuxie095.entity;

       

       

    // User 实体类

    public class User {

       

    private String username;

    private String password;

    private String address;

    public String getUsername() {

    return username;

    }

    public void setUsername(String username) {

    this.username = username;

    }

    public String getPassword() {

    return password;

    }

    public void setPassword(String password) {

    this.password = password;

    }

    public String getAddress() {

    return address;

    }

    public void setAddress(String address) {

    this.address = address;

    }

    @Override

    public String toString() {

    return "User [username=" + username + ", password=" + password

    + ", address=" + address + "]";

    }

    }

       

       

       

    2)编写 Action

       

    ListAction.java:

       

    package com.siwuxie095.action;

       

    import java.util.ArrayList;

    import java.util.List;

       

    import com.opensymphony.xwork2.ActionSupport;

    import com.siwuxie095.entity.User;

       

    public class ListAction extends ActionSupport {

       

    /*

    * (1) Action 中定义 List 集合对象

    *

    * 因为总归是要 new 的,所以就在这里创

    * 建,而不是声明了

    */

    private List<User> list=new ArrayList<User>();

    /*

    * (2) 提供 List 集合对象的 get 方法即可

    */

    public List<User> getList() {

    return list;

    }

    @Override

    public String execute() throws Exception {

    /*

    * 如果上面仅仅是声明了List 集合对象,

    * 那么就要在这里创建,即 new 出来

    */

    /*

    * (3) 在执行的方法中,向 List 集合对象中设置值

    */

    User user1=new User();

    user1.setUsername("小白");

    user1.setPassword("8888");

    user1.setAddress("中国");

    User user2=new User();

    user2.setUsername("小黑");

    user2.setPassword("4444");

    user2.setAddress("美国");

    list.add(user1);

    list.add(user2);

    return SUCCESS;

    }

       

    }

       

       

       

    3)配置 Action

       

    struts.xml:

       

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

    <!DOCTYPE struts PUBLIC

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

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

       

    <struts>

    <package name="demo" extends="struts-default" namespace="/">

    <action name="list" class="com.siwuxie095.action.ListAction">

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

    </action>

    </package>

       

    </struts>

       

       

       

    4)编写页面

       

    从值栈获取 List 集合的方式共有三种

       

    1)方式一

       

    list.jsp:

       

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

    pageEncoding="UTF-8"%>

    <!-- 引入 Struts2 标签库 -->

    <%@ 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">

    <title>List</title>

    </head>

    <body>

       

    <!-- 获取值栈中 List 集合的数据 -->

    <s:property value="list[0].username"></s:property>

    <s:property value="list[0].password"></s:property>

    <s:property value="list[0].address"></s:property>

    <br/>

    <s:property value="list[1].username"></s:property>

    <s:property value="list[1].password"></s:property>

    <s:property value="list[1].address"></s:property>

    </body>

    </html>

       

       

       

    2)方式二

       

    list.jsp:

       

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

    pageEncoding="UTF-8"%>

    <!-- 引入 Struts2 标签库 -->

    <%@ 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">

    <title>List</title>

    </head>

    <body>

       

    <!-- 获取值栈中 List 集合的数据 -->

    <s:iterator value="list">

    <!-- 遍历 List 集合,得到其中的每个 User 对象 -->

    <s:property value="username"></s:property>

    <s:property value="password"></s:property>

    <s:property value="address"></s:property>

    <br/>

    </s:iterator>

    </body>

    </html>

       

       

       

    3)方式三

       

    list.jsp:

       

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

    pageEncoding="UTF-8"%>

    <!-- 引入 Struts2 标签库 -->

    <%@ 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">

    <title>List</title>

    </head>

    <body>

       

    <!-- 获取值栈中 List 集合的数据 -->

    <s:iterator value="list" var="user">

    <!--

    遍历 List 集合,得到其中的每个 User 对象

    优化机制:把每次遍历出来的 User 对象,放

    context 中,变成对象引用,再取出

    在写 OGNL 表达式时,需要使用特殊符号 #

    -->

    <s:property value="#user.username"></s:property>

    <s:property value="#user.password"></s:property>

    <s:property value="#user.address"></s:property>

    <br/>

    </s:iterator>

    </body>

    </html>

       

       

       

    5)访问路径

       

    http://localhost:8080/工程名/list.action

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    solr7.7.0 添加core (二)
    centos 安装solr7.7+tomcat8.5.31+jdk1.8 环境搭建(一)
    springboot 切面编程 日志模块
    Mysql 优化
    mysql查询某个字段中是否有重复的值
    php for循环字母
    layui.table.toolbar里的内容加判断
    phpmyadmin 导入大文件配置
    解决laravel5.2 使用ajax时的 VerifyCsrfToken问题
    火狐浏览器禁止缓存
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/7355557.html
Copyright © 2011-2022 走看看