Struts 1 学习笔记-2-2(Struts标签的测试用例)

本人共测试了<bean : write>,以及其中的property,format等,<logic : empty> ,<logic : notEmpty>,<logic : present>,<logic : notPresent>,<logic : iterate>

在这里我就只贴出几个关键的代码,如有人需要全部代码,可联系本人。

1.struts-config.xml:

<? xml version="1.0" encoding="ISO-8859-1"  ?>

<! DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

< struts-config >
    
< action-mappings >
        
        
< action  path ="/beanwrite"  type ="com.codedestiny.struts.BeanWriteTestAction"  scope ="request" >
            
< forward  name ="success"  path ="/beanwrite.jsp" ></ forward >
        
</ action >
        
        
< action  path ="/emptypresent"  type ="com.codedestiny.struts.EmptyPresentTestAction"  scope ="request" >
            
< forward  name ="success"  path ="/emptypresent.jsp" ></ forward >
        
</ action >
        
        
< action  path ="/iterate"  type ="com.codedestiny.struts.IterateTestAction"  scope ="request" >
            
< forward  name ="success"  path ="/iterate.jsp" ></ forward >
        
</ action >
        
    
</ action-mappings >     
    
< message-resources  parameter ="MessageResources"   />
</ struts-config >

2.BeanWriteTestAction.java

package  com.codedestiny.struts;

import  java.util.Date;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

public   class  BeanWriteTestAction  extends  Action  {
    
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute(
"hello""Hello world");
        request.setAttribute(
"cd""<font color='red' size='10'>成都欢迎您</font>");
        request.setAttribute(
"today"new Date());
        request.setAttribute(
"number"1234456789.987654321);
        
        Group relatedGroup 
= new Group();
        relatedGroup.setName(
"西华大学");
        User u 
= new User();
        u.setUsername(
"CodeDestiny");
        u.setAge(
21);
        u.setRelatedGroup(relatedGroup);
        
        request.setAttribute(
"user", u);
        
        
        
return mapping.findForward("success");
    }

}

相应的beanwrite.jsp:

<% @ page language="java" import="java.util.*" pageEncoding="GB18030" %>
<% @ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"  %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >
    
< title > BeanWrite </ title >
  
</ head >
  
  
< body >
    
< h1 > Bean Write 测试 </ h1 >
    
    
< li > 普通字符串 </ li >< br >   
    hello(jsp):
