Struts2(十)OGNL标签二与Struts2标签

时间:2023-01-10 19:38:09

一、Struts2标签的优势

标签库简化了用户对标签的使用

结合OGNL使用,对于集合、对象的访问功能非常强大

提供可扩展的主题、模板支持、极大简化了视图页面的编写

不依赖任何表现层技术

Struts2标签的分类:

通用标签(Generir Tags)

  • 数据标签(Data Tags)
  • 控制标签(Control Tags)

UI标签(UI Tags)

Ajax标签(Ajax Tags)

二、数据标签

Struts2(十)OGNL标签二与Struts2标签

Struts2(十)OGNL标签二与Struts2标签

web.xml

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

三个实体类

package com.pb.entity;
/*
* 地区类
*/
public class District { private int districtId; //地区编号
private String districtName; //地区名称 public District() {
super();
// TODO Auto-generated constructor stub
}
public District(int districtId, String districtName) {
super();
this.districtId = districtId;
this.districtName = districtName;
}
public int getDistrictId() {
return districtId;
}
public String getDistrictName() {
return districtName;
}
public void setDistrictId(int districtId) {
this.districtId = districtId;
}
public void setDistrictName(String districtName) {
this.districtName = districtName;
} } package com.pb.entity;
/*
* 街道
*/
public class Street { private int streetId; //街道ID
private String streetName; //街道名称
private District district; //街道所在地区
public Street() {
super();
// TODO Auto-generated constructor stub
}
public Street(int streetId, String streetName, District district) {
super();
this.streetId = streetId;
this.streetName = streetName;
this.district = district;
}
public District getDistrict() {
return district;
}
public int getStreetId() {
return streetId;
}
public String getStreetName() {
return streetName;
}
public void setDistrict(District district) {
this.district = district;
}
public void setStreetId(int streetId) {
this.streetId = streetId;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
} } package com.pb.entity; import java.util.Date;
/*
* 房屋
*/
public class House { private int houseId; //房屋ID
private String title; //房屋标题
private Street street;//房屋所在街道
private Date addDate;//房屋发布日期
private String desc; //房屋描述
public House() {
super();
// TODO Auto-generated constructor stub
}
public House(int houseId, String title, String desc, Date addDate,
Street street) {
super();
this.houseId = houseId;
this.title = title;
this.desc = desc;
this.addDate = addDate;
this.street = street;
}
public Date getAddDate() {
return addDate;
}
public String getDesc() {
return desc;
}
public int getHouseId() {
return houseId;
}
public Street getStreet() {
return street;
}
public String getTitle() {
return title;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public void setDesc(String desc) {
this.desc = desc;
}
public void setHouseId(int houseId) {
this.houseId = houseId;
}
public void setStreet(Street street) {
this.street = street;
}
public void setTitle(String title) {
this.title = title;
} }

houseaction

package com.pb.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.District;
import com.pb.entity.House;
import com.pb.entity.Street; public class HouseAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<House> houses;
@Override
public String execute() throws Exception {
//声明地区
District dt1=new District(75501,"宝安区");
District dt2=new District(75502,"南山区");
//声明街道
Street street1=new Street(74,"布心一村",dt1);
Street street2=new Street(25,"商业街",dt1);
Street street3=new Street(87,"科技园",dt2);
Street street4=new Street(99,"南头古城",dt2);
//房屋
House house1=new House(11,"一房一厅","靠近公园",new Date(),street1);
House house2=new House(12,"二房一厅","环中线",new Date(),street2);
House house3=new House(13,"单间","高新公寓",new Date(),street3);
House house4=new House(14,"一房一厅","靠近中山公园",new Date(),street4);
houses=new ArrayList<House>();
//添加
houses.add(house1);
houses.add(house2);
houses.add(house3);
houses.add(house4);
return SUCCESS;
} public List<House> getHouses() {
return houses;
} public void setHouses(List<House> houses) {
this.houses = houses;
} }

struts.xml

 <package name="default" namespace="/" extends="struts-default">
<action name="house" class="com.pb.web.action.HouseAction">
<result name="success">
/houseList.jsp
</result>
</action>
</package>

