一口一口吃掉Struts(五)——测试Struts标签库

来源:互联网 发布:如何在手机开淘宝网店 编辑:程序博客网 时间:2024/05/17 04:16

Struts标签库的使用和JSTL标签库使用方式类似。

认识可以了,建议熟练掌握JSTL标签库。struts标签库基本认识就可以了。

 

核心操作:

配置:
 * 配置国际化支持,在struts-config.xml文件中加入如下配置:
 <message-resources parameter="MessageResources" />
 * 最好提供国际化资源文件(MessageResources.properties),将该文件放到src下
 
使用:
 采用taglib引入
 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>    
 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>   

 

测试代码:

ACTION

package com.jialin;import java.util.ArrayList;import java.util.Date;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 TestAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {//普通字符串request.setAttribute("hello", "helloworld");//html文本request.setAttribute("world", "<font color='red'>Hello World!</font>");//日期request.setAttribute("today", new Date());//数字request.setAttribute("number", 12345.1234);Group group = new Group();group.setName("师院");User user = new User();user.setUsername("张三");user.setAge(18);user.setGroup(group);//结构数据request.setAttribute("user", user);//空request.setAttribute("attr1", null);request.setAttribute("attr2", "");request.setAttribute("attr3", new ArrayList());//ListList userList = new ArrayList();for (int i=0; i<10; i++) {User user1 = new User();user1.setUsername("张三_" + i);user1.setAge(18 + i);user1.setGroup(group);userList.add(user1);}request.setAttribute("userlist", userList);return mapping.findForward("showresult");}}

User

package com.jialin;public class User {private String username;private int age;private Group group;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Group getGroup() {return group;}public void setGroup(Group group) {this.group = group;}}

Group

package com.jialin;public class Group {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}

 

JSP

<%@ page language="java" contentType="text/html; charset=GB18030"    pageEncoding="GB18030"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>     <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>Insert title here</title></head><body><h1>测试beanwrite标签</h1><li>普通字符串</li><br>hello(标签):<bean:write name="hello"/><p></p><li>html文本</li><br>world(default):<bean:write name="world"/><br/>world(filter true):<bean:write name="world" filter="true"/><br/>world(filter false):<bean:write name="world" filter="false"/><p><li>格式化日期</li><br>today(default):<bean:write name="today"/><br>today(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/><p><li>格式化数字</li><br>n(default):<bean:write name="number"/><br>n(format="###,###.#####"):<bean:write name="number" format="###,###.#####"/><br>n(format="###,###.00000"):<bean:write name="number" format="###,###.00000#"/><br><P><li>结构</li><br>姓名:<input type="text" name="username" value="<bean:write name="user" property="username"/>"> <br>年龄:<input type="text" name="userage" value="<bean:write name="user" property="age"/>"> <br>所属组:<input type="text" name="usergroup" value="<bean:write name="user" property="group.name"/>"> <br><h1>测试logic:empty,logic:notEmpty,logic:present,logic:notPresent</h1><hr><logic:empty name="attr1">atrr1为空<br></logic:empty><logic:notEmpty name="attr1">atrr1不为空<br></logic:notEmpty><logic:present name="attr1">atrr1存在<br></logic:present><logic:notPresent name="attr1">atrr1不存在<br></logic:notPresent><p><logic:empty name="attr2">atrr2为空<br></logic:empty><logic:notEmpty name="attr2">atrr2不为空<br></logic:notEmpty><logic:present name="attr2">atrr2存在<br></logic:present><logic:notPresent name="attr2">atrr2不存在<br></logic:notPresent><p><logic:empty name="attr3">atrr3为空<br></logic:empty><logic:notEmpty name="attr3">atrr3不为空<br></logic:notEmpty><logic:present name="attr3">atrr3存在<br></logic:present><logic:notPresent name="attr3">atrr3不存在<br></logic:notPresent><p><h1>测试迭代标签</h1><hr><table border="1"><tr><td>姓名</td><td>年龄</td><td>所属组</td></tr><logic:empty name="userlist"><tr><td colspan="3">没有符合条件的数据</td></tr></logic:empty><logic:notEmpty name="userlist"><logic:iterate id="user" name="userlist"><tr><td><bean:write name="user" property="username"/></td><td><bean:write name="user" property="age"/></td><td><bean:write name="user" property="group.name"/></td></tr></logic:iterate></logic:notEmpty></table></body></html>


测试结果:

 

下篇继续……

 

 

原创粉丝点击