日期:2014-05-17 浏览次数:20759 次
package com.fw.action; import java.sql.SQLException; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.fw.beans.User; import com.fw.dao.UserDao; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private UserDao userdao; public UserDao getUserdao() { return userdao; } public void setUserdao(UserDao userdao) { this.userdao = userdao; } private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void validate() { if("".equals(user.getUname())){ this.addFieldError("name", "<font color=red>用户名不能为空!</font>"); } if("".equals(user.getUpass())){ this.addFieldError("upass", "<font color=red>密码不能为空!</font>"); } super.validate(); } public String logic(){ /*System.out.println("----------"); User u=new User(); u.setUname("quan"); u.setUpass("lei"); userdao.adduser(u); */ /* User u=userdao.findid(5); System.out.println(u.getUpass()); */ User u; try { u = userdao.login(user.getUname(), user.getUpass()); } catch (Exception e) { System.out.println("登录失败!"); return null; //e.printStackTrace(); } System.out.println("登录成功!"); System.out.println(user.getUname()+"\n"+user.getUpass()); return null; } public String findall(){ System.out.println("--------"); List<User> list=userdao.findall(); for (User user : list) { System.out.println(user.getUname()); } HttpServletRequest request=ServletActionContext.getRequest(); request.setAttribute("list", list); return "list"; } }
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <%@ taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title></title> </head> <body> <a href="user!logic.action">----</a> <s:debug></s:debug> <s:form action="user!logic.action" method="post"> <s:textfield name="user.uname" label="用户名"></s:textfield> <s:fielderror></s:fielderror> <s:textfield name="user.upass" label="密码"></s:textfield> <s:submit value="logic"></s:submit> </s:form> <a href="user!findall.action">查询所有</a> </body> </html>