目录

检查详细示例(Check Detailed Example)

property标签用于获取值的属性,如果未指定值,则默认为堆栈的顶部。 此示例显示了三个简单数据标记的用法 - 即set, pushproperty

创建动作类

对于本练习,让我们重用“数据类型转换”一章中给出的示例,但几乎没有修改。 那么让我们从创建类开始吧。 请考虑以下POJO类Environment.java

package com.iowiki.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.iowiki.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;
   }
}

创建视图 (Create Views)

让我们拥有具有以下内容的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标签来获取环境名称的值。 由于环境变量位于操作类中,因此它在值堆栈中自动可用。 我们可以使用属性environment.name直接引用它。 当类中的属性数量有限时,方法1可以正常工作。 想象一下,如果您在Environment类中有20个属性。 每次需要引用这些变量时,都需要添加“环境”。 作为前缀。 这就是推送标签派上用场的地方。

  • 在第二种方法中,我们将“environment”属性推送到堆栈。 因此,现在在push标签的主体内,环境属性在堆栈的根目录中可用。 因此,您现在可以非常轻松地引用该属性,如示例中所示。

  • 在最后一个方法中,我们使用set标签创建一个名为myenv的新变量。 此变量的值设置为environment.name。 所以,现在我们可以在我们引用环境名称的任何地方使用这个变量。

配置文件 (Configuration Files)

你的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.iowiki.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文件。 然后在Tomcat的webapps目录中部署此WAR。 最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/system.action 。 这将产生以下屏幕 -

Struts属性标记
↑回到顶部↑
WIKI教程 @2018