show.jsp <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>beanWrite标签</title> </head> <body> <h1> 测试beanwrite </h1> <li> 普通字符串 </li> <br> (hello)jsp脚本:<%=request.getAttribute("hello")%><br> (hello)struts标签: <bean:write name="hello" /> <p> <li> html文本 </li> <br> 默认 bj: <bean:write name="bj" /> <br> bj (filter="true"): <bean:write name="bj" filter="true" /> <br> bj (filter="false"): <bean:write name="bj" filter="false" /> <p> <li> 格式化日期 </li> <br> today : <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> num : <bean:write name="num" /> <br> num format="####,####.####" : <bean:write name="num" format="####,####.####" /> <br> num format="####,####.0000" : <bean:write name="num" format="####,####.0000" /> <br><p> <li> 结构 对象 </li> <br> 小组: <input type="text" value="<bean:write name="user" property="group.groupname"/>" /> <br> 名字: <input type="text" value="<bean:write name="user" property="username"/>" /> <br> 年龄: <input type="text" value="<bean:write name="user" property="age"/>" /> <br> </body> </html> web.action package web.action; 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; import entity.Group; import entity.User; /** * BeanWrite标签 */ public class BeanWriteAction 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("num", 12345.678); // 结构 Group group = new Group(); group.setGroupname("第一小组"); User user = new User(); user.setUsername("张三"); user.setAge(18); user.setGroup(group); request.setAttribute("user", user); return new ActionForward("/show.jsp"); } } entity package entity; public class Group { private String groupname; public String getGroupname() { return groupname; } public void setGroupname(String groupname) { this.groupname = groupname; } } package entity; 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; } } 配置文件 struts-config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans /> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/beanwrite" type="web.action.BeanWriteAction"></action> </action-mappings> <message-resources parameter="web.ApplicationResources" /> </struts-config> 效果图