Struts标签(2)Bean标签

来源:互联网 发布:java 超时异常 编辑:程序博客网 时间:2024/05/16 11:21
Struts标签(2)Bean标签


<bean:write>标签(重点掌握)

 ------------------------------------------------------------------------------------------------------------------------------------------------


1. <bean:define>标签

<%@ page language="java" pageEncoding="gbk"%>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

 

<html:html lang="true">

  <head>

   

    <title>BeanDemo01.jsp</title>

 

  </head>

 

<body>

<!--bean:define标记可以定义新bean,可以复制现有bean,也可以从现有bean复制属性-->

    <bean:define id="str" value="Hello JPO!!!"></bean:define>

    <h1><bean:write name="str"></bean:write></h1>

    <h1>${str}</h1>

  </body>

</html:html>

 

Hello JPO!!!

Hello JPO!!!

 

2. <bean:size>标签

<%@ page language="java" pageEncoding="gbk"%>

<%@ page import="java.util.*" %>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

 

<html:html lang="true">

  <head>

   

    <title>BeanDemo02.jsp</title>

 

  </head>

 

  <body>

   

    <%

      Map m = new HashMap();

      m.put("one","1");

      m.put("two","2");

      m.put("three","3");

      m.put("zero","0");

     

      request.setAttribute("map",m);

     

      Collection c = new ArrayList();

      c.add("ws");

      c.add("jpo");

      c.add("jp3");

     

      request.setAttribute("namecol",c);

     

    %>

   

    <!-- bean:size标签,求出长度:数组、CollectionMap -->

    <bean:size id="size" name="map" scope="request"/>

    <h1>Map长度是:${size}</h1>

   

    <bean:size id="si" name="namecol" scope="request"/>

    <h1>Collection长度是:${si}</h1>

   

  </body>

</html:html>

 

 

Map长度是:4

Collection长度是:3

 

3. <bean:write>标签(重点掌握)

<%@ page language="java" pageEncoding="gbk"%>

<%@ page import="org.ws.tag.*" %>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

 

<html:html lang="true">

  <head>

   

    <title>BeanDemo04.jsp</title>

 

  </head>

 

  <body>

   

    <!-- 推荐写法(使用标签) -->

    <jsp:useBean id="simple" class="org.ws.tag.SimpleBean" scope="request"></jsp:useBean>

    <jsp:setProperty name="simple" property="name" value="ws"/>

    <jsp:setProperty name="simple" property="password" value="jpo"/>

   

    <h1>使用JSP标签</h1>

    名称:<jsp:getProperty name="simple" property="name"/>

    密码:<jsp:getProperty name="simple" property="password"/>

   

    <!-- bean:write标签 -->

    <h1>使用bean标签</h1>

    名称:<bean:write name="simple" property="name" scope="request"/>

   密码:<bean:write name="simple" property="password" scope="request"/>

   

    <!-- JSP2.0的表达式语言EL(Expression Language) -->

    <!-- JSP2.0中,可以使用EL代替Struts中的bean:write标签 -->

    <h1>使用EL</h1>

    名称:${simple.name}

    密码:${simple.password}

   

  </body>

</html:html>

 

 

上面的推荐写法(使用标签),效果等同于下面的这段Java代码:

    <%

      SimpleBean si = new SimpleBean();

      si.setName("yyg");

      si.setPassword("juke");

      request.setAttribute("simple",si);

    %>

 

SimpleBean.java

 

package org.ws.tag;

 

public class SimpleBean {

     

      private String name;

      private String password;

     

      public String getName() {

            return name;

      }

      public void setName(String name) {

            this.name = name;

      }

      public String getPassword() {

            return password;

      }

      public void setPassword(String password) {

            this.password = password;

      }

     

}

 

 

使用JSP标签

名称:ws 密码:jpo

使用bean标签

名称:ws 密码:jpo

使用EL

名称:ws 密码:jpo

 

4. <bean:message>标签

 

<%@ page language="java" pageEncoding="gbk"%>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

 

<html:html lang="true">

  <head>

   

    <title>BeanDemo05.jsp</title>

 

  </head>

 

  <body>

      <!-- Struts国际化:根据浏览器的不同,自动进行语言的显示,自动去调用相应的属性文件 -->

      <!-- bean:message标签中提供了一个占位功能,在输出的文件中占着一位,这一位的数据等着标签来填写 -->

    <bean:message key="welcome" arg0="WS"/>

  </body>

</html:html>

 

 

ApplicationResources.properties

# Resources for parameter 'com.ws.struts.ApplicationResources'

# Project StrutsTag

welcome={0}, You are welcome!!!

 

WS, You are welcome!!!