页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
//绝对路径
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 引入绝对路径 -->
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>房屋列表信息</title>
</head>
<body>
<!--房屋列表中第一个房屋的信息标题 -->
房屋列表中第一个房屋的信息标题 :<s:property value="houses[0].title"/><br/>
房屋列表中第一个房屋的信息ID :<s:property value="houses[0].houseId"/><br/>
<!--房屋列表中第一个房屋的信息标题 -->
房屋列表中第一个房屋的信息标题 :<s:property value="" default="默认信息"/><br/>
<!--是否转义HTML,将OGNL转化为字符要加单引号 -->
是否转义HTML:<s:property value="'<hr/>'" /><br/>
<!--是否转义HTML 将OGNL转化为字符要加单引号 escapeXml默认为true -->
是否转义HTML:<s:property value="'<hr/>'" escapeHtml="false" /><br/>
<!--s:date标签格式化日期 -->
房屋列表中第一个房屋的发布日期 未格式:<s:property value="houses[0].addDate"/><br/>
房屋列表中第一个房屋的发布日期 格式化后:<s:date name="houses[0].addDate" format="yyyy-MM-dd HH:mm:ss"/><br/>
<s:property value="'<hr/>'" escapeHtml="false"/><br/>
<!--s:set标签 -->
房屋列表中第一个房屋的街道名称:<s:set var="s" value="houses[0].Street" />
<s:property value="#s.streetName"/><br/>
房屋列表中第一个房屋的街道ID:<s:property value="#s.streetId"/><br/>
房屋列表中第一个房屋的地区名称:
<s:property value="houses[0].Street.District.districtId"/><br/>
房屋列表中第一个房屋的地区名称:<s:set var="d" value="#s.District" />
<s:property value="#d.districtName"/><br/>
<br/> 房屋列表中第一个房屋的地区ID:<s:property value="#d.districtId"/><br/><br/>
<s:property value="'<hr/>'" escapeHtml="false"/><br/>
<!--scope指定s:set var的取值 -->
房屋列表中第一个房屋的街道名称:<s:set var="s" value="houses[0].Street" scope="request" />
<s:property value="#request.s.streetName"/><br/>
房屋列表中第一个房屋的街道ID:<s:property value="#request.s.streetId"/><br/>
<s:property value="'<hr/>'" escapeHtml="false"/><br/> 房屋列表中第一个房屋的地区名称:<s:set var="d" value="houses[0].Street.District" scope="session"/>
<s:property value="#session.d.districtName"/><br/>
房屋列表中第一个房屋的地区ID:<s:property value="#session.d.districtId"/><br/>
<s:debug/><br/> </body>
</html>

地址栏输入:http://localhost:8080/OGNLDemo2/house.action发生跳转

三、s:a与s:url

Struts2(十)OGNL标签二与Struts2标签

<s:url var="house" value="house.action">
<!-- 传递参数 name为参数名称 vaule为要传的值 -->
<s:param name="str" value="'struts2'"/>
</s:url>
<!-- 使用%{}来获取值-->
<s:a href="%{#house}">跳转列表页面</s:a>
<!--动态包含 -->
<s:include value="include.jsp"></s:include>

Struts2(十)OGNL标签二与Struts2标签

四、控制标签

Struts2(十)OGNL标签二与Struts2标签

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
//绝对路径
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 引入绝对路径 -->
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>房屋列表信息</title>
<style type="text/css">
.header{
text-align: center;
background-color: #999999;
font-weight: bold;
font-size: 18px;
}
.even {
text-align: center;
background-color: #FFCCFF;
} .odd {
text-align: center;
background-color: #CCCCFF;
}
</style>
</head>
<body>
<table border="1" width="100%">
<tr class="header">
<td>房屋编号</td>
<td>房屋标题</td>
<td>房屋描述</td>
<td>房屋发布日期</td>
<td>房屋街道ID</td>
<td>房屋街道</td>
<td>房屋街道地区ID</td>
<td>房屋街道地区名称</td>
</tr>
<s:iterator var="hou" value="houses" status="status">
<s:if test="#status.even">
<tr class="even">
</s:if>
<s:if test="#status.odd">
<tr class="odd">
</s:if>
<!-- 定义街道 -->
<s:set var="strtmp" value="#hou.Street" />
<!--定义地区 -->
<s:set var="distmp" value="#strtmp.District" />
<td><s:property value="#hou.houseId" /></td>
<td><s:property value="#hou.title" /></td>
<td><s:property value="#hou.desc" /></td>
<td><s:date name="#hou.addDate" format="yyyy-MM-dd HH:mm:ss" /></td>
<td><s:property value="#strtmp.streetId" /></td>
<td><s:property value="#strtmp.streetName" /></td>
<td><s:property value="#distmp.districtId" /></td>
<td><s:property value="#distmp.districtName" /></td>
</tr>
</s:iterator>
</table>
<s:debug />
</body>
</html>

struts.xml

<action name="house2" class="com.pb.web.action.HouseAction">
<result name="success">
/houseList2.jsp
</result>
</action>

地址栏中输入:http://localhost:8080/OGNLDemo2/house2.action访问