struts复习第四天


Struts的标签

bean,html,logic标签库,在Struts的标签也支持JSP2.0的EL表达式。

bean标签库
<bean:... >
bean标签库,是用来访为JavaBean的属性或者是为Bean的属性赋值,创建JavaBean,类似于JSP中的jsp:useBean动作的功能。
bean标签库中的标签中大部分都有以下的属性
id="xxx" 指定Bean的名字标识,Bean在被读出后将引用保存在以这个id命名的对象中,也就是在JSP中使用的变量的引用。
name="xxxx"指定要读取Bean的名字
property="xxxx"
scope="page|request|session|application"

资源读取标签
<bean:resource>读取资源文件
<bean:cookie>使用这个标签可以对Cookie进行操作
<bean:header>读取header信息

例:
<bean:resource id="indexpage" name="index.jsp"/>
<bean:write name="indexpage"/>

<bean:head id="head" name="user-agent"/>
<bean:write name="head"/>

<bean:cookie id="testcookie" name="/taglib/bean-cookie" value="emptyValue"/>
<%
 if(testcookie.getValue.equals("emptyValue")){
             javax.servlet.http.Cookie cook=new javax.servlet.http.Cookie("/taglib/bean-cookie","taglib cookie");
             cook.setComment("test");
             cook.setMaxAge(3600);
             response.addCookie(cook);
        }
%>

可以将资源写到页面的标签
<bean:write>将Bean的属性加入到输出流中
<bean:write name="xxx"/>name属性是要加入的资源,也就是先前在其他资源标签中的id属性定义的名字

<bean:define>定义变量
例:<bean:define id="test" value="test in Struts">
    <bean:write name="test"/>

<bean:message>读取消息,可以根据key读取消息。
例:
<bean:message key="org.test">

html标签库

<html:form>

对应html中的<form>,使用<html:form>会将表单中的属性自动封装成Form,他的action属性可以直接写struts的配置文件中的path


<html:text>、<html:password>、<html:textarea>、<html:hidden>、<html:submit>
<html:reset>、<html:checkbox>、<html:radio>、<html:select>、<html:option>
以上者写标签的使用是和html中的form相对应的。

<html:options>这个标签用来表示一组选择项
<%
   java.util.ArrayList list=new java.util.ArrayList();
   list.add(new org.apache.struts.util.LabelValueBean("show value","value"));
   list.add(new org.apache.struts.util.LabelValueBean("show value1","value1"));
   pageContext.setAttribute("vlauelist" list);
%>
<html:form action="xxx.do">
   <html:select property="test">
      <html:options collection="valuelist" property="value" labelProperty="label"/>
   </html:select>
</html:form>


Struts的Tiles框架

Tiles是一个框架,他实现了页面的复合视图,页面代码中不仅有数据,也有页面的布局格式。

要在基于Struts应用中使用Tiles框架,就要在struts-config.xml中配置
<plugin className="org.apache.struts.tiles.TilesPlugin">
  <set-property property="definintions-config" value="/WEB-INF/tiles-defs.xml">
  <!--定义tiles布局文件tiles-defs.xml-->
</plugin>

tiles的布局配置文件tiles-defs.xml

<tiles-definitions>
    <!-- 页面基本布局-->
    <definition name="pms_base" path="/common/pms_layout.jsp">
     <put name="title" value="pms title" />
        <put name="header" value="/common/header.jsp" />
        <put name="body"   value="some body page" />
        <put name="footer" value="/common/footer.jsp" />
    </definition>
   <!-- 其他页面定义-->
   <definition name="add" extends="pms_base">
        <put name="title" value="add" />
        <put name="body" value="/person_add.jsp" />
    </definition>
</tiles-definitions>

在struts-config.xml中要把forward的配置更改一下
<action path="/person/toadd"  type="alan.pms.action.PersonAction"
            name="personForm" scope="request"
            parameter="toAdd">
            <forward name="add" path="add"/>
</action>

这样就会使页面加上header.jsp和footer.jsp显示在配置好的页面中

在页面中使用tiles标签时,要引入标签库,<%@taglib uri="/WEB-INF/tiles.tld" prefix="tiles"%>

<tiles:insert page="xxx.jsp">
   <tiles:put name="header" value="header.jsp">
   <tiles:put name="footer" value="footer.jsp">
</tiles:insert>

在struts-config.xml中要把forward的配置更改一下
<action path="/person/toadd"  type="alan.pms.action.PersonAction"
            name="personForm" scope="request"
            parameter="toAdd">
            <forward name="add" path="add"/>
</action>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值