整合Spring+hibernate+struts2搭建步骤
1.idea创建新项目
2.导入项目相关jar包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
3.创建hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">scott</property>
<property name="connection.password">orcl</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<mapping resource="mapping/Account.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.创建web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
5.创建struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="web" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.intercept.myInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-results>
<result name="loginout">index.jsp</result>
</global-results>
<action name="login" class="com.action.AccountAction" method="login">
<result name="success">menu.jsp</result>
<result name="input">err.jsp</result>
</action>
<action name="modifyAccountMoney" class="com.action.AccountAction" method="modifyAccountMoney">
<result name="success">success.jsp</result>
<result name="input">err.jsp</result>
</action>
<action name="getOut" class="com.action.AccountAction" method="getOut">
<result name="success">index.jsp</result>
<result name="input">err.jsp</result>
</action>
</package>
</struts>
6.编写Myfilter过滤器
package com.filter;
import com.util.HibernateDao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req =(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse)response;
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
Session session = null;
Transaction transaction = null;
try {
session = HibernateDao.getCurrentSession();
transaction = session.beginTransaction();
filterChain.doFilter(req,resp);
transaction.commit();
}catch (Exception e){
transaction.rollback();
}
}
public void destroy() {
}
}
7.编写MyInterceptor拦截器
package com.intercept;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.Map;
public class myInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation ai) throws Exception {
Object users = ai.getInvocationContext().getSession().get("user");
String name = ai.getInvocationContext().getName();
if(null!=users||"login".equals(name)){
return ai.invoke();
}
return "loginout";
}
}
8.编写工具类HibernateDao
package com.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateDao {
private static SessionFactory sf=null;
static {
Configuration conf=new Configuration().configure();
sf=conf.buildSessionFactory();
}
public static Session openSession(){
return sf.openSession();
}
public static Session getCurrentSession(){
return sf.getCurrentSession();
}
}
9.编写实体类
package com.entity;
import java.util.Date;
public class Account {
private Integer id;
private String accountNumber;
private String accountPwd;
private Float accountMoney;
private Integer accountStatus;
private Date creationTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountPwd() {
return accountPwd;
}
public void setAccountPwd(String accountPwd) {
this.accountPwd = accountPwd;
}
public Float getAccountMoney() {
return accountMoney;
}
public void setAccountMoney(Float accountMoney) {
this.accountMoney = accountMoney;
}
public Integer getAccountStatus() {
return accountStatus;
}
public void setAccountStatus(Integer accountStatus) {
this.accountStatus = accountStatus;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
}
10.编写dao层,service层
package com.dao.impl;
import com.dao.AccountDao;
import com.entity.Account;
import com.util.HibernateDao;
import org.hibernate.Session;
public class AccountDaoImpl implements AccountDao {
public Account getAccountByNumAndPwd(Account account) {
Session session = HibernateDao.getCurrentSession();
String hql="from Account where accountNumber=:accountNumber and accountPwd=:accountPwd";
Account account1 = (Account)session.createQuery(hql).setProperties(account).uniqueResult();
return account1;
}
public Account modifyAccountMoney(Integer id, Float reaccount) {
Session session = HibernateDao.getCurrentSession();
Account a =(Account) session.get(Account.class,id);
a.setAccountMoney(a.getAccountMoney()-reaccount);
return a;
}
}
package com.service.impl;
import com.dao.AccountDao;
import com.dao.impl.AccountDaoImpl;
import com.entity.Account;
import com.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao = new AccountDaoImpl();
public Account getAccountByNumAndPwd(Account account) {
return accountDao.getAccountByNumAndPwd(account);
}
public Account modifyAccountMoney(Account account, Float reaccount) {
return accountDao.modifyAccountMoney(account.getId(),reaccount);
}
}
11.编写数据库映射文件 Account.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.entity.Account" >
<id name="id">
<generator class="increment"></generator>
</id>
<property name="accountNumber" column="account_number"/>
<property name="accountPwd" column="account_pwd"/>
<property name="accountMoney" column="account_money"/>
<property name="accountStatus" column="account_status"/>
<property name="creationTime" column="creation_time"/>
</class>
</hibernate-mapping>
12.编写action
package com.action;
import com.entity.Account;
import com.opensymphony.xwork2.ActionSupport;
import com.service.AccountService;
import com.service.impl.AccountServiceImpl;
import org.apache.struts2.ServletActionContext;
public class AccountAction extends ActionSupport {
private Account account;
private Float reAccountMoney;
private AccountService accountService=new AccountServiceImpl();
public String login(){
Account a=accountService.getAccountByNumAndPwd(account);
if(a!=null){
account=a;
ServletActionContext.getRequest().getSession().setAttribute("user",a);
return SUCCESS;
}else{
return "loginout";
}
}
public String modifyAccountMoney(){
account=accountService.modifyAccountMoney(account,reAccountMoney);
return SUCCESS;
}
public String getOut(){
ServletActionContext.getRequest().getSession().invalidate();
return SUCCESS;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Float getReAccountMoney() {
return reAccountMoney;
}
public void setReAccountMoney(Float reAccountMoney) {
this.reAccountMoney = reAccountMoney;
}
}
13.编写页面就OK了
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p id="message"></p>
<p>您的卡号是:${account.accountNumber}</p>
<p>您的账户余额是:${account.accountMoney}</p>
<input type="hidden" value="${account.accountMoney}" id="accMon" name="account.accountMoney">
<c:if test="${account.accountStatus==0}">
<p>您的卡号状态是:可用</p>
<form method="post" action="modifyAccountMoney.action">
<input type="hidden" value="${account.id}" name="account.id">
<p>请输入取款金额:<input type="text" name="reAccountMoney" id="mon"></p>
<input type="submit" value="提交" id="sub">
</form>
</c:if>
<c:if test="${account.accountStatus==1}">
<p>您的卡号状态是:不可用</p>
</c:if>
<p><input type="button" value="返回" onclick="window.history.go(-1)"/></p>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("#mon").blur(function(){
var mon=$("#mon").val();
var accMon=$("#accMon").val();
if(accMon<mon){
$("#message").text("您的余额不足");
$("#mon").val("");
$("#sub").css("disable","disable");
}
});
})
</script>
</body>
</html>