struts2标签
终于把struts2标签看的差不多了,不过还有好多还不是很熟悉,我是结合Max的struts2教程和struts自带的reference文档学习的!笔记中有好多都是从Max的博客中搬来的。不过也没有办法,咱水平还不行,也只能站在人家的肩膀上学习一下了!!
if,elseif, else标签
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'tags.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" > |
小技巧:#parameters.country[ 1 ]可以这样取值!原来Struts2是将URL传的参数封装 成一个数组的, |
也就是说你可以在一个URL传两个同名的参数(即?name=a&name=b); |
<%request.setCharacterEncoding( "utf-8" );%> |
(request获取方式)country=<%=request.getParameter( "country" ) %><br> |
<s: if
test= "#parameters.country[1] == 'www'" >中国</s: if > |
<s:elseif test= "#parameters.country[0] == '美国'" >美国</s:elseif> |
<s:set name= "country"
value= "#parameters.country[1]" ></s:set> |
country=<s:property value= "#country"
/> <br> |
<s: if
test= "#country == 'www'" >中国</s: if > |
<s:elseif test= "#country == '美国'" >美国</s:elseif> |
这里面有个小常识,就是通过url传递参数的时候:Struts2是将URL传的同名参数封装成一个数组,也就是说我们可以在一个URL传两个同名的参数(即?name=a&name=b);获取的时候直接#parameters.name[0], parameters.name[1]就可以分别取出两个参数的值。
include标签
被包含页面:
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
this is the include page!!! |
包含页面:
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme() +
"://" |
+ request.getServerName() +
":" + request.getServerPort() |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'includea.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" mce_href= "styles.css" > |
<s:include value= "include.jsp" > |
<!-- 提交表单时才会获得value的值 --> |
<s:param name= "value1" >test1</s:param> |
<s:param name= "value2"
value= "user" ></s:param> |
当然struts的include标签静态动态页面都能包含的!
i18n标签
描述:加载资源包到值堆栈。它可以允许text标志访问任何资源包的信息,而不只当前action相关联的资源包。
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'tags3.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" mce_href= "styles.css" > |
The i18n value is <s:text name= "hi!!" ></s:text> |
iterator标签
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'tags2.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" mce_href= "styles.css" > |
List<String> strs =
new ArrayList<String>(); |
request.setAttribute( "strs" , strs); |
<s:iterator value= "#request.strs"
var= "strs" > |
<s:property value= "#strs" /> |
这个标签挺容易理解的。
param标签
struts2的<s: param>标签问我觉得比较复杂的。struts2的s:param标签主要有两个属性name与value, 若想在value属性中输入直接量,则可以这样写:<s:param name="some" value="%{'user'}"/>, 也可以这样写:<s:param name="some">user</s:param>。 但如果直接赋值,这个值不是由Action动态生成的,而是自己指定的一个字符串,则只能用后者。
param页面代码:
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'param.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" mce_href= "styles.css" > |
<!-- struts2的s:param标签主要有两个属性name与value,
|
若想在value属性中输入直接量,则可以这样写:<s:param name= "some"
value= "%{'user'}" />,
|
也可以这样写:<s:param name= "some" >user</s:param>。 |
但如果直接赋值,这个值不是由Action动态生成的,而是自己指定的一个字符串,则只能用后者。 --> |
<s:url value= "paramAction.jsp"
id= "href" > |
<s:param name= "value1" >hello!!</s:param> |
<s:param name= "valu2"
value= "%{'HELLO!'}" ></s:param> |
<s:a href= "%{href}"
mce_href= "%{href}" >paramAction</s:a> |
paramAction页面:
<%@ page language= "java"
import = "java.util.*"
pageEncoding= "UTF-8" %> |
<%@ taglib uri= "/struts-tags"
prefix= "s" %> |
String path = request.getContextPath(); |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
<base href= "<%=basePath%>" > |
<title>My JSP
'paramAction.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" mce_href= "styles.css" > |
value1的值:<%=request.getParameter( "value1" ) %><br> |
value1的值:<s:property value= "#parameters.value1"
/><br> |
value2的值:<%=request.getParameter( "value2" ) %><br/><!-- 获取不到值 --> |
value2的值:${#param.value2 }<!-- 获取不到值 --> |
经常用到的UI标签
<%@ page contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8"
%> |
<%@ taglib prefix= "s"
uri= "/struts-tags"
%> |
<%@ taglib prefix= "s"
uri= "/struts-tags"
%> |
<%@ taglib prefix= "sx"
uri= "/struts-dojo-tags"
%> |
<title>UI Tags Example</title> |
<sx:head parseContent= "true" />
|
<s:form action= "exampleSubmit"
method= "post"
enctype= "multipart/form-data"
tooltipConfig= "#{'jsTooltipEnabled':'true'}" > |
tooltip= "Enter your Name here"
/> |
<s:date name= "Select Your Birthday" />
|
<!-- 在struts2. 2.1 中<sx:datetimepicker/>的标签使用有所变化,需要引入struts2-dojo-plugin- 2.2 . 1 .jar
|
tooltip= "Select Your Birthday" |
tooltip= "Enter your Biography" |
tooltip= "Choose Your Favourite Color" |
list= "{'Red', 'Blue', 'Green'}" |
tooltip= "Choose Your Favourite Language" |
label= "Favourite Language" |
list= "#{'CN':'中文','EN':'英文','FR':'外文'}" |
tooltip= "Choose your Friends" |
list= "{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}" |
tooltip= "Confirmed that your are Over 18" |
tooltip= "Choose Your State" |
name= "region"
list= "{'North', 'South'}" |
doubleList= "top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" |
headerValue= "---------- Please Select ----------" |
tooltip= "Upload Your Picture" |
tooltip= "Select Your Favourite Cartoon Characters" |
label= "Favourite Cartoons Characters" |
name= "leftSideCartoonCharacters" |
list= "{'Popeye', 'He-Man', 'Spiderman'}" |
headerValue= "--- Please Select ---" |
doubleList= "{'Superman', 'Mickey Mouse', 'Donald Duck'}" |
doubleName= "rightSideCartoonCharacters" |
doubleHeaderKey= "doubleHeaderKey" |
doubleHeaderValue= "--- Please Select ---" |
tooltip= "Enter your thoughts here"
/> |
<s:submit οnclick= "alert('aaaa');"
/> |
<s:reset οnclick= "alert('bbbb');"
/> |
</html>