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 -->
<!-- 配置搜索組件的基礎(chǔ)包(將被注解的類的對象納入Spring IoC容器的關(guān)聯(lián)) -->
<context:component-scan base-package="com.kygo.dang"/>
<!-- 使用注解來配置需要Spring IoC容器托管的對象以及對象之間的依賴關(guān)系 -->
<context:annotation-config/>
<!-- 配置為基于AspectJ的切面自動生成代理 -->
<aop:aspectj-autoproxy />
<!-- 配置數(shù)據(jù)庫連接池(空間換時(shí)間的性能優(yōu)化) -->
<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提供的事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置使用注解來聲明事務(wù) -->
<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">
<!-- 通過配置上下文參數(shù)指定Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<!-- 配置Web項(xiàng)目的前端控制器(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>
<!-- 配置創(chuàng)建IoC容器的上下文監(jiān)聽器 -->
<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默認(rèn)首頁
<default-action-ref name="index" />
struts2路徑隨便輸入調(diào)到指定頁面
<action name="*">
<result type="redirect">index</result>
</action>
</package>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。