Spring+hibernate+struts2
配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!-- @Component / @Controller / @Service / @Repository -->
<!-- 配置搜索组件的基础包(将被注解的类的对象纳入Spring IoC容器的关联) -->
<context:component-scan base-package="com.kygo.dang"/>
<!-- 使用注解来配置需要Spring IoC容器托管的对象以及对象之间的依赖关系 -->
<context:annotation-config/>
<!-- 配置为基于AspectJ的切面自动生成代理 -->
<aop:aspectj-autoproxy />
<!-- 配置数据库连接池(空间换时间的性能优化) -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dang?useUnicode=true&characterEncocding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="initialSize" value="10" />
<property name="maxTotal" value="50" />
<property name="maxWaitMillis" value="15000" />
</bean>
<!-- 配置hibernate的会话工厂(持久层需要依赖的对象) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- @Entity -->
<property name="packagesToScan" value="com.kygo.dang.entity" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 配置Spring为Hibernate提供的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置使用注解来声明事务 -->
<tx:annotation-driven/>
</beans>
配置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>
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default" namespace="/" >
<action name="index" class="index">
<result>/WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="person" 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">
<!-- 通过配置上下文参数指定Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<!-- 配置Web项目的前端控制器(Filter或者Servlet) -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.action</welcome-file>
</welcome-file-list>
<!-- 配置创建IoC容器的上下文监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
struts2 action例子
@Controller("index")
@Scope("prototype")
public class IndexAction {
@Autowired
private BookService bookService;
private List<BookType> bookTypes;
public List<BookType> getBookTypes() {
return bookTypes;
}
public String execute() {
bookTypes = bookService.getAllBookType();
return "success";
}
}
struts2默认首页
<default-action-ref name="index" />
struts2路径随便输入调到指定页面
<action name="*">
<result type="redirect">index</result>
</action>
</package>