SSH整合相关文件配置

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

<!-- 启用基于注解的bean管理和IOC(DI) -->
<context:component-scan base-package="com.kaishengit"/>

<!-- 读取classPath中的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minIdle" value="5"/>
<property name="maxActive" value="20"/>
</bean>

<!-- 配置基于注解的事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.kaishengit.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

</bean>

<!-- Hibernate 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

 

2:baseAction

package com.kaishengit.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{

private static final long serialVersionUID = 1L;

public Map<String,Object> getSession() {
return ActionContext.getContext().getSession();
}

public Map<String,Object> getApplication() {
return ActionContext.getContext().getApplication();
}

public HttpServletRequest getHttpServletRequest() {
return ServletActionContext.getRequest();
}

public HttpServletResponse getHttpServletResponse() {
return ServletActionContext.getResponse();
}

public HttpSession getHttpSession() {
return getHttpServletRequest().getSession();
}

public ServletContext getServletContext() {
return ServletActionContext.getServletContext();
}

}

 

3:baseDao

package com.kaishengit.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.inject.Inject;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

public class BaseDao<T,PK extends Serializable> {

@Inject
private SessionFactory sessionFactory;
private Class<?> clazz;

public BaseDao() {
ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
Type[] types = parameterizedType.getActualTypeArguments();
clazz = (Class<?>) types[0];
}

public Session getSession() {
return sessionFactory.getCurrentSession();
}

public void save(T t) {
getSession().saveOrUpdate(t);
}

@SuppressWarnings("unchecked")
public T get(PK id) {
return (T) getSession().get(clazz, id);
}

public void del(PK id) {
getSession().delete(get(id));
}

public void del(T t) {
getSession().delete(t);
}

@SuppressWarnings("unchecked")
public List<T> findAll() {
return getCriteria().list();
}

@SuppressWarnings("unchecked")
public T findByProperty(String propertyName,Object value) {
Criteria c = getCriteria();
c.add(Restrictions.eq(propertyName, value));
return (T) c.uniqueResult();
}



private Criteria getCriteria() {
return getSession().createCriteria(clazz);
}




}

 

4:jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mydb
jdbc.username=root
jdbc.password=root

 

5: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">

<!-- Spring提供的字符集过滤器 -->
<filter>
<filter-name>springEncoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- OpenSessionInView:将Hibernate中Session的关闭延迟到响应结束 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</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>

<!-- Spring监听器 用于初始化Spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>



<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

 

 

 

 























</beans>

 

posted @ 2013-10-18 21:38  doeapk  阅读(102)  评论(0)    收藏  举报