对Struts中的BeanWrite标签做下测试(具体过程可以参见尚学堂的视频)
前提:好像必须要导入struts的国际化标签,有待验证。
第一步:添加BeanWriteTestAction类
- package com.bjsxt.struts;
- /**
- * 测试BeanWrite
- */
- publicclass 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");
- }
- }
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", "北京欢迎您"); //日期 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)
- <%@ 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>
- <metahttp-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:writename="hello"/><br>
- <p>
- <li>html文本</li><br>
- bj(default=true):<bean:writename="bj"/><br>
- bj(filter="true"):<bean:writename="bj"filter="true"/><br>
- bj(filter="false"):<bean:writename="bj"filter="false"/><br>
- <p>
- <li>格式化日期</li><br>
- today(default):<bean:writename="today"/><br>
- today(format="yyyy-MM-dd HH:mm:ss"):<bean:writename="today"format="yyyy-MM-dd HH:mm:ss"/>
- <p>
- <li>格式化数字</li><br>
- n(default):<bean:writename="n"/><br>
- n(format="###,###.####"):<bean:writename="n"format="###,###.####"/><br>
- n(format="###,###.####"):<bean:writename="n"format="###,###.0000"/><br>
- <p>
- <li>结构</li><br>
- 姓名:<inputtype="text"value="<bean:write name="user" property="username"/>"><br>
- 年龄:<inputtype="text"value="<bean:write name="user" property="age"/>"><br>
- 所属组:<inputtype="text"value="<bean:write name="user" property="group.name"/>"><br>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>测试BeanWrite 测试BeanWrite
第三步:显示出来的结果如下: