J2EE SSH2 integration and J2EESSH2 Integration

Source: Internet
Author: User
Tags domian

J2EE SSH2 integration and J2EESSH2 Integration

In recent work, it is generally because the boss of the project team has set up the ssh2 framework for our little cainiao to use, so I have not independently built the framework for a long time, think about the small projects that I used to build myself step by step. To be honest, it was quite difficult to build an environment at that time, because I always forgot to import some jar packages, resulting in exceptions, or some jar Packages may cause conflicts, and the exception thrown is even more difficult. Although I am a newbie who has only one year of experience, I still want to do some work. I always feel that I have not solved a good problem before. Now I have to try it out again. So, if you have time to build the environment, you can sort out your previous knowledge.

1: import the jar package;

Struts2:

Spring:

Hibernate:

I don't need to explain what these jar packages are for. Everyone knows the basic jar packages of ssh2;

There are also some other jar packages: struts2-spring-plugin-2.1.8.1.jar (the jar package for struts2-spring consolidation), c3p0-0.9.2-pre1.jar (using the link pool to link the database)

2: add the struts. xml file

<? 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> <! -- Overwrite Convention --> <package name = "users" namespace = "/lishun" extends = "struts-default"> <! -- The action here is called spring management, so the class attribute value here is the id name of the injected Action, the action class will be mentioned later --> <action name = "users" class = "usersAction" method = "execute"> <result name = "success">/index. jsp </result> </action> </package> </struts>

Configure web. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" 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_3_0.xsd"> <display-name> </display-name> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> <! -- Define struts core listener --> <filter-name> action2 </filter-name> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> action2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <! -- Integrate struts with spring; Do not forget to export the struts2-spring-plugin-2.1.8.1.jar package --> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: application. xml </param-value> </context-param> </web-app>


Configure hibernate. cfg. xml

<! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd>


Configure application. xml

 

<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: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: context = "http://www.springframework.org/schema/context" xmlns: util = "http://www.springframework.org/schema/util" xsi: sche MaLocation = "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.spring Framework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <! -- Automatically scans com. all the beans under the lishun package are injected with values through annotations --> <context: component-scan base-package = "com. lishun "> </context: component-scan> <! -- Load the attribute file to read the database link string --> <context: property-placeholder location = "claspath: DataBaseConnection. properties "/> <bean name =" sessionFactory "class =" org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <! -- Manage hibernate to spring --> <property name = "configLocation" value = "classpath: hibernate. cfg. xml "> </property> <property name =" dataSource "> <bean class =" com. mchange. v2.c3p0. comboPooledDataSource "> <! -- The first four items are the four major strings of the database link, here we use the attribute file to read the database link string --> <property name = "jdbcUrl" value = "$ {jdbcUrl}"> </property> <property name = "user" value = "$ {user}"> </property> <property name = "password" value = "$ {password}"> </property> <property name = "driverClass" value = "$ {driverClass}"> </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 the sessionFactory transaction, based on Annotation --> <bean name = "txManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <tx: annotation-driven transaction-manager = "txManager"/> </beans>

DataBaseConnection. properties File

 

3: Test

This is my directory structure. It is called layer by layer.

Controller Action:

@ Controller ("usersAction") // Controller annotation, and defines that the id of the modified bean is usersAction, @ Scope ("prototype") // declares that the bean Scope is prototype: every time a bean is obtained from a container, it is a new object. Public class UsersAction extends ActionSupport {@ Resource private IUserService servise; public String execute () {servise. sava (); return "success ";}}

Business Layer seivice:

@ Service // service layer annotation public class UserServiceBean implements IUserService {// inject a value to the attribute @ Resource public IUserDao userDao; @ Override @ Transactional // This method automatically starts the transaction and submits the transaction. When there is an exception, it rolls back the transaction public void sava () {userDao. sava ();}}

Data operation layer dao:

Public class UserDaoBean implements IUserDao {// inject sessionfactory value from the configuration file @ Resource private SessionFactory seesionFactory; @ Override public void sava () {Session session = seesionFactory. getCurrentSession (); System. out. println (session); Users u = new Users (); u. setUname ("Wang Nima"); session. save (u );}}

There is a javabean entity at most. This entity has only two fields: id and name. Many fields are not used for testing.

public class Users {    private Integer uid;    private String uname;    public Integer getUid() {        return uid;    }    public void setUid(Integer uid) {        this.uid = uid;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    @Override    public String toString() {        return "Users [uid=" + uid + ", uname=" + uname + "]";    }}

Corresponding ing File

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 


Last access url: http: // localhost: 8080/Lishun_SSH2_01/lishun/users

The test is successful. The framework is set up.

 

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.