struts2和struts1.x的标签库

struts2.0的标签库(简介)
用过struts1.x的人都知道,标签库有html、bean、logic、tiles,
而struts2.0里的标签却没有分类,只用在jsp头文件加上
<%@ taglib prefix="s" uri="/struts-tags" %>
就能使用struts2.0的标签库

下面就介绍下每个标签的用法(有错请指正):

A:

<s:a href=""></s:a>-----超链接,类似于html里的<a></a>
<s:action ></s:action>-----执行一个view里面的一个action
<s:actionerror/>-----如果action的errors有值那么显示出来
<s:actionmessage/>-----如果action的message有值那么显示出来
<s:append></s:append>-----添加一个值到list,类似于list.add();
<s:autocompleter></s:autocompleter>-----自动完成<s:combobox>标签的内容,这个是ajax

B:
<s:bean ></s:bean>-----类似于struts1.x中的,JavaBean的值

C:
<s:checkbox></s:checkbox>-----复选框
<s:checkboxlist list=""></s:checkboxlist>-----多选框
<s:combobox list=""></s:combobox>-----下拉框
<s:component></s:component>-----图像符号

D:
<s:date/>-----获取日期格式
<s:datetimepicker></s:datetimepicker>-----日期输入框
<s:debug></s:debug>-----显示错误信息
<s:div></s:div>-----表示一个块,类似于html的<div></div>
<s:doubleselect list="" doubleName="" doubleList=""></s:doubleselect>-----双下拉框

E:
<s:if test=""></s:if>
<s:elseif test=""></s:elseif>
<s:else></s:else>-----这3个标签一起使用,表示条件判断

F:
<s:fielderror></s:fielderror>-----显示文件错误信息
<s:file></s:file>-----文件上传
<s:form action=""></s:form>-----获取相应form的值

G:
<s:generator separator="" val=""></s:generator>----和<s:iterator>标签一起使用

H:
<s:head/>-----在<head></head>里使用,表示头文件结束
<s:hidden></s:hidden>-----隐藏值

I:
<s:i18n ></s:i18n>-----加载资源包到值堆栈
<s:include value=""></s:include>-----包含一个输出,servlet或jsp页面
<s:inputtransferselect list=""></s:inputtransferselect>-----获取form的一个输入
<s:iterator></s:iterator>-----用于遍历集合

L:
<s:label></s:label>-----只读的标签

M:
<s:merge></s:merge>-----合并遍历集合出来的值

O:
<s:optgroup></s:optgroup>-----获取标签组
<s:optiontransferselect doubleList="" list="" doubleName=""></s:optiontransferselect>-----左右选择框

P:
<s:param></s:param>-----为其他标签提供参数
<s:password></s:password>-----密码输入框
<s:property/>-----得到'value'的属性
<s:push value=""></s:push>-----value的值push到栈中,从而使property标签的能够获取value的属性

R:

<s:radio list=""></s:radio>-----单选按钮
<s:reset></s:reset>-----重置按钮


S:
<s:select list=""></s:select>-----单选框
<s:set ></s:set>-----赋予变量一个特定范围内的值
<s:sort comparator=""></s:sort>-----通过属性给list分类
<s:submit></s:submit>-----提交按钮
<s:subset></s:subset>-----为遍历集合输出子集

T:
<s:tabbedPanel ></s:tabbedPanel>-----表格框
<s:table></s:table>-----表格
<s:text ></s:text>-----I18n文本信息
<s:textarea></s:textarea>-----文本域输入框
<s:textfield></s:textfield>-----文本输入框
<s:token></s:token>-----拦截器
<s:tree></s:tree>-----树
<s:treenode label=""></s:treenode>-----树的结构

U:
<s:updownselect list=""></s:updownselect>-----多选择框
<s:url></s:url>-----创建url

struts1.x标签库 (收藏)struts标签库

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

<jsp:setProperty 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" > 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" />

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 message=”true”/>//如果为true则从全局中搜索

则:ActionMessages actionMessages=new ActionMessages();

actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(“key”));

saveMessages(request,actionMessages);

b.从一定的范围中查找:<html:messages >

<bean:write /> </html:messages>

则:protected void saveMessages(HttpServletRequest request,ActionMessages messages){

...request.setAttribute(GLOBAL_MESSAGE_key,messages); }

二:Struts Bean标签

1.访问http请求信息和JSP对象

a. <bean:cookie 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 property="value"/>

b. <bean:header /> <bean:write />

c. <bean:page property="session"/>//检索JSP范围,隐含对象

   <bean:write property="creationTime"/>

d. <bean:parameter value="noarg"/> // value为默认值

   <bean:write />

检索多值:<bean:parameter multiple="yes" 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" />// bundle不能省,只能访问资源文件

<bean:message bundle="special" property="name"/>

b. <bean:include>同<jsp:include>,但将WEB资源存放在一个变量中,有forward,page,href

<bean:include page="/testpage1.jsp"/> <bean:write filter="false"/>

<bean:include forward="testpage2"/> <bean:write filter="false"/>

3.定义或输出javaBean,bean:write标签filter为true时会将特殊符号转换成普通字符串

a.value属性:<bean:define value="lib"/><bean:write />

name和property属性:<% request.setAttribute("sessionBean", session); %>

     <bean:define property="servletContext"/>

// contextBean为javax.servlet.ServletContext类型,实例化

<bean:write property="servletContextName"/>

   name和type属性(用于复制):<bean:define

                    type="javax.servlet.ServletContext"/>

<bean:write 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 property="name" value="Tom">not Tom </logic:notEqual>

c. <% request.setAttribute("number","100"); %>

<logic:lessThan value="100.0a" > "100" 小于"100.0a"</logic:lessThan >

2.字符串匹配:变量中是否包含指定的字符串

<% request.setAttribute("authorName", "LindaSun");%>

a.<logic:match scope="request" value="Linda">

   <bean:write /> has the string 'Sun' in it.</logic:match>

<logic:notMatch scope="request" value="Linda" /> //还有end属性

<logic:match scope="request" value="Linda" location="start">

<bean:write /> 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 > emptyString is empty! </logic:empty>

b.<logic:notPresent property="noSuchProperty">

判断指定的安全角色,用户,cookie,header或javaBean是否存在 </logic:notPresent>

c. <logic:messagesPresent >在范围内检索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 />,与配置中<global-forwards>同名

b.<logic:redirect href="http://www.apache.org"/>,也有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 indexId="index" offset="1" length="2">

<bean:write />.<bean:write /><BR></logic:iterate>

b. 遍历Map<% HashMap h= new HashMap();String fruits[ ] = {"apple","orange","banana"};

h.put("Fruits", fruits); request.setAttribute("catalog", h); %>

<logic:iterate > <bean:write property="key"/><BR>

<logic:iterate property="value"><bean:write />

</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 path="/layout.jsp">

      <put value="header.jsp"/>

      <put value="indexContent.jsp"/>

      <put value="footer.jsp"/> </definition>

<definition path="/layout.jsp">

      <put value="sidebar.jsp"/>

      <put value="header.jsp"/>

      <put value="productContent.jsp"/>

      <put 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    path="/sidebar-layout.jsp">

      <put value="flags.jsp"/>        </definition>

   <definition    path="/layout.jsp">

      <put value="sidebar-definition" type="definition"/>

      <put value="header.jsp"/>

   </definition>       </tiles-definitions>//将一个组件put成另一个组件的元素

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值