struts+hibernate简单实例

工程已上传http://download.csdn.net/source/2195248

 

工程目录

 

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Insert title here

 

web.xml

…… action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 2 detail 2 2 action *.do …… 

struts-config.xml

 

LoginForm.java

package Form; import org.apache.struts.action.ActionForm; public class LoginForm extends ActionForm { private String username; private String password; //无参构造函数与Setter Getter方法必须 public LoginForm(){ } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }  

LoginAction.java

package Action; import org.apache.struts.action.*; import Form.LoginForm; import javax.servlet.http.*; import Hibernate.hibernate; import Bean.User; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request ,HttpServletResponse response){ //获得表单映射,取出属性值 LoginForm loginForm = (LoginForm)form; String username=loginForm.getUsername(); String password = loginForm.getPassword(); //hibernate持久层实现查询业务 hibernate hiber = new hibernate(); User user = hiber.queryUser(username); //页面映射跳转 if(user==null) return mapping.findForward("failure"); else if(!user.getPassword().equals(password)){ return mapping.findForward("failure"); } else return mapping.findForward("success"); } }  

User.java

package Bean; public class User { //实现Setter Getter方法,属性名字与User.hbm.xml配置文件内设置名对应 private String username; private String password; public User(String username,String password){ this.username=username; this.password=password; } public User(){} public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

User.hbm.xml

 

hibernate.cfg.xml

true com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/Wilford root root org.hibernate.dialect.MySQLDialect  

hibernate.java

package Hibernate; import org.hibernate.*; import org.hibernate.cfg.Configuration; import Bean.User; import java.util.*; public class hibernate { //会话工厂,用于创建会话 private static final SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory(); //返回指定username的User对象 public User queryUser(String username){ User user; ArrayList list; //hibernate会话 Session session=sessionFactory.openSession(); //hiberante事务 Transaction tx=session.beginTransaction(); String hql="from User as user where user.username=?"; list=(ArrayList)session.createQuery(hql).setString(0,username).list(); tx.commit(); session.close(); if(list.size()>0) user=(User)list.get(0); else user=null; return user; } }  

 

你可能感兴趣的:(JAVA,J2EE,hibernate,struts,string,user,class,getter)