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中数据

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

发布了2 篇原创文章 · 获赞 0 · 访问量 4

猜你喜欢

转载自blog.csdn.net/qq_32552297/article/details/105529792