1. 程式人生 > >從值棧獲取數據

從值棧獲取數據

實現 space char row value ext throws tle 配置

--------------------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】

從值棧獲取數據