从值栈获取数据

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

   

   

   

   

   

   

   

   

从值栈获取数据

   

   

1、使用 Struts2 标签 + OGNL 表达式 获取值栈数据

   

主要:<s:property value="OGNL 表达式"/>

   

   

   

   

2、具体步骤

   

1)在 Action 中向值栈放数据

   

2)从 JSP 页面中获取值栈数据

   

   

   

   

3、具体实现

   

1)编写 Action

   

DataAction.java:

   

package com.siwuxie095.action;

   

import com.opensymphony.xwork2.ActionSupport;

   

public class DataAction extends ActionSupport {

// (1) Action 中定义变量

private String username;

// (2) 提供变量的 get 方法即可

public String getUsername() {

return username;

}

   

@Override

public String execute() throws Exception {

// (3) 在执行的方法中设置变量的值

username="siwuxie095";

return SUCCESS;

}

}

   

   

   

2)配置 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="data" class="com.siwuxie095.action.DataAction">

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

</action>

</package>

   

</struts>

   

   

   

3)编写页面

   

data.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>Data</title>

</head>

<body>

<!--

获取在 Action 的执行方法中设置的变量的值

value 属性:Action 中定义的变量名

-->

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

</body>

</html>

   

   

   

4)访问路径

   

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

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/7355523.html