Struts自定义标签--使用表格显示数据
时间:2011-03-20
1.app.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.1</jsp-version>
<tag>
<name>display</name>
<tagclass>com.test.DisplayTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
2.DisplayTag.java
package com.test;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.test.Hibernate.User;
public class DisplayTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name = "alluser";
private String page;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
public int doEndTag() throws JspException {
boolean valid = false;
HttpSession session = pageContext.getSession();
if (session != null && session.getAttribute(name) != null) {
valid = true;
} else {
try {
pageContext.forward(page);
} catch (Exception e) {
// TODO: handle exception
throw new JspException(e.toString());
}
}
JspWriter out = pageContext.getOut();
try {
List users = (List) session.getAttribute("alluser");
if (valid) {
out.println("<table border=\"2\" width=''100%'' height=''100%''>");
out.println("<tr>");
out.println("<th>ID</th>");
out.println("<th>Name</th>");
out.println("<th>password</th>");
out.println("</tr>");
for (int i = 0; i < users.size(); i++) {
User user = (User) users.get(i);
out.println("<tr>");
out.println("<td>" + user.getId() + "</td>");
out.println("<td>" + user.getUsername() + "</td>");
out.println("<td>" + user.getPassword() + "</td>");
}
out.println("</table>");
} else {
out.println("null");
}
} catch (Exception e) {
// TODO Auto-generated catch block
throw new JspTagException("IOException:&
|