Spring3整合Struts2+Hibernate 實現註冊、資料庫驗證登陸
阿新 • • 發佈:2019-01-10
1 web.xml 與上一篇一樣
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INF/classes/applicationContext.xml </param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
2 struts.xml 配置兩個action 分別實現註冊和登陸
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true"/> <package name="app" extends="struts-default"> <action name="registPro" class="app.action.RegistAction"> <result name="error">error.jsp</result> <result name="success">welcome.jsp</result> </action> <action name="loginPro" class="app.action.LoginAction"> <result name="error">error.jsp</result> <result name="success">welcome.jsp</result> </action> <action name="*"> <result>{1}.jsp</result> </action> </package> </struts>
3 applicationContext.xml 原本Spring用來配置bean,整合後,Hibernate資料庫引數也在此配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORACLE"> </property> <property name="username" value="TESTUSER"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true; </value> </property> <property name="mappingResources"> <list> <value>app/domain/User.hbm.xml</value> </list> </property> </bean> <bean id="userDao" class="app.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="registService" class="app.service.impl.RegistServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <bean id="loginService" class="app.service.impl.LoginServiceImpl"> <property name="userDao" ref="userDao"/> </bean> </beans>
registService loginService 依賴userDao,userDao依賴sessionFactory,sessionFactory可以用dataSource來配置
4 domain類,User.java,對映資料庫表。及對映檔案User.hbm.xml
package app.domain;
public class User {
private String account;
private String password;
public User(){
}
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="app.domain.User" table="loginRegistUser">
<id name="account" column="account"
type="string">
</id>
<property name="password" type="string"/>
</class>
</hibernate-mapping>
5 Dao類,UserDao.java及UserDaoImpl.java 。繼承HibernateDaoSupport類可以方便的對User持久化類進行操作,需要傳入引數sessionFactory(在bean中配置)
package app.dao;
import app.domain.User;
public interface UserDao {
User get(String account);
String save(User user);
void Update(User user);
void Delete(String account);
}
package app.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import app.dao.UserDao;
import app.domain.User;
public class UserDaoImpl extends HibernateDaoSupport
implements UserDao {
public void Delete(String account) {
// TODO Auto-generated method stub
getHibernateTemplate().delete(get(account));
}
public void Update(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().update(user);
}
public User get(String account) {
// TODO Auto-generated method stub
return getHibernateTemplate().get(User.class,account);
}
public String save(User user) {
// TODO Auto-generated method stub
return (String)(getHibernateTemplate().save(user));
}
}
6 Service類,LoginService·.java RegistService.java LoginServiceImpl.java RegistServiceImple.java 。依賴UserDao實現註冊、登陸業務
package app.service;
import app.domain.User;
public interface LoginService {
boolean login(User user);
}
package app.service;
import app.domain.User;
public interface RegistService {
boolean regist(User user);
}
package app.service.impl;
import app.dao.UserDao;
import app.domain.User;
import app.service.LoginService;
public class LoginServiceImpl implements LoginService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean login(User user) {
// TODO Auto-generated method stub
User libUser=userDao.get(user.getAccount());
if(user.getPassword().equals(libUser.getPassword())){
return true;
}else{
return false;
}
}
}
package app.service.impl;
import app.dao.UserDao;
import app.domain.User;
import app.service.RegistService;
public class RegistServiceImpl implements RegistService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean regist(User user) {
// TODO Auto-generated method stub
String result=userDao.save(user);
if(result.equals(user.getAccount())){
return true;
}else{
return false;
}
}
}
6 action類,LoginAction.java RegistLogin.java 依賴Service類
package app.action;
import app.domain.User;
import app.service.LoginService;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
private User user;
private String tip;
private LoginService loginService;
public String execute() throws Exception {
// TODO Auto-generated method stub
if(loginService.login(user)){
setTip(user.getAccount()+"歡迎登陸");
return SUCCESS;
}else{
return ERROR;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}
package app.action; import app.domain.User; import app.service.RegistService; import com.opensymphony.xwork2.Action; public class RegistAction implements Action { //下面是用於封裝使用者請求引數的屬性 private User user; //用於封裝處理結果的屬性 private String tip; //系統所用的業務邏輯元件 private RegistService registService; //設定注入業務邏輯元件所必需的setter方法 //處理使用者請求的execute方法 public String execute() throws Exception { //呼叫業務邏輯元件的regist方法來處理請求 if (registService.regist(user)) { setTip("哈哈,註冊成功!"); return SUCCESS; } else { return ERROR; } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public void setRegistService(RegistService registService) { this.registService = registService; } }
7 home.jsp error.jsp welcome.jsp
<%--
網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
author yeeku.H.lee [email protected]
version 1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用者註冊/登陸</title>
</head>
<body>
<h3>使用者註冊</h3>
<s:form action="registPro">
<s:textfield name="user.account" label="賬戶"/>
<s:password name="user.password" label="密碼"/>
<tr>
<td colspan="2">
<s:submit value="註冊" theme="simple"/>
<s:reset value="重填" theme="simple"/></td>
</tr>
</s:form>
<h3>使用者登入</h3>
<s:form action="loginPro">
<s:textfield name="user.account" label="賬戶"/>
<s:password name="user.password" label="密碼"/>
<tr>
<td colspan="2">
<s:submit value="登陸" theme="simple"/>
<s:reset value="重填" theme="simple"/></td>
</tr>
</s:form>
</body>
</html>
<%--
網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
author yeeku.H.lee [email protected]
version 1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>錯誤頁面</title>
</head>
<body>
失敗!
</body>
</html>
<%--
網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
author yeeku.H.lee [email protected]
version 1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>成功頁面</title>
</head>
<body>
您已經登入!
<s:property value="tip"/>
</body>
</html>