<bean:write>

 

对Struts中的BeanWrite标签做下测试(具体过程可以参见尚学堂的视频)

前提:好像必须要导入struts的国际化标签,有待验证。

第一步:添加BeanWriteTestAction类

Java代码 
  1. package com.bjsxt.struts; 
  2. /**
  3. * 测试BeanWrite
  4. */ 
  5. publicclass BeanWriteTestAction extends Action { 
  6.     public ActionForward execute(ActionMapping mapping, ActionForm form, 
  7.             HttpServletRequest request, HttpServletResponse response) 
  8.             throws Exception { 
  9.         //普通属性 
  10.         request.setAttribute("hello", "Hello World"); 
  11.          
  12.         //html文本 
  13.         request.setAttribute("bj", "<font color='red'>北京欢迎您</font>"); 
  14.          
  15.         //日期 
  16.         request.setAttribute("today", new Date()); 
  17.          
  18.         //数字 
  19.         request.setAttribute("n", 123456.987); 
  20.          
  21.         //结构 
  22.         Group group = new Group(); 
  23.         group.setName("尚学堂"); 
  24.          
  25.         User user = new User(); 
  26.         user.setUsername("张三"); 
  27.         user.setAge(18); 
  28.         user.setGroup(group); 
  29.          
  30.         request.setAttribute("user", user); 
  31.          
  32.         return mapping.findForward("success"); 
  33.     } 
  34.  
package com.bjsxt.struts;
/**
 * 测试BeanWrite
 */
public class BeanWriteTestAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//普通属性
		request.setAttribute("hello", "Hello World");
		
		//html文本
		request.setAttribute("bj", "<font color='red'>北京欢迎您</font>");
		
		//日期
		request.setAttribute("today", new Date());
		
		//数字
		request.setAttribute("n", 123456.987);
		
		//结构
		Group group = new Group();
		group.setName("尚学堂");
		
		User user = new User();
		user.setUsername("张三");
		user.setAge(18);
		user.setGroup(group);
		
		request.setAttribute("user", user);
		
		return mapping.findForward("success");
	}

}

 

第二步:添加测试的jsp页面(beanwrite.jsp)

Html代码
  1. <%@ page language="java"contentType="text/html; charset=GB18030" 
  2.     pageEncoding="GB18030"%> 
  3. <%@ taglib prefix="bean"uri="http://struts.apache.org/tags-bean"%>     
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  5. <html> 
  6. <head> 
  7. <metahttp-equiv="Content-Type"content="text/html; charset=GB18030"> 
  8. <title>测试BeanWrite</title> 
  9. </head> 
  10. <body> 
  11.     <h1>测试BeanWrite</h1> 
  12.     <hr> 
  13.     <li>普通字符串</li><br> 
  14.     hello(jsp脚本):<%=request.getAttribute("hello") %><br> 
  15.     hello(标签):<bean:writename="hello"/><br> 
  16.     <p> 
  17.     <li>html文本</li><br> 
  18.     bj(default=true):<bean:writename="bj"/><br> 
  19.     bj(filter="true"):<bean:writename="bj"filter="true"/><br> 
  20.     bj(filter="false"):<bean:writename="bj"filter="false"/><br> 
  21.     <p> 
  22.     <li>格式化日期</li><br> 
  23.     today(default):<bean:writename="today"/><br> 
  24.     today(format="yyyy-MM-dd HH:mm:ss"):<bean:writename="today"format="yyyy-MM-dd HH:mm:ss"/> 
  25.     <p> 
  26.     <li>格式化数字</li><br> 
  27.     n(default):<bean:writename="n"/><br> 
  28.     n(format="###,###.####"):<bean:writename="n"format="###,###.####"/><br> 
  29.     n(format="###,###.####"):<bean:writename="n"format="###,###.0000"/><br> 
  30.     <p> 
  31.     <li>结构</li><br> 
  32.     姓名:<inputtype="text"value="<bean:write name="user" property="username"/>"><br> 
  33.     年龄:<inputtype="text"value="<bean:write name="user" property="age"/>"><br> 
  34.     所属组:<inputtype="text"value="<bean:write name="user" property="group.name"/>"><br> 
  35.     </body> 
  36. </html> 
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>    
<!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>测试BeanWrite</title>
</head>
<body>
	<h1>测试BeanWrite</h1>
	<hr>
	<li>普通字符串</li><br>
	hello(jsp脚本):<%=request.getAttribute("hello") %><br>
	hello(标签):<bean:write name="hello"/><br>
	<p>
	<li>html文本</li><br>
	bj(default=true):<bean:write name="bj"/><br>
	bj(filter="true"):<bean:write name="bj" filter="true"/><br>
	bj(filter="false"):<bean:write name="bj" filter="false"/><br>
	<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="n"/><br>
	n(format="###,###.####"):<bean:write name="n" format="###,###.####"/><br>
	n(format="###,###.####"):<bean:write name="n" format="###,###.0000"/><br>
	<p>
	<li>结构</li><br>
	姓名:<input type="text" value="<bean:write name="user" property="username"/>"><br>
	年龄:<input type="text" value="<bean:write name="user" property="age"/>"><br>
	所属组:<input type="text" value="<bean:write name="user" property="group.name"/>"><br>
	</body>
</html>

 

第三步:显示出来的结果如下:

posted @ 2012-05-10 19:46  残星  阅读(5649)  评论(0编辑  收藏  举报