Struts2 + Spring + Hibernate environment, struts2hibernate

Source: Internet
Author: User

Struts2 + Spring + Hibernate environment, struts2hibernate

Struts-2.3.20

Spring-1, 4.1.4

Hibernate-4.3.8

Slf4j-1.7.10

 

1. Create a database in MySQL

mysql> create database myoa default character set utf8

 

2. Create a Web Project in MyEclipse

Right-click on the project-Properties and set the code to UTF-8.

 

3. Configure Struts2

  • Copy the jar package (according to the example, aopalliance. jar ).
  • Write Struts Filter in web. xml.
  • Copy struts. xml.
  • Configure struts. xml:
    <constant name="struts.devMode" value="true" /><constant name="struts.action.extension" value="action" /><constant name="struts.ui.theme" value="simple"/>

     

4. Configure Hibernate

  • Copy the jar package (including the jpa package, c3p0 package, mchange-commons package, and JDBC driver package ).
  • Copy hibernate. cfg. xml and log4j. properties (in hibernate \ project \ etc \).
  • Copy *. hbm. xml (search for hibernate \)
  • Configure hibernate. cfg. xml:
    <session-factory name="foo"><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql:///myoa</property><property name="connection.username">root</property><property name="connection.password">123456</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="show_sql">true</property><property name="hbm2ddl.auto">update</property><mapping resource="User.hbm.xml" /></session-factory>

     

5. Configure Spring

  • Copy the jar package (including the commons-logging package ).
  • Create applicationContext. xml (search for context: component-scan on The IoC container page of document 5. The IoC container ).
  • Configure applicationContext. xml:
    <context:component-scan base-package="com.yangleda.oa"/>

 

6. Integrate Spring and Struts2

  • Copy struts2-spring-plugin.jar.
  • Configure Spring Listener in web. xml:
    <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>

 

Test:

  • In/WEB-INF/create SSH. jsp, the content is a success prompt.
  • Create TestAction. java:
    @Controller@Scope("prototype")public class TestAction extends ActionSupport {private static final long serialVersionUID = 1L;@Overridepublic String execute() throws Exception {return SUCCESS;}}
  • Configure action in struts. xml:
    <action name="test" class="testAction"><result>/WEB-INF/SSH.jsp</result></action>
  • Access http: // localhost: 8080/MyOA/test. action.

7. Integrate Spring and Hibernate

  • Create jdbc. properties:
    jdbcUrl=jdbc:mysql:///myoadriverClass=com.mysql.jdbc.Driveruser=rootpassword=123456
  • Delete the four repeated attributes in hibernate. cfg. xml.
  • Configure applicationContext. xml:
    <! -- Import the external properties file --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean"> <! -- Specify the location of the hibernate configuration file --> <property name = "configLocation" value = "classpath: hibernate. cfg. xml"> </property> <! -- Configure the c3p0 database connection pool --> <property name = "dataSource"> <bean class = "com. mchange. v2.c3p0. ComboPooledDataSource"> <! -- Data connection information --> <property name = "jdbcUrl" value = "$ {jdbcUrl}"> </property> <property name = "driverClass" value = "$ {driverClass} "> </property> <property name =" user "value =" $ {user} "> </property> <property name =" password "value =" $ {password} "> </property> <! -- Other configurations --> <! -- Three connections are obtained during initialization. The value must be between minPoolSize and maxPoolSize. Default: 3 --> <property name = "initialPoolSize" value = "3"> </property> <! -- The minimum number of connections retained in the connection pool. Default: 3 --> <property name = "minPoolSize" value = "3"> </property> <! -- The maximum number of connections retained in the connection pool. Default: 15 --> <property name = "maxPoolSize" value = "5"> </property> <! -- The number of connections that c3p0 obtains at the same time when connections in the connection pool are exhausted. Default: 3 --> <property name = "acquireIncrement" value = "3"> </property> <! -- Control the number of PreparedStatements loaded in the data source. If both maxStatements and maxStatementsPerConnection are 0, the cache is disabled. Default: 0 --> <property name = "maxStatements" value = "8"> </property> <! -- MaxStatementsPerConnection defines the maximum number of statements cached for a single connection in the connection pool. Default: 0 --> <property name = "maxStatementsPerConnection" value = "5"> </property> <! -- Maximum idle time. connections are discarded if they are not used within 1800 seconds. If it is 0, it will never be discarded. Default: 0 --> <property name = "maxIdleTime" value = "1800"> </property> </bean> <! -- Configure declarative Transaction Management (using annotation) --> <bean id = "txManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <tx: annotation-driven transaction-manager =" txManager "/>

The above <beans> to add the "xmlns: tx =" http://www.springframework.org/schema/tx ", and in" xsi: schemaLocation "specify the schema address" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ".

 

Test:

  • Create a domain sub-package where User. java and User. hbm. xml.
  • Compile User. java:
    public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
  • Modify User. hbm. xml:
    <class name="User" table="test_user"><id name="id"><generator class="native" /></id></class>
  • Create TestService. java:
    @Servicepublic class TestService {@Resourceprivate SessionFactory sessionFactory;@Transactionalpublic void saveTwo() {Session session = sessionFactory.getCurrentSession();session.save(new User());//    int a = 1 / 0;session.save(new User());}}
  • Modify TestAction. java:
    @Resourceprivate TestService testService;@Overridepublic String execute() throws Exception {testService.saveTwo();return SUCCESS;}
  • Access http: // localhost: 8080/MyOA/test. action.

 

8. Sort resource folders
Source Folder: src, config, test.
Folder: Web Root/script, Web Root/style, Web Root/WEB-INF/jsp.

 

9. Configure slf4j

  • Copy slf4j-api.jar and slf4j-log4j.jar
  • Modify log4j. properties:
    log4j.rootLogger=warn, stdoutlog4j.logger.com.yangleda.oa=debug

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.