TiglibAction中的代码:
package com.yulin.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 TiglibAction extends Action {
@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //输出普通字符串 request.setAttribute("string", "输出普通string"); //输出html文件 request.setAttribute("html", "<font color='red'>输出html文件</font>"); //输出date request.setAttribute("date", new Date()); //输出number request.setAttribute("number", 12345.67); //输出User Group g = new Group(); g.setName("China"); User u = new User(); u.setUsername("LinZhang"); u.setAge(23); u.setGroup(g); request.setAttribute("user", u); return mapping.findForward("success"); }
}
User类代码:
package com.yulin.struts; 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.yulin.struts;
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 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><li>测试Beanwrite标签</li><hr>普通string输出:<br>string(jsp):<%=request.getAttribute("string") %><br>string(taglib):<bean:write name="string"/><hr>输出html文件:<br>html(default):<bean:write name="html"/><br>html(filter="true"):<bean:write name="html" filter="true"/><br>html(filter="false"):<bean:write name="html" filter="false"/><br><hr>输出date:<br>date(default):<bean:write name="date"/><br>date(format="yy-MM-dd HH:mm:ss"):<bean:write name="date" format="yy-MM-dd HH:mm:ss"/><br><hr>输出number:<br>number(default):<bean:write name="number"/><br>number(format="###,###.###"):<bean:write name="number" format="###,###.###"/><br>number(format="###,###.000"):<bean:write name="number" format="###,###.000"/><br><hr>输出User:<br>Name:<input type="text" value=" <bean:write name="user" property="username"/> " ><br>Age:<input type="text" value=" <bean:write name="user" property="age"/> "><br>Group:<input type="text" value=" <bean:write name="user" property="group.name"/> "><br></body></html>
浏览器显示出来的效果: