最近一直學習struts2+hibernate框架,因而想把兩個框架整合到一塊兒,作一個小的登陸項目。其餘很少說,直接看例子。css
1)、Struts2 和hibernate的環境配置html
包括jar包、web.xml以及hibernate.cfg.xml的配置,前面的文章有詳細的講述,這裏就不在一一敘述。java
2)、登陸界面實現(使用了bootstrap自適應框架,詳細內容見官網http://www.bootcss.com/)git
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陸</title> <link rel="stylesheet" href="styles/bootstrap.min.css"> <link rel="stylesheet" href="styles/MyOwn.css"> </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="navbar-header"></div> </nav> <div class="login-page center-block"> <div class="center-container"> <h2 class="color text-center">登陸系統</h2> <form class="form-horizontal center-block" role="form" action="login.action"> <div class="form-group "> <label class="col-sm-2 control-label">用戶名</label> <div class="col-sm-8"> <input type="text" class="form-control col-lg-sm" id="name" name="username" placeholder="用戶名"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">密碼</label> <div class="col-sm-8"> <input type="password" class="form-control" id="inputPassword3" name="pwd" placeholder="密碼"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox col-sm-5"> <label> <input type="checkbox"> 記住密碼 </label> </div> <a class=" col-sm-4 control-label" href="register.html">新用戶註冊</a> </div> </div> <div class="form-group "> <div class="col-sm-8 col-sm-offset-2"> <button type="submit" class="btn btn-primary btn-block" id="btn">登陸</button> </div> </div> </form> </div> </div> </body>
3)、生成實體類的對象關係映射文件,用戶( User.java)以及對應的User.hbm.xmlgithub
package com.entity; public class User { private Integer id; private String username; private String pwd; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public User(String username,String pwd) { this.username = username; this.pwd = pwd; } public User() { // TODO Auto-generated constructor stub } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-11-26 16:32:02 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.entity.User" table="USER"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" /> </property> <property name="pwd" type="java.lang.String"> <column name="PWD" /> </property> </class> </hibernate-mapping>
4)、建立用戶自定義SessionFactory工廠(Hibernateutil.java)web
package com.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class Hibernateutil { private static SessionFactory sessionFactory = null; // 採用單例模式 private Hibernateutil() { } public static SessionFactory getSessionFactory() { if (sessionFactory == null) { // 建立Configuration對象:對應hibernate的基本配置信息和對象關係映射信息 Configuration config = new Configuration().configure(); // 建立一個對象StandardServiceRegistry,configure("你的***.cfg.xml"),默認爲hibernate.cfg.xml StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure().build(); // 建立會話工廠對象 sessionFactory = config.buildSessionFactory(registry); return sessionFactory; } else return sessionFactory; } }
5)、建立用戶業務邏輯接口和類(UserImpl.java和UserDao.java)sql
UserImpl.javaapache
package com.service.impl; import com.entity.User; //用戶業務邏輯接口 public interface UserImpl { // login登陸 public boolean Login(User user); //註冊 public boolean Register(User user); }
UserDao.javabootstrap
package com.service.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.entity.User; import com.service.impl.UserImpl; import com.util.Hibernateutil; public class UserDao implements UserImpl { private Transaction ts = null; private String sql = ""; private Session session = null; @Override public boolean Login(User user) { try { getSession(); sql = "select username,pwd from User where username=?and pwd=?"; Query query = session.createQuery(sql); query.setParameter(0, user.getUsername()); query.setParameter(1, user.getPwd()); List list = query.list(); ts.commit(); if (!list.isEmpty()) return true; else return false; } catch (Exception ex) { ex.printStackTrace(); return false; } finally { session.close(); } } @Override public boolean Register(User user) { // TODO Auto-generated method stub try { getSession(); session.save(user); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } finally { session.close(); } } public void getSession() { session = Hibernateutil.getSessionFactory().openSession(); ts = session.beginTransaction(); } }
6)、設計用戶Action類session
UserAction.java
package com.action.userAction; import java.util.Map; import javax.enterprise.event.Observes; import org.apache.struts2.interceptor.RequestAware; import com.entity.User; import com.service.dao.UserDao; public class UserAction implements RequestAware { private String username; private String pwd; private Map<String, Object> request; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String Login() { UserDao userDao = new UserDao(); User user = new User(); user.setUsername(username); user.setPwd(pwd); if (userDao.Login(user)) { request.put("username", username); return "success"; } else return "error"; } public String Register() { UserDao userDao = new UserDao(); User user = new User(); user.setUsername(username); user.setPwd(pwd); if(userDao.Register(user)){ return "success"; }else return "failure"; } @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request = request; } }
7)、註冊struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 配置擴展名 --> <package name="struts" extends="struts-default" namespace="/"> <action name="login" class="com.action.userAction.UserAction" method="Login"> <result name="success">/index.html</result> <result name="error">/register.jsp</result> </action> <action name="register" class="com.action.userAction.UserAction" method="Register"> <result name="success">/index.html</result> <result name="failure">/login.html</result> </action> </package> </struts>
7)、效果預覽