- Struts 2 教程
- Struts2 - 首页
- Struts2 - 基本 MVC 架构
- Struts2 - 概述
- Struts2 - 环境设置
- Struts2 - 架构
- Struts2 - 示例
- Struts2 - 配置
- Struts2 - Action
- Struts2 - 拦截器
- Struts2 - 结果类型
- Struts2 - 值栈/OGNL
- Struts2 - 文件上传
- Struts2 - 数据库访问
- Struts2 - 发送邮件
- Struts2 - 验证
- Struts2 - 国际化
- Struts2 - 类型转换
- Struts2 - 主题/模板
- Struts2 - 异常处理
- Struts2 - 注解
- Struts 2 标签
- Struts2 - 控制标签
- Struts2 - 数据标签
- Struts2 - 表单标签
- Struts2 - Ajax 标签
- Struts 2 集成
- Struts2 - Spring
- Struts2 - Tiles
- Struts2 - Hibernate
- Struts 2 有用资源
- Struts2 - 问题与解答
- Struts2 - 快速指南
- Struts2 - 有用资源
- Struts2 - 讨论
Struts 2 - push 标签
property 标签用于获取值的属性,如果没有指定,则默认为栈顶的值。此示例演示了三个简单的数据标签的使用 - 即set、push 和property。
创建 Action 类
在本练习中,让我们重用“数据类型转换”章节中给出的示例,但稍作修改。所以让我们从创建类开始。考虑以下 POJO 类Environment.java。
package com.tutorialspoint.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;
}
}
让我们有以下 action 类:
package com.tutorialspoint.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 标签获取 environment 的 name 属性的值。由于 environment 变量位于 action 类中,因此它会自动在值栈中可用。我们可以直接使用 propertyenvironment.name 来引用它。当类中属性数量有限时,方法 1 可以正常工作。想象一下,如果 Environment 类中有 20 个属性。每次需要引用这些变量时,都需要添加“environment.”作为前缀。这时 push 标签就派上用场了。
在第二种方法中,我们将“environment”属性推送到栈中。因此,现在在 push 标签的主体内部,environment 属性在栈的根部可用。因此,您现在可以像示例中所示的那样轻松地引用该属性。
在最后一种方法中,我们使用 set 标签创建一个名为 myenv 的新变量。此变量的值设置为 environment.name。因此,现在我们可以在任何需要引用 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.tutorialspoint.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>
右键单击项目名称并单击导出 > WAR 文件以创建 WAR 文件。然后将此 WAR 部署到 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URLhttps://:8080/HelloWorldStruts2/system.action。这将生成以下屏幕: