分类: Java
2008-12-10 09:53:48
1.app.tld
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("
ID | ");Name | ");password | ");
---|---|---|
" + user.getId() + " | ");" + user.getUsername() + " | ");" + user.getPassword() + " | ");
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" %>
...
...