property 标签是用来获取属性的值,这将默认堆栈的顶部,如果没有指定。这个例子显示了三个简单的数据标记 - 即set, push 和 property 的用法。
对于这个练习,让我们重用的例子在“数据类型转换”一章,但小的修改。因此,让我们开始创建类。考虑以下pojo类environment.java。
package com.h3.struts2; public class environment { private string name; public environment(string name) { this.name = name; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
我们动作类:
package com.h3.struts2; import com.opensymphony.xwork2.actionsupport; public class systemdetails extends actionsupport { private environment environment = new environment("development"); private string operatingsystem = "windows xp sp3"; public string execute() { return success; } public environment getenvironment() { return environment; } public void setenvironment(environment environment) { this.environment = environment; } public string getoperatingsystem() { return operatingsystem; } public void setoperatingsystem(string operatingsystem) { this.operatingsystem = operatingsystem; } }
让我们有system.jsp以下内容:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>system details</title> </head> <body> <p>the environment name property can be accessed in three ways:</p> (method 1) environment name: <s:property value="environment.name"/><br/> (method 2) environment name: <s:push value="environment"> <s:property value="name"/><br/> </s:push> (method 3) environment name: <s:set name="myenv" value="environment.name"/> <s:property value="myenv"/> </body> </html>
现在,让我们去逐一通过三个选项:
在第一个方法中,我们使用属性标记的环境的名称来获取值。由于环境变量是在动作类,它可以自动值栈中的。我们可以直接引用它使用属性environment.name。当一个类中的属性数有限。试想一下,如果有20个环境类中的属性。每次需要参考这些变量需要添加“environment.”作为前缀。push标签记处理。
在第二种方法中,我们把“environment”属性的堆栈。因此,现在主体内的 push 标记,环境属性是可堆栈的根。所以,现在引用属性,很容易在这个例子所示。
在最后的方法,我们使用设置的标签来创建一个新的变量称为myenv。此变量的值设置为environment.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> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="system" class="com.h3.struts2.systemdetails" method="execute"> <result name="success">/system.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://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
右键点击项目名称,并单击 export > war file 创建一个war文件。然后部署此war在tomcat的webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/system.action。这会给出以下画面: