<%@page import="hp.epm.ssh.app.compare.PersonComparator"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="hp.epm.ssh.app.bean.Person" %>
<%@ 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'common_tag.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:debug></s:debug>
<br>
s:property:打印值栈中的属性的值:对于对象栈,打印值栈中对应的属性值
<br><br>
<s:property value="productName"/><br>
${productName}
<br><br>
对于Map栈,打印request,session,application的某个属性值或某个请求参数的值
<br><br>
<s:property value="#session.date"/><br>
${sessionScope.date}
<br><br>
<s:property value="#parameters.name[0]"/><br>
没有对应的EL表达式
<br><br>
s:url 创建一个URL字符串
<br><br>
<s:url value="/getProduct" var="url1"/>
1 <s:property value="#request.url1"/><br> <!-- OK -->
1 <s:property value="#session.url1"/><br>
1 <s:property value="#application.url1"/><br>
1 <s:property value="#attr.url1"/><br> <!-- OK -->
1 <s:property value="#url1"/><br> <!-- OK -->
1 <s:property value="url1"/><br> <!-- OK -->
2 ${pageScope.url1 }<br>
2 ${requestScope.url1 }<br> <!-- OK -->
2 ${sessionScope.url1 }<br>
2 ${applicationScope.url1 }<br>
<br><br>
<s:url value="/testUrl" var="url2" >
<s:param name="productId" value="2002"></s:param>
</s:url>
1 <s:property value="url2"/><br>
2 ${url2 }
<br><br>
<s:url value="/testUrl" var="url3" >
<!--会自动对value进行解析,若不希望自动进行解析,则使用''引起来 -->
<s:param name="productId" value="productId"></s:param>
</s:url>
1 <s:property value="url3"/><br>
2 ${url3 }
<br><br>
<s:url value="/testUrl" var="url4" >
<!--会自动对value进行解析,若不希望自动进行解析,则使用''引起来 -->
<s:param name="productId" value="'productId'"></s:param>
</s:url>
1 <s:property value="url4"/><br>
2 ${url4 }
<br><br>
<!-- 构建一个请求action的url -->
<s:url action="testAction" namespace="/helloWorld" method="save" var="url5" />
1 <s:property value="url5"/><br>
2 ${url5 }
<br><br>
<s:url value="testUrl" var="url6" includeParams="get"/>
1 <s:property value="url6"/><br>
2 ${url6 }
<br><br>
<s:url value="testUrl" var="url7" includeParams="all"/>
1 <s:property value="url7"/><br>
2 ${url7 }
<br><br>
s:set 向page,request,session,application域对象中加入一个属性值
<br><br>
<s:set name="pPrice" value="productPrice" scope="page"></s:set>
page范围1:productPrice:<s:property value="#request.pPrice"/><br>
page范围1:productPrice:<s:property value="#attr.pPrice"/><br> <!-- OK -->
page范围1:productPrice:<s:property value="#pPrice"/><br>
page范围1:productPrice:<s:property value="pPrice"/><br>
page范围2:productPrice:${pageScope.pPrice }<br> <!-- OK -->
page范围2:productPrice:${pPrice } <!-- OK -->
<br><br>
<s:set name="pName" value="productName" scope="request"></s:set>
request范围1:productName:<s:property value="#request.pName"/><br> <!-- OK -->
request范围1:productName:<s:property value="#attr.pName"/><br> <!-- OK -->
request范围1:productName:<s:property value="#pName"/><br>
request范围1:productName:<s:property value="pName"/><br>
request范围2:productName:${requestScope.pName }<br> <!-- OK -->
request范围2:productName:${pName } <!-- OK -->
<br><br>
<s:set var="pName" value="productName" scope="request"></s:set>
request范围1:productName:<s:property value="#request.pName"/><br> <!-- OK -->
request范围1:productName:<s:property value="#attr.pName"/><br> <!-- OK -->
request范围1:productName:<s:property value="#pName"/><br>
request范围1:productName:<s:property value="pName"/><br>
request范围2:productName:${requestScope.pName }<br> <!-- OK -->
request范围2:productName:${pName } <!-- OK -->
<br><br>
<s:set name="ppName" value="1234" scope="request"></s:set>
request范围1:productName:<s:property value="#request.ppName"/><br>
request范围2:productName:${requestScope.ppName }
<br><br>
<s:set name="pDesc" value="productDesc" scope="session"></s:set>
session范围1:productDesc:<s:property value="#session.pDesc"/><br>
session范围2:productDesc:${sessionScope.pDesc }
<br><br>
s:push 把一个对象在标签开始后压入到值栈中,标签结束时,弹出值栈
<br><br>
<%
Person person=new Person();
person.setName("hp");
person.setAge(10);
request.setAttribute("person", person);
%>
<s:push value="#request.person">
inside push:${name }<br>
inside push:<s:property value="name"/>
</s:push>
<br><br>
outside push:${name }<br>
outside push:<s:property value="name"/>
<br><br>
<s:if test="productPrice>6000">
iphone6s plus
</s:if>
<s:elseif test="productPrice>5000">
iphone6s
</s:elseif>
<s:else>
Not interested.
</s:else>
<br><br>
<s:if test="#request.person.age>10">
大于10岁
</s:if>
<s:else>
小于等于10岁
</s:else>
<br><br>
s:iterator 遍历集合,把集合对象中的每一个元素依次压入和弹出
<br><br>
<%
List<Person> persons=new ArrayList<Person>();
persons.add(new Person("AA",10));
persons.add(new Person("BB",20));
persons.add(new Person("CC",30));
persons.add(new Person("DD",40));
persons.add(new Person("EE",50));
request.setAttribute("persons", persons);
%>
<s:iterator value="#request.persons" >
${name } - ${age }
<s:property value="name"/> - <s:property value="age"/> <br>
</s:iterator>
${name } - ${age } <br>
<s:property value="name"/> - <s:property value="age"/>
<br><br>
<s:iterator value="persons" status="status">
index:${status.index}-count:${status.count}: ${name } - ${age }
index:<s:property value="#attr.status.index"/>-count:<s:property value="#attr.status.count"/> <s:property value="name"/> - <s:property value="age"/><br>
</s:iterator>
<br><br>
s:sort可以对集合中的元素进行排序
<br><br>
<%
PersonComparator pc=new PersonComparator();
request.setAttribute("comparator", pc);
%>
<s:sort comparator="#request.comparator" source="persons" var="persons2"/>
<s:iterator value="#attr.persons2">
${name } - ${age }
<s:property value="name"/> - <s:property value="age"/> <br>
</s:iterator>
<br><br>
s:date可以对Date对象进行排版
<%
request.setAttribute("date", new Date());
%>
<br><br>
<s:date name="#request.date" format="yyyy-MM-dd HH:mm:ss" var="date2" />
date:<s:property value="#request.date2"/><br> <!-- OK -->
date:<s:property value="#attr.date2"/><br> <!-- OK -->
date:<s:property value="#date2"/><br> <!-- OK -->
date:<s:property value="date2"/><br> <!-- OK -->
date:${requestScope.date2 }<br> <!-- OK -->
date:${date2 }<br> <!-- OK -->
<br><br>
<s:iterator value="persons">
<!-- 可以使用%{}把属性包装起来,使其进行强制OGNL解析 -->
<s:a href="getPerson.action?=%{name}">${name }</s:a>
</s:iterator>
</body>
</html>
product.java
package hp.epm.ssh.app.tagAction;
import hp.epm.ssh.app.bean.Person;
import java.util.ArrayList;
import java.util.List;
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private double productPrice;
private List<Person> persons=new ArrayList<Person>();
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", productDesc=" + productDesc
+ ", productPrice=" + productPrice + "]";
}
public String save(){
System.out.println("save:"+this);
return "details";
}
public String testTag(){
this.productId=1001;
this.productDesc="SmartPhone";
this.productName="Iphone6S";
this.productPrice=5299;
persons.add(new Person("AAA",11));
persons.add(new Person("BBB",22));
persons.add(new Person("CCC",33));
persons.add(new Person("DDD",44));
persons.add(new Person("EEE",55));
return "common";
}
}