<% =  request.getAttribute( " hello " %> < br >
    hello(tag):
< bean:write  name ="hello" />< br >
    
    
< hr >
    
    
< li > html文本 </ li >< br >
    cd(defualt):
< bean:write  name ="cd" />< br >    
    cd(filter=true):
< bean:write  name ="cd"  filter ="true" />< br >
    cd(filter=false):
< bean:write  name ="cd"  filter ="false" />< br >  
    
    
< hr >
    
    
< li > 日期格式化 </ li >< br >
    today(default):
< bean:write  name ="today" />< br >
    today(format):
< bean:write  name ="today"  format ="yyyy-MM-dd HH:mm:ss" />     
    
    
< hr >
    
    
< li > 数字格式化 </ li >< br >
    number(default):
< bean:write  name ="number" />< br >
    number(format:###,####.00):
< bean:write  name ="number"  format ="###,###.00" />< br >  
    
    
< hr >
    
    
< li > 结构 </ li >< br >
    name:
< bean:write  name ="user"  property ="username" />< br >
    age:
< bean:write  name ="user"  property ="age" />< br >
    relatedGroup:
< bean:write  name ="user"  property ="relatedGroup.name" />< br >
    
  
</ body >
</ html >

3.EmptyPresentTestAction.java:

package  com.codedestiny.struts;

import  java.util.ArrayList;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

public   class  EmptyPresentTestAction  extends  Action  {

    
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        request.setAttribute(
"attr2""");
        request.setAttribute(
"attr3"new ArrayList());
        
        
        
return mapping.findForward("success");
    }

}

相应的emptypresent.jsp:

<% @ page language="java" import="java.util.*" pageEncoding="GB18030" %>
<% @ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >
  
</ head >
  
  
< body >
    
< h1 > 测试 logic:empty,notEmpty 以及 logic:present,notPresent </ h1 >
    
       
< hr >
    
< logic:empty  name ="attr1" > attr1为空 </ logic:empty >
    
< logic:notEmpty  name ="attr1" > attr1不为空 </ logic:notEmpty >< br >
    
< logic:present  name ="attr1" > attr1存在 </ logic:present >
    
< logic:notPresent  name ="attr1" > attr1不存在 </ logic:notPresent >< br >
    
    
< hr >
    
< logic:empty  name ="attr2" > attr2为空 </ logic:empty >
    
< logic:notEmpty  name ="attr2" > attr2不为空 </ logic:notEmpty >< br >
    
< logic:present  name ="attr2" > attr2存在 </ logic:present >
    
< logic:notPresent  name ="attr2" > attr2不存在 </ logic:notPresent >< br >
    
    
< hr >
    
< logic:empty  name ="attr3" > attr3为空 </ logic:empty >
    
< logic:notEmpty  name ="attr3" > attr3不为空 </ logic:notEmpty >< br >
    
< logic:present  name ="attr3" > attr3存在 </ logic:present >
    
< logic:notPresent  name ="attr3" > attr3不存在 </ logic:notPresent >< br >
    
  
</ body >
</ html >

 

4.IterateTestAction.java:

package  com.codedestiny.struts;
import  java.util.ArrayList;
import  java.util.List;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;


public   class  IterateTestAction  extends  Action  {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        List
<User> users = new ArrayList<User>();
        Group relatedGroup 
= new Group();
        relatedGroup.setName(
"Xihua Univercity");
        
for(int i=1; i<10; i++{
            User u 
= new User();
            u.setUsername(
"user" + i);
            u.setAge(
20 + i);
            u.setRelatedGroup(relatedGroup);
            users.add(u);
        }

        
        request.setAttribute(
"users", users);        
        
        
return mapping.findForward("success");
    }

    
}

 

相应的iterate.jsp:

<% @ page language="java" import="java.util.*" pageEncoding="GB18030" %>
<% @ page import="com.codedestiny.struts.*"  %>
<% @ page import="java.util.*"  %>
<% @ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"  %>
<% @ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"  %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >
  
</ head >
  
  
< body >
      
< h1 > 测试logic:iterate </ h1 >
      
      
< hr >
      常规:
< br >
    
< table  border ="1" >
        
< tr >< td > 姓名 </ td >< td > 年龄 </ td >< td > 所属组 </ td ></ tr >
        
<%
            List
<User> users = (ArrayList<User>)request.getAttribute("users");
            
if(users == null || users.size() == 0) {
                out.println(
"<tr><td colspan=3>没有任何相关数据</td></tr>");
            } 
else {
                
for(Iterator<User> it=users.iterator(); it.hasNext(); ) {
                    User u 
= it.next();
                    
%>
                        
< tr >
                            
< td > <% =  u.getUsername()  %> </ td >
                            
< td > <% =  u.getAge()  %> </ td >
                            
< td > <% =  u.getRelatedGroup().getName()  %> </ td >
                        
</ tr >
                    
<%
                }
            }
            
        
%>
    
</ table >
    
    
< hr >
    使用logic:iterate,empty,notEmpty 并结合 bean:write,property
< br >
    
    
< table  border ="1" >
        
< tr >< td > 姓名 </ td >< td > 年龄 </ td >< td > 所属组 </ td ></ tr >
        
        
< logic:empty  name ="users" >
            
< tr >< td  colspan =3 > 没有任何相关数据 </ td ></ tr >
        
</ logic:empty >
        
        
< logic:notEmpty  name ="users" >
            
< logic:iterate  id ="user"  name ="users" >
                
< tr >
                    
< td >< bean:write  name ="user"  property ="username" /></ td >
                    
< td >< bean:write  name ="user"  property ="age" /></ td >
                    
< td >< bean:write  name ="user"  property ="relatedGroup.name" /></ td >
                
</ tr >
            
</ logic:iterate >
        
</ logic:notEmpty >
        
    
</ table >
    
  
</ body >
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值