程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts自定義標簽--使用表格顯示數據

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";
}
}

3.LoginAction.java

package com.test.struts.action;
   import java.util.List;
   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 org.apache.struts.validator.DynaValidatorForm;
   import com.test.Hibernate.UserDAO;
   public class LoginAction extends Action {
   private UserDAO userDao;
   public UserDAO getUserDao() {
  return userDao;
}
   public void setUserDao(UserDAO userDao) {
  this.userDao = userDao;
}
   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  DynaValidatorForm loginForm = (DynaValidatorForm) form;
  String username = loginForm.getString("username");
  String password = loginForm.getString("password");
  if (userDao.isValidUser(username, password)) {
  List users = userDao.showAll();//在userDao中寫操作數據庫的方法,其中,showAll()方法是取出數據庫中說有數據
  request.setAttribute("user", username);
  request.getSession().setAttribute("alluser", users);
  return mapping.findForward("indexGo");
  } else {
  return mapping.getInputForward();
  }
}
}

4.使用

...
   <%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
   ...
   <app:display page="login.jsp"/>
   ...

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved