struts2 從值棧中獲取數(shù)據(jù)

使用struts標簽+ognl獲取值棧的數(shù)據(jù)

<s:propery=ty value="ognl表達式"/>

一、獲取字符串

1、步驟
  • 向值棧中放入字符串(第三種方法)
  • action配置到一個頁面
  • 在頁面中取值
2、代碼

ValuesStackAction2.java

package work.zhangdoudou.Action;

import com.opensymphony.xwork2.ActionSupport;

public class ValuesStackAction2 extends ActionSupport{
    //1定義
    private String name;
    //2生成個get方法
    public String getName() {
        return name;
    }

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

    @Override
    public String execute() throws Exception {
        //在執(zhí)行的方法里向變量設(shè)置值
        name="wangwu";
        
        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="userAction2" class="work.zhangdoudou.Action.ValuesStackAction2" 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_ognl</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>

配置界面ValuesStack.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'ValuesStackAction.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
   <s:debug></s:debug>
   <!-- 獲取字符串值 -->
   <s:property value="name"/>
  </body>
</html>
3、運行效果
image.png

二、獲取對象

1、步驟
  • 在值棧里放一個對象
  • 給對象設(shè)置值
  • action配置到一個頁面
  • 在頁面中獲取值棧中對象的值
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>

</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_ognl</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>

配置界面ValuesStack.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'ValuesStackAction.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
   <s:debug></s:debug>
   獲取對象的值:<br/>
   <s:property value="user.username"/><br>
   <s:property value="user.password"/><br>
   <s:property value="user.type"/><br>
  </body>
</html>
3、運行結(jié)果
image.png

三、獲取list

1、步驟
  • 在值棧里放一個list
  • 給list設(shè)置值
  • action配置到一個頁面
  • 在頁面中獲取值棧中l(wèi)ist的值
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存放數(shù)據(jù)
        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>

</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_ognl</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>

配置界面ValuesStack.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'ValuesStackAction.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
   <s:debug></s:debug>
 
  獲取 list值第一種死遍歷:<br/>
   <s:property value="list[0].username"/><br>
   <s:property value="list[0].password"/><br>
   <s:property value="list[0].type"/><br>
   <s:property value="list[1].username"/><br>
   <s:property value="list[1].password"/><br>
   <s:property value="list[1].type"/><br>
    <hr/>
   獲取 list值第二種iterator標簽:<br/>
   <!-- 用標簽iterator標簽 -->
   <s:iterator value="list">
       <!-- 遍歷list得到list里頭的每個user對象 -->
       <s:property value="username"/><br>
       <s:property value="password"/><br>
       <s:property value=" type"/><br>
   
   </s:iterator>
    <hr/>
     獲取 list值第三種iterator標簽:<br/>
    <!-- 用標簽iterator標簽
            用兩個屬性value 和var
     --> 
     <s:iterator value="list" var="user">
       <!-- 遍歷list得到list里頭的每個user對象 
            機制:把每次遍歷的user對象放到context里面
            獲取context里面的數(shù)據(jù):寫ognl表達式
       -->
       <s:property value="#user.username"/><br>
       <s:property value="#user.password"/><br>
       <s:property value="#user.type"/><br>
     </s:iterator>
  </body>
</html>
3、運行效果
image.png

<s:iterator value="list" var="user">加了var屬性會把值臨時放進context中


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 什么是Struts2的框架Struts2是Struts1的下一代產(chǎn)品,是在 struts1和WebWork的...
    inke閱讀 2,340評論 0 50
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,718評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,012評論 0 11
  • 詳談 Struts2 的核心概念 本文將深入探討Struts2 的核心概念,首先介紹的是Struts2 的體系結(jié)構(gòu)...
    可愛傻妞是我的愛閱讀 1,226評論 0 2
  • 意志力時時都在體現(xiàn)!抉擇,誘惑都會消耗意志力!對自己的行為有所察覺。比如有件更為重要的事情需要去做,但這時你卻開始...
    四點云閱讀 352評論 0 0

友情鏈接更多精彩內(nèi)容