struts2 向值栈中存放对象和List集合(第三种方式)

一、向值栈中放对象

1、实现步骤
  • 定义对象变量
  • 生成变量的get方法
  • 在执行的方法里头向对象设置值
2、代码

User.java

package work.zhangdoudou.Bean;

public class User {
    private String username;
    private String password;
    private String type;
    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 getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
        
}

ValuesStackAction3.java

package work.zhangdoudou.Action;

import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;

public class ValuesStackAction3 extends ActionSupport{
    //1定义对象变量
    private User user;
    //2生成个get方法
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    @Override
    public String execute() throws Exception {
        //向值栈的user放对象
        user= new User();
        user.setUsername("zhangsan");
        user.setPassword("123");
        user.setType("student");
        
        return SUCCESS;
    }
}   

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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="userAction3" class="work.zhangdoudou.Action.ValuesStackAction3" method="execute">
            <result name="success">/ValuesStack.jsp</result>
        </action>
    </package>
</struts>

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2.217</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
3、运行结果
image.png

二、向值栈中存放List集合

1、步骤
  • 定义list集合的变量
  • 生成get方法
  • 在执行方法向list设置数据
2、代码

User.java

package work.zhangdoudou.Bean;

public class User {
    private String username;
    private String password;
    private String type;
    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 getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
        
}

ValuesStackAction4.java

package work.zhangdoudou.Action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;

public class ValuesStackAction4 extends ActionSupport{
    //1定义list变量
    private List<User> list;
    //2生成个get方法

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }
    @Override
    public String execute() throws Exception {
        //向list存放数据
        User user1=new User();
        list=new ArrayList<User>();
        user1.setUsername("zhangsan");
        user1.setPassword("123");
        user1.setType("student");
        User user2=new User();
        user2.setUsername("lisi");
        user2.setPassword("456");
        user2.setType("student1");
        list.add(user1);
        list.add(user2);
        
        return SUCCESS;
    }
}

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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="userAction4" class="work.zhangdoudou.Action.ValuesStackAction4" method="execute">
            <result name="success">/ValuesStack.jsp</result>
        </action>
        
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2.217</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
3、运行结果
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,954评论 18 399
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 6,696评论 0 50
  • 这个故事发生在2016年一月份,怕时间越久忘得越快,所以决定记录下来。 01.初期 我们大概是在一月初放寒假,但我...
    曾琦阅读 4,661评论 15 15
  • 好的电影就像一本好书 不同时刻不同心境对待电影内容的感觉也不一样。就像,不同年龄段看的书类型不一样,想我高中时怎么...
    AnnD阅读 3,085评论 0 0
  • ZierHe阅读 669评论 0 0