SSH (spring+struts2+hibernate) Integrated version of the detailed

Source: Internet
Author: User

Spring+struts2+hibernate Framework Integration steps:

Development tools: Idea+oracle Database

One: Introduction of dependent jar packages

<!--Oracle Driver--
<!--Https://mvnrepository.com/artifact/com.oracle/ojdbc14--
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<!--Hibernate core dependency--
<!--Https://mvnrepository.com/artifact/org.hibernate/hibernate-core--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>

<!--JTA The primary role is transaction--
<!--Https://mvnrepository.com/artifact/javax.transaction/jta--
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

<!--Https://mvnrepository.com/artifact/javax/javaee-api--
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

<!--spring configuration jar-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-context--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-TX--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<!--jar--> used by AOP
<!--Https://mvnrepository.com/artifact/org.aspectj/aspectjweaver--
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>

<!--Web Programming jar-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-web--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-WEBMVC--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>

<!--c3p0 Connection pool--
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

<!--Spring Integration hibernate-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-orm--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<!--struts2 Core Pack--
<!--Https://mvnrepository.com/artifact/org.apache.struts/struts2-core--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>

<!--xwork Core Pack--
<!--Https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core--
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4</version>
</dependency>

<!--struts to integrate spring jar--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>

Second: Build the whole project structure

Beans Layer
public class Studentssh {
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;
}
}

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<!--map File Start-
<!--table name--
<class name= "Studentssh" table= "Studentssh" >
<!--column Name
<id name= "id" column= "id" >
<!--primary key generation policy native: auto-Generate primary key fields--
<generator class= "native" ></generator>
</id>
<property name= "name" column= "name" ></property>
</class>


DAO layer
Public interface Istudentdao {
/**
* Add Students
* @param student
* @return
*/
public int Add (STUDENTSSH student);
}

public class Studentdaoimpl implements Istudentdao {
Injected into the sessionfactory plant
Private Sessionfactory sessionfactory;
Add to
public int Add (STUDENTSSH student) {
Serializable count = Sessionfactory.getcurrentsession (). Save (student);
Return (Integer) count;
}

Public Sessionfactory getsessionfactory () {
return sessionfactory;
}
public void Setsessionfactory (Sessionfactory sessionfactory) {
This.sessionfactory = sessionfactory;
}

}

Service Layer
Public interface Istudentservice {
/**
* Add Students
* @param student
* @return
*/
public int Add (STUDENTSSH student);
}

public class Studentserviceimpl implements Istudentservice {
Interfaces injected into the DAO layer
Private Istudentdao DAO;
Public Istudentdao Getdao () {
return DAO;
}
public void Setdao (Istudentdao dao) {
This.dao = DAO;
}

/**
* Add
* @param student
* @return
*/
@Transactional
public int Add (STUDENTSSH student) {
int result = Dao.add (student);
return result;
}
}

Action Layer
public class Studentaction extends Actionsupport {
Private Studentssh studentssh;
Private Istudentservice Studentservice;
/**
* NEW
* @return
*/
Public String Add () {
int count= Studentservice.add (STUDENTSSH);
if (count>0) {
return SUCCESS;
}else {
return LOGIN;
}

}

Public Studentssh Getstudentssh () {
return studentssh;
}
public void Setstudentssh (Studentssh studentssh) {
This.studentssh = Studentssh;
}

Public Istudentservice Getstudentservice () {
return studentservice;
}
public void Setstudentservice (Istudentservice studentservice) {
This.studentservice = Studentservice;
}
}

Three: Writing the configuration file
applicationcontext.xml File
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
">
<!--data Source-
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Driverclass" value= "${jdbc.driverclass}" ></property>
<property name= "Jdbcurl" value= "${jdbc.jdbcurl}" ></property>
<property name= "user" value= "${jdbc.user}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
</bean>

<!--identification jdbc.properties-->
<context:property-placeholder location= "Classpath:jdbc.properties" ></context:property-placeholder>

<!--sessionfactory Factory--
<bean id= "Factory" class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource" ></property>
<property name= "Hibernateproperties" >
<props>
< print SQL statements!--Console--
<prop key= "Hibernate.show_sql" >true</prop>
<!--formatting sql-->
<prop key= "Hibernate.format_sql" >true</prop>
<!--dialect---
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop>
<!--bound Thread--
<prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate5.springsessioncontext</prop>
</props>
</property>
<!--scan small configuration files--
<property name= "mappingdirectorylocations" value= "Classpath:cn/sjl/beans" ></property>
</bean>

<!--DAO layers--
<bean id= "Studentdao" class= "Cn.sjl.dao.StudentDAOImpl" >
<property name= "Sessionfactory" ref= "Factory" ></property>
</bean>

<!--service Layer--
<bean id= "Studentservice" class= "Cn.sjl.service.StudentServiceImpl" >
<property name= "DAO" ref= "Studentdao" ></property>
</bean>

<!--creating a struts layer struts by default is multiple--
<bean id= "studentaction" class= "Cn.sjl.action.StudentAction" scope= "prototype" >
<property name= "Studentservice" ref= "Studentservice" ></property>
</bean>

<!--transaction Manager--
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Factory" ></property>
</bean>

<!--Affairs--
<tx:annotation-driven transaction-manager= "TransactionManager" ></tx:annotation-driven>
</beans>

struts.xml File
 <?xml version= "1.0" encoding= "UTF-8"? 
<! DOCTYPE struts public
-//apache software foundation//dtd struts Configuration 2.3//en "
" Http://stru Ts.apache.org/dtds/struts-2.3.dtd "
<struts>
<constant name=" struts.objectfactory "value=" Spring "></CONSTANT>
<package name=" result "namespace="/"extends=" Struts-default ";
<actio N name= "Add" class= "Studentaction" method= "add";
<result name= "Success" >/jsp/index.jsp</result>
<result name= "Login" >/JSP/ADD.JSP</RESULT>
</action>
</package>
< ;/struts>

 jdbc.driverclass=oracle.jdbc.driver.oracledriver 
Jdbc.jdbcurl=jdbc:oracle:thin: @localhost: 1521:ORCL
Jdbc.user=root
Jdbc.password=root

IV: Web. xml file (this cannot be less)
 <context-param> 
<param-name>contextconfiglocation</param-name>
<param-value >classpath:applicationcontext.xml</param-value>
</context-param>
<filter>
< Filter-name>struts</filter-name>
<!--core Controller-->
<filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</ Url-pattern>
</filter-mapping>
<!--listener-->
<listener>
<listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>
</listener>

Five: JSP page
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>


<title> Add </title>

<body>
<s:debug></s:debug>
<form method= "POST" action= "/add" >
Name: <input type= "text" name= "Studentssh.name" >
<input type= "Submit" value= "Add" >
</form>
</body>


<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>


<title> Add Success </title>

<body>
</body>


VI: Deployment and operation


SSH (spring+struts2+hibernate) Integrated version of the detailed

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.