Spring_Hibernate_Struts2整合

1、jar包

  

2、項目結構

  

3、web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2Spring1</display-name>
  <welcome-file-list>
    <welcome-file>register.jsp</welcome-file>
  </welcome-file-list>
  <!-- 用來定位spring配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 監聽器 -->
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  <!-- struts配置 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class> org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置OpenSessionInViewFilter -->
  <filter>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>
4、applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="reg" class="com.lzm.action.RegAction" scope="prototype">
	<property name="userService" ref="userService"/>
</bean>
<bean id="userService" class="com.lzm.service.UserService">
	<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="com.lzm.impl.UserDaoImpl">
	<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<!-- 數據庫驅動 -->
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<!-- 數據庫url -->
	<property name="url" value="jdbc:mysql://localhost:3306/s2sh"/>
	<!-- 數據庫用戶名 -->
	<property name="username" value="root"/>
	<!-- 數據庫用戶密碼 -->
	<property name="password" value="910812"/>
	<!-- 數據庫連接池初始化大小 -->
	<property name="initialSize" value="5"/>
	<!-- 數據庫連接池最大連接數 -->
	<property name="maxActive" value="100"/>
	<!-- 數據庫連接池最大空閒時間 -->
	<property name="maxIdle" value="30"/>
	<!-- 數據庫連接池最大等待時間 -->
	<property name="maxWait" value="1000"/>
</bean>

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				org.hibernate.dialect.MySQLDialect
			</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.hbm2ddl.auto">update</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/lzm/entity/User.hbm.xml</value>
		</list>
	</property>
</bean>
<!-- 配置Hibernate的事務管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事務增強,指定事務管理器 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
	<!-- 配置詳細的事務定義 -->
	<tx:attributes>
		<!-- 所有find開頭的方法是read-only的 -->
		<tx:method name="find*" read-only="true"/>
		<!-- 其他方法使用默認的事務設置 -->
		<tx:method name="*"/>
	</tx:attributes>
</tx:advice>
<aop:config>
	<!-- 切入點 -->
	<aop:pointcut id="allMethods" expression="execution(*com.lzm.service.*.*(..))" />
	<!-- 指定在allMethods切入點應用txAdvice切面 -->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
</aop:config> 
</beans>


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>
	<package name="reg" extends="struts-default">
		<action name="reg" class="reg">
			<result name="success">/regsuccess.jsp</result>
			<result name="input">/register.jsp</result>
		</action>
	</package>
</struts>
	

7、RegAction.java

package com.lzm.action;

import java.util.ArrayList;
import java.util.List;

import com.lzm.entity.User;
import com.lzm.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class RegAction extends ActionSupport {
	
	private UserService userService;
	private List<User> list=new ArrayList<User>();
	
	private String name;
	private Integer age;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public List<User> getList() {
		return list;
	}
	public void setList(List<User> list) {
		this.list = list;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String execute(){
		User user=new User();
		user.setName(name);
		user.setAge(age);
		userService.add(user);
		
		return list();
	}
	
	public String list() {
		List<User> list=userService.findUsers();
		this.list.addAll(list);
		return SUCCESS;
	}
	
}


8、UserDao.java

package com.lzm.dao;

import java.util.List;

import com.lzm.entity.User;

public interface UserDao {

	public void add(User user);
	public List<User> findUsers();
}

9、UserDaoImpl.java

package com.lzm.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lzm.dao.UserDao;
import com.lzm.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public void add(User user) {
		getHibernateTemplate().save(user);
		
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<User> findUsers() {
		@SuppressWarnings("rawtypes")
		List<User> list=getHibernateTemplate().execute(
				new HibernateCallback(){
					public Object doInHibernate(Session session)throws HibernateException,SQLException{
						Query query=session.createQuery("from User");
						return query.list();
					}
				});
		return list;
	}

}

10、UserService.java

package com.lzm.service;

import java.util.List;

import com.lzm.dao.UserDao;
import com.lzm.entity.User;

public class UserService {

	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void add(User user){
		userDao.add(user);
	}
	
	public List<User> findUsers(){
		return userDao.findUsers();
	}
}

11、register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>register</title>
</head>
<body>
	<form action="reg.action" method="post" name="regForm">
		<table>
			<tr>
				<td>
					姓名:
				</td>
				<td>
					<input type="text" name="name" size="15"/>
				</td>
			</tr>
			<tr>
				<td>
					年齡:
				</td>
				<td>
					<input type="text" name="age" size="15"/>
				</td>
			</tr>
			<tr>
				<td>
					<input type="submit" value="註冊"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>







發佈了46 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章