- 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 - 属性标签
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标签获取环境名称的值。由于环境变量在Action类中,它会自动在值栈中可用。我们可以使用属性environment.name直接引用它。当类中属性数量有限时,方法1效果很好。想象一下,如果Environment类中有20个属性。每次需要引用这些变量时,都需要添加“environment.”作为前缀。这就是push标签派上用场的地方。
在第二种方法中,我们将“environment”属性推入栈中。因此,现在在push标签的主体内容内,environment属性在栈顶可用。从现在开始,您可以像示例中显示的那样轻松地引用该属性。
在最后一种方法中,我们使用set标签创建一个名为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.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服务器并尝试访问URL https://:8080/HelloWorldStruts2/system.action。这将产生以下屏幕:
struts_data_tags.htm
广告