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"> <html> <head> <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"> </head> <body> <!-- 小技巧:#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:else >其他國籍的人</s:else> <br> <!-- 為某個屬性賦值 --> <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> <s:else >其他國籍的人</s:else> <br> </body> </html>
這里面有個小常識,就是通過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() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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"> --> </head> <body> This is my JSP1 page. <br> <s:include value="include.jsp"> <!-- 提交表單時才會獲得value的值 --> <s:param name="value1">test1</s:param> <s:param name="value2" value="user"></s:param> </s:include> </body> </html>
當然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"> <html> <head> <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"> --> </head> <body> <!-- <s:text>標簽沒弄明白 --> <s:i18n name="I18N"> The i18n value is <s:text name="hi!!"></s:text> </s:i18n> <br> <s:debug></s:debug> </body> </html>
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"> <html> <head> <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"> --> </head> <% List<String> strs = new ArrayList<String>(); strs.add("a"); strs.add("b"); strs.add("c"); strs.add("d"); strs.add("e"); request.setAttribute("strs", strs); %> <body> <s:iterator value="#request.strs" var="strs"> <s:property value="#strs"/> </s:iterator> <s:debug></s:debug> </body> </html>
這個標簽挺容易理解的。
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"> <html> <head> <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"> --> </head> <body> <!-- 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:url> <s:a href="%{href}" mce_href="%{href}">paramAction</s:a> <s:debug></s:debug> </body> </html>
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"> <html> <head> <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"> --> </head> <body> value1的值:<%=request.getParameter("value1") %><br> value1的值:<s:property value="#parameters.value1" /><br> value2的值:<%=request.getParameter("value2") %><br/><!-- 獲取不到值 --> value2的值:${#param.value2 }<!-- 獲取不到值 --> <s:debug></s:debug> </body> </html>
經常用到的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" %> <html> <head> <title>UI Tags Example</title> <s:head /> <sx:head parseContent="true"/> </head> <body> <s:actionerror/> <s:actionmessage/> <s:fielderror /> <s:form action="exampleSubmit" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}"> <s:textfield label="Name" name="name" tooltip="Enter your Name here" /> <s:date name="Select Your Birthday"/> <!-- 在struts2.2.1中<sx:datetimepicker/>的標簽使用有所變化,需要引入struts2-dojo-plugin-2.2.1.jar 這個包。 --> <sx:datetimepicker tooltip="Select Your Birthday" label="Birthday" name="birthday" /> <s:textarea tooltip="Enter your Biography" label="Biograph" name="bio" cols="20" rows="3"/> <s:select tooltip="Choose Your Favourite Color" label="Favorite Color" list="{'Red', 'Blue', 'Green'}" name="favoriteColor" emptyOption="true" headerKey="None" headerValue="None"/> <s:select tooltip="Choose Your Favourite Language" label="Favourite Language" list="#{'CN':'中文','EN':'英文','FR':'外文'}" name="favouriteLanguage" emptyOption="true" headerKey="None" headerValue="None"/> <s:checkboxlist tooltip="Choose your Friends" label="Friends" list="{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}" name="friends"/> <s:checkbox tooltip="Confirmed that your are Over 18" label="Age 18+" name="legalAge"/> <s:doubleselect tooltip="Choose Your State" label="State" name="region" list="{'North', 'South'}" value="'South'" doubleValue="'Florida'" doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" doubleName="state" headerKey="-1" headerValue="---------- Please Select ----------" emptyOption="true" /> <s:file tooltip="Upload Your Picture" label="Picture" name="picture" /> <s:optiontransferselect tooltip="Select Your Favourite Cartoon Characters" label="Favourite Cartoons Characters" name="leftSideCartoonCharacters" leftTitle="Left Title" rightTitle="Right Title" list="{'Popeye', 'He-Man', 'Spiderman'}" multiple="true" headerKey="headerKey" headerValue="--- Please Select ---" emptyOption="true" doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" doubleName="rightSideCartoonCharacters" doubleHeaderKey="doubleHeaderKey" doubleHeaderValue="--- Please Select ---" doubleEmptyOption="true" doubleMultiple="true" /> <s:textarea label="Your Thougths" name="thoughts" tooltip="Enter your thoughts here" /> <s:submit onclick="alert('aaaa');" /> <s:reset onclick="alert('bbbb');" /> </s:form> </body> </html>
這些代碼是直接從Max的博文中copy過來的,本來這是一個完整的例子我只是把前臺展示代碼考過來了。在這里有一個要注意的地方,不過在代碼中也有體現,這是struts2比較高級的版本和低版本之間的區別,具體從哪個 版本改的我也不是很清楚。這個不同的用法是關于<s: datetimepicker>的。在使用<s: datetimepicker>時,較高的版本中還必須引入struts2-dojo-plugin-2.2.1.jar,還得指明<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> ,如果做實驗不成功的可以考慮下是不是這個問題。
struts-tags就算是復習到這里了,如果要是配合一個具體的項目的話就會更好了,繼續努力!!!