Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30479954
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-12-10 09:53:48

1.app.tld

 



 1.0
 1.1
 
  display
  com.test.DisplayTag
  empty
  
   page
   true
   false
  

 

 

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("

");
    out.println("");
    out.println("");
    out.println("");
    out.println("");
    out.println("");
    for (int i = 0; i < users.size(); i++) {
     User user = (User) users.get(i);
     out.println("");
     out.println("");
     out.println("");
     out.println("");
    }
    out.println("
IDNamepassword
" + user.getId() + "" + user.getUsername() + "" + user.getPassword() + "
");
   } 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" %>

...

...

 

阅读(1826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~