property标签用于获取值的属性,如果没有指定,它将默认为在值栈的顶部。此示例显示了三个简单的数据标签的用法,即set,push和property。
创建action类
在本章节的学习中,我们会再次使用Struts2 类型转换章节中给出的示例,但需要稍作修改。我们可以考虑使用下面的POJO类Environment.java进行类的创建。
创建以下action类:
- package com.w3xue.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.w3xue.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>
- 在第一种方法中,我们使用property标签获取环境名称的值。因为环境变量在action类中,所以它在值栈中自动可用,我们可以使用environment.name属性直接引用它。当一个类中的属性数量有限时,方法一工作正常。想象一下,如果你在Environment类中有20个属性,每次你需要引用这些变量,你都需要添加“environment.”作为前缀,而这就是需要push标签来处理的地方了。
- 在第二种方法中,我们将“environment”属性推送到值栈。因而在push标签的中,environment属性在值栈的根目录下可用。所以你可以很容易地引用属性,如示例所示。
- 在最后的方法中,我们使用set标签创建一个名为myenv的新变量。此变量的值设置为environment.name。那么,无论我们在哪引用environment的名称,我们都可以使用这个变量。
配置文件
你的struts.xml应该如下所示:你的web.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.w3xue.struts2.SystemDetails"
- method="execute">
- <result name="success">/System.jsp</result>
- </action>
- </package>
- </struts>
现在,右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/system.action,将显示以下界面:
- <?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>
转载本站内容时,请务必注明来自W3xue,违者必究。