Struts2向jsp提供数据并获取

新建struts2 web项目

在这里插入图片描述

代码及配置

EmpAction.java

package net.jqsoft.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.jqsoft.domain.Emp;

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


public class EmpAction extends ActionSupport implements ModelDriven {
    List<Emp> emp;

    @Override
    public Object getModel() {
        if(emp==null){
            emp = new ArrayList<>();
        }
        return emp;
    }


    public String execute() {
        emp.add(new Emp(9527,"zxc"));
        emp.add(new Emp(9528,"zxx"));
        emp.add(new Emp(9529,"zxx1"));

        ActionContext context = ActionContext.getContext();
        context.getApplication().put("ApplicationE",emp);	//向application中添加emp对象
        context.getSession().put("SessionE",emp);
        Map request = (Map) ActionContext.getContext().get("request");
        request.put("RequestE", emp);
        context.put("emp",emp);

        return SUCCESS;
    }
}

Emp

package net.jqsoft.domain;

public class Emp {
    private Integer id;
    private String name;

    public Emp() {
    }

    public Emp(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

struts.xml

<?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>
    <package name="emp" namespace="/emp" extends="struts-default">
        <action name="list" class="net.jqsoft.action.EmpAction">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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>

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>struts2</title>
  </head>
  <body>
    获取Context里的值<br/>
    --------------------<br/>
    Application<br/>
    <s:iterator value="#application.ApplicationE" var="e" status="st">
      <s:property value="#st.getIndex()+1"/> <s:property value="#m.id"/> <s:property value="#e.name"/><br/>
    </s:iterator>
    --------------------<br/>
    Session<br/>
    <s:iterator value="#session.SessionE" var="e" status="st">
      <s:property value="#st.getIndex()+1"/> <s:property value="#e.id"/> <s:property value="#e.name"/><br/>
    </s:iterator>
    --------------------<br/>
    Request<br/>
    <s:iterator value="#request.RequestE" var="e" status="st">
      <s:property value="#st.getIndex()+1"/> <s:property value="#e.id"/> <s:property value="#e.name"/><br/>
    </s:iterator>
    --------------------<br/>
    Context Stack<br/>
    <s:iterator value="#emp" var="e" status="st">
      <s:property value="#st.getIndex()+1"/> <s:property value="#e.id"/> <s:property value="#e.name"/><br/>
    </s:iterator>
    --------------------<br/>
    Value Stack<br/>
    <s:iterator value="model" var="e" status="st">
      <s:property value="#st.getIndex()+1"/> <s:property value="#e.id"/> <s:property value="#e.name"/><br/>
    </s:iterator>
    --------------------<br/>
    <s:debug></s:debug>
  </body>
</html>

访问结果

在这里插入图片描述

Context map中数据

在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值