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>