随笔-8  评论-8  文章-10  trackbacks-0
        在早期jsp的开发中会用到大量的Scriptlet代码,这样造成了jsp页面维护性和可读性的下降,而Struts所带的标签库在一定程度上解决了此问题,所以我们提倡使用标签而不是用Scriptlet。
        
一、Bean标签
1.<bean:define> 定义或复制一个对象
    eg:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html lang="true">
  
<head>
    
<title>bean_define</title>
  
</head>
  
  
<body>
  
<bean:define id="str" value="Hello">
  
<h1>${str}</h1>
  
</body>
</html:html>

2.<bean:size>求出长度,数组、Collection、Map
    Collection eg:
<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html lang="true">
  
<head>
    
<title>bean_size</title>
  
</head>
  
  
<body>
  
<%
    Collection 
coll= new ArrayList() ;
    coll.add("北京") ;
    coll.add("上海") ;
    coll.add("西安") ;

    // 将Collection对象保存在四种属性范围之中
    request.setAttribute("coll",coll) ;
  %
>
  
<bean:size id="len" name="coll" scope="request"/>
  
<h1>长度是:${len}</h1>
  
</body>
</html:html>

    map eg:
<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html lang="true">
  
<head>
    
<title>bean_size</title>
  
</head>
  
  
<body>
  
<%
    Map 
= new HashMap() ;
    m.put("one","1") ;
    m.put("two","2") ;
    m.put("three","3") ;

    // 将Map对象保存在四种属性范围之中
    request.setAttribute("namemap",m) ;
  %
>
  
<bean:size id="len" name="namemap" scope="request"/>
  
<h1>长度是:${len}</h1>
  
</body>
</html:html>

3.<bean:write>打印对象或对象中的属性 类似于${}的功能
    eg:
    先编写一个Person类(JavaBean)
package com.illu;

public class Person {
    
private String name;
    
private String password;
    
public Person(){}
    
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页面
<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="com.illu.Person"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html lang="true">
  
<head>
    
<title>bean_write</title>
  
</head>
  
  
<body>
  
   <jsp:useBean id="person" class="com.illu.Person" scope="request"/>
          <jsp:setProperty name="person" property="name" value="illu"/>
          <jsp:setProperty name="person" property="password" value="123456789"/>

  
<h1>使用EL:</h1>
  
<h2>姓名:${person.name}</h2>
  
<h2>密码:${person.password}</h2>
  
<hr>
  
<h1>使用Bean标签:</h1>
  
<h2>姓名:<bean:write name="person" property="name" scope="request"/></h2>
  
<h2>密码:<bean:write name="person" property="password" scope="request"/></h2>
  
</body>
</html:html>

    4.<bean:message>Struts 国际化 或调用Struts的消息资源配置文件(用于保存显示信息)
    eg:
    首先在struts-config.xml中配置消息资源配置文件,加入一下代码即可
<message-resources parameter="illu.struts.ApplicationResources" />
    再在illu.struts下创建ApplicationResources.properties文件
    在文件中加入welcome = welcome {0} !
    {0}表示参数位置
    
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
  
<head>
    
<title>bean_message</title>
  
</head>
  
  
<body>
  
<bean:message key="welcome" arg0="illu"/>
  
</body>
</html:html>

    



每天进步一点点

posted on 2008-08-05 15:56 应越 阅读(246) 评论(0)  编辑  收藏 所属分类: struts学习

只有注册用户登录后才能发表评论。


网站导航: