一.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"/>