Struts自定义标签--操纵表格显示数据[Java编程]
本文“Struts自定义标签--操纵表格显示数据[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
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:" + e.toString());
}
return SKIP_PAGE;
}
@Override
public void release() {
// TODO Auto-generated method stub
super.release();
this.name = "alluser";
this.page = "login.jsp";
}
}
以上是“Struts自定义标签--操纵表格显示数据[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |