一.html標簽
1.html元素的標簽:
a1.<html:link forward="index"> 鏈接<global-forwards>中的name </html:link>
a2. <html:link href="http://www.baidu.com" > 鏈接到站點外 </html:link>
a3. <html:link page="/HtmlBasic.do">同一個應用 </html:link>
如:包含請求參數: <html:link page="/HtmlBasic.do?prop1=abc&prop2=123" />
包含單個請求變量:<% String stringBean = "Value to Pass on URL";
pageContext.setAttribute("stringBean", stringBean);%>
<html:link page="/HtmlBasic.do" paramId="urlParamName" paramName="stringBean" />
<jsp:useBean id="javaBean" scope="page" class="CustomerBean" />
<jsp:setProperty name="javaBean" property="name" value="weiqin" /> //要有范圍
<html:link page="/Html" paramId="url" paramName="javaBean" paramProperty="name"/>
包含多個請求變量: <% java.util.HashMap myMap = new java.util.HashMap();
myMap.put("myString", new String("myStringValue") );
myMap.put("myArray", new String[] { "str1", "str2", "str3" });
pageContext.setAttribute("map", myMap);%>
<html:link page="/HtmlBasic.do" name="map"> url </html:link>
b.<html:img>: <html:img page="/struts-power.gif" /> //也可以包含單個或多個請求變量
<html:img src="/struts-power.gif" paramId="urlParamName" paramName="stringBean" />
<html:img page="/struts-power.gif" name="map" />
2.基本表單標簽:<html:form>,<html:text>,<html:hidden>,<html:submit>,<html:reset>
<html:cancel>Cancel</html:cancel> :Action中的取消事件:
FormBasicForm fbf = (FormBasicForm) form;
if (isCancelled(request)) { fbf.setStatus("Cancel was pressed!");
return (mapping.findForward("success")); }//表示取消選中
else { fbf.setStatus("Submit was pressed!");
return (mapping.findForward("success")); }
3.下拉列表和多選列表
<html:select property="colors" size="6" multiple="true" > // multiple下拉可多選
<html:option value="htmlselect.orange">Orange</html:option> //基本的頁面輸入
<html:option value="red" bundle="htmlselect.Colors" key="red"/>//從資源文件中顯示
<% Vector colorCollection = new Vector(); colorCollection.add(
new org.apache.struts.util.LabelValueBean("Pink", "htmlselect.pink"));
colorCollection.add( // Pink為label htmlselect.pink為value
new org.apache.struts.util.LabelValueBean("Brown", "htmlselect.brown"));
pageContext.setAttribute("colorCollection", colorCollection); %>
<html:options collection="colorCollection" property="value" //實際值
labelProperty="label" /> </html:select>// labelProperty顯示值
4.顯示錯誤消息: errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("key") );
a.顯示全局:<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE"/>
b.顯示特定字段: errors.add("checkbox1", new ActionMessage("error.checkbox"));
<html:errors property="checkbox1" bundle="HtmlErrors" /> //bundle資源文件
5.顯示信息<html:messages>:
a.<html:messages id=”message” message=”true”/>//如果為true則從全局中搜索
則:ActionMessages actionMessages=new ActionMessages();
actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(“key”));
saveMessages(request,actionMessages);
b.從一定的范圍中查找:<html:messages id=”message”>
<bean:write name=”message” /> </html:messages>
則:protected void saveMessages(HttpServletRequest request,ActionMessages messages){
...request.setAttribute(GLOBAL_MESSAGE_key,messages); }
二:Struts Bean標簽
1.訪問http請求信息和JSP對象
a. <bean:cookie id="cookie" name="cookiedemo" value="firsttime"/> // value為默認值
<% if (cookie.getValue().equals("firsttime")) {
Cookie c = new Cookie("cookiedemo", "Hi Linda!");
c.setComment("A test cookie");c.setMaxAge(3600);response.addCookie(c);} %>
輸出: <bean:write name="cookie" property="value"/>
b. <bean:header id="lang" name="Accept-Language"/> <bean:write name="lang"/>
c. <bean:page id="this_session" property="session"/>//檢索JSP范圍,隱含對象
<bean:write name="this_session" property="creationTime"/>
d. <bean:parameter id="arg1" name="testarg" value="noarg"/> // value為默認值
<bean:write name="arg1"/>
檢索多值:<bean:parameter id="arg2" multiple="yes" name="testarg" value="noarg"/>
通過鏈接傳遞參數:<html:link page="/this.jsp?testarg=123&testarg=456&testarg=789">
循環輸出:<% for (int i=0; i <arg2.length; i++) {out.write(arg2[i] + "<BR>");} %>
2.訪問WEB應用資源: <message-resources parameter="res" key="special" />
a. <bean:message key="hello" arg0="Linda" /> //默認資源文件中:hello=Hello,{0}
<% request.setAttribute("stringBean","hello"); SomeBean bean=new SomeBean();
bean.setName("hello"); request.setAttribute("someBean",bean); %>
通過變量名或javaBean得到key值再訪問資源文件:
<bean:message bundle="special" name="stringBean"/>// bundle不能省,只能訪問資源文件
<bean:message bundle="special" name="someBean" property="name"/>
b. <bean:include>同<jsp:include>,但將WEB資源存放在一個變量中,有forward,page,href
<bean:include id="tp1" page="/testpage1.jsp"/> <bean:write name="tp1" filter="false"/>
<bean:include id="tp2" forward="testpage2"/> <bean:write name="tp2" filter="false"/>
3.定義或輸出javaBean,bean:write標簽filter為true時會將特殊符號轉換成普通字符串
a.value屬性:<bean:define id="name" value="lib"/><bean:write name="name"/>
name和property屬性:<% request.setAttribute("sessionBean", session); %>
<bean:define id="contextBean" name="sessionBean" property="servletContext"/>
// contextBean為javax.servlet.ServletContext類型,實例化
<bean:write name="contextBean" property="servletContextName"/>
name和type屬性(用于復制):<bean:define id="contextBean_copy" name="contextBean"
type="javax.servlet.ServletContext"/>
<bean:write name="contextBean_copy" property="majorVersion"/>
三.Struts Logic標簽:
1.邏輯判斷:(greatEqual,lessEqual,greatThan...)
<% Cookie c = new Cookie("username", "Linda"); c.setComment("A test cookie");
c.setMaxAge(3600); response.addCookie(c); %>
a.<logic:equal cookie="username" value="Linda">UserName is Linda </logic:equal>
b. <% SomeBean bean=new SomeBean(); bean.setName("Linda");
request.setAttribute("someBean",bean);%>
<logic:notEqual name="someBean" property="name" value="Tom">not Tom </logic:notEqual>
c. <% request.setAttribute("number","100"); %>
<logic:lessThan name="number" value="100.0a" > "100" 小于"100.0a"</logic:lessThan >
2.字符串匹配:變量中是否包含指定的字符串
<% request.setAttribute("authorName", "LindaSun");%>
a.<logic:match name="authorName" scope="request" value="Linda">
<bean:write name="authorName"/> has the string 'Sun' in it.</logic:match>
<logic:notMatch name="authorName" scope="request" value="Linda" /> //還有end屬性
<logic:match name="authorName" scope="request" value="Linda" location="start">
<bean:write name="authorName"/> starts with the string 'Linda'.</logic:match>
3.判斷指定內容是否存在
<% ActionErrors errors = new ActionErrors();
errors.add("totallylost", new ActionMessage("application.totally.lost"));
request.setAttribute(Globals.ERROR_KEY, errors);
request.setAttribute("myerrors",errors);request.setAttribute("emptyString","");%>
a.<logic:empty name="emptyString"> emptyString is empty! </logic:empty>
b.<logic:notPresent name="noSuchBean" property="noSuchProperty">
判斷指定的安全角色,用戶,cookie,header或javaBean是否存在 </logic:notPresent>
c. <logic:messagesPresent name="myerrors" >在范圍內檢索key</logic:messagesPresent>
<logic:messagesNotPresent message="true">
從Globals.MESSAGE_KEY中檢索,不同于Globals.ERROR_KEY </logic:messagesNotPresent>
<logic:messagesNotPresent property="noSuchError">
從指定的ActionMessages對象中檢索 </logic:messagesNotPresent>
4.請求轉發或重定向a.<logic:forward name="index"/>,與配置中<global-forwards>同名
b.<logic:redirect />,也有page,href和forward三種屬性
5.循環遍歷//offset為開始位置,indexId為序號
a. 遍歷集合<% Vector animals=new Vector(); animals.addElement("Dog");
animals.addElement("Cat"); animals.addElement("Bird");
request.setAttribute("Animals", animals);%>
<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
<bean:write name="index"/>.<bean:write name="element"/><BR></logic:iterate>
b. 遍歷Map<% HashMap h= new HashMap();String fruits[ ] = {"apple","orange","banana"};
h.put("Fruits", fruits); request.setAttribute("catalog", h); %>
<logic:iterate id="map" name="catalog"> <bean:write name="map" property="key"/><BR>
<logic:iterate id="val" name="map" property="value"><bean:write name="val"/>
</logic:iterate> </logic:iterate> //如果value不是集合就不用嵌套了
四.利用Tiles模板和Tiles組件創建復合式網頁
1.在web.xml中配置所需要的<tablib>url和location
2.建立tiles的xml文件放在WEB-INF下:<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions> <definition name="index-definition" path="/layout.jsp">
<put name="header" value="header.jsp"/>
<put name="content" value="indexContent.jsp"/>
<put name="footer" value="footer.jsp"/> </definition>
<definition name="product-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar.jsp"/>
<put name="header" value="header.jsp"/>
<put name="content" value="productContent.jsp"/>
<put name="footer" value="footer.jsp"/> </definition>
</tiles-definitions>
3.在struts配置文件中配置TilesPlugin插件(ctrl+n):
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
<set-property property="definitions-parser-validate" value="true"/> </plug-in>
4.在Action已經存在ActionServlet,并在struts配置文件中配置Action來調用Tiles組件
<action path="/index" type="org.apache.struts.actions.ForwardAction"
parameter="index-definition"> </action>
ForwardAction為Action內置對象,專門負責轉發功能,在將請求轉發給parameter的組件
5.在index.jsp中插入Tiles組件<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert definition="index-definition"/>
6.在layout.jsp文件中進行布局并設計好其他頁面:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<table width="100%" height="100%">
<tr><td height="15%"><tiles:insert attribute="header"/> </td> </tr>
<tr><td valign="top"><tiles:insert attribute="content"/> </td> </tr>
<tr><td valign="bottom"><tiles:insert attribute="footer"/></td></tr> </table>
備注:在需要出現的頁面只有寫<tiles:insert definition="*"/>就可以插入框架,根據definition中的content改變主體。
7.Tiles組件的組合(type="definition")以示區別
<tiles-definitions>
<definition name="sidebar-definition" path="/sidebar-layout.jsp">
<put name="top" value="flags.jsp"/> </definition>
<definition name="index-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar-definition" type="definition"/>
<put name="header" value="header.jsp"/>
</definition> </tiles-definitions>//將一個組件put成另一個組件的元素
posted on 2007-08-20 20:23
Ke 閱讀(900)
評論(0) 編輯 收藏 所屬分類:
struts