Spring-struts2-hibernate maven Integration

Source: Internet
Author: User


MyEclipse the process of building a MAVEN project I skipped it.

Look at the project.


Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelversion><groupid>com.iss</groupid><artifactid>maven02</artifactid ><packaging>war</packaging><version>0.0.1-snapshot</version><name>maven02 Maven webapp</name><url>http://maven.apache.org</url><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding></properties><dependencies ><!--log4j Log--><dependency><groupid>log4j</groupid><artifactid>log4j</ artifactid><version>1.2.17</version></dependency><!--servlet API--><dependency ><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId>< Version>3.1-b09</version><scope>provided</scope></dependency><!--MySQL driver--><dependency><groupid>mysql </groupId><artifactId>mysql-connector-java</artifactId><version>5.1.24</version> </dependency><!--mchange c3p0 Data source--><dependency><groupid>com.mchange</groupid>< artifactid>c3p0</artifactid><version>0.9.5-pre2</version></dependency><!--JUnit- -><dependency><groupid>junit</groupid><artifactid>junit</artifactid>< version>4.11</version><scope>test</scope></dependency><!--Fastjson-->< dependency><groupid>com.alibaba</groupid><artifactid>fastjson</artifactid>< version>1.1.29</version></dependency><!--struts2--><dependency><groupid> org.apache.struts</groupid><artifactid>struts2-core</artifactid><version>2.3.14</ Version><exclUsions><exclusion><groupid>javassist</groupid><artifactid>javassist</artifactid ></exclusion></exclusions></dependency><dependency><groupid>org.apache.struts </groupid><artifactid>struts2-spring-plugin</artifactid><version>2.3.14</version ></dependency><dependency><groupId>org.apache.struts</groupId><artifactId> struts2-convention-plugin</artifactid><version>2.3.14</version></dependency><!-- Spring--><dependency><groupid>org.springframework</groupid><artifactid>spring-core </artifactId><version>3.2.2.RELEASE</version></dependency><dependency>< Groupid>org.springframework</groupid><artifactid>spring-context</artifactid><version >3.2.2.release</version></dependency><dependency><groupid>org.springframework</ Groupid><artifactid> spring-jdbc</artifactid><version>3.2.2.release</version></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId>< Version>3.2.2.release</version></dependency><dependency><groupid> Org.springframework</groupid><artifactid>spring-web</artifactid><version>3.2.2.release </version></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-expression</artifactid><version>3.2.2.release</version></dependency ><dependency><groupid>org.springframework</groupid><artifactid>spring-orm</ artifactid><version>3.2.2.release</version></dependency><!--Aspectjweaver-->< dependency><groupid>org.aspectj</groupid><artifactid>aspectjweaver</artifactid>< Version>1.7.2</version></dependency><!--Hibernate--><dependency><groupid>org.hibernate</groupid><artifactid> hibernate-core</artifactid><version>4.2.0.final</version></dependency></ dependencies><build><!--maven Plugin--><plugins><plugin><artifactid> maven-war-plugin</artifactid></plugin><plugin><artifactid>maven-compiler-plugin</ artifactid><configuration><source>1.6</source><target>1.6</target>< Encoding>utf-8</encoding></configuration></plugin><plugin><artifactid> maven-resources-plugin</artifactid><configuration><encoding>utf-8</encoding></ configuration></plugin><plugin><artifactid>maven-javadoc-plugin</artifactid>< configuration><encoding>utf-8</encoding></configuration></plugin><plugin>< artifactid>maven-surefire-plugin</artifactid><version>2.7.2</version><configuration><forkmode>once</forkmode><argline>- dfile.encoding=utf-8</argline></configuration></plugin></plugins></build></ Project>
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><context-param ><param-name>contextconfiglocation</param-name><param-value>/web-inf/classes/spring.xml,/ web-inf/classes/spring-hibernate.xml,</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><filter><filter-name>encodingFilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>UTF-8</param-value></init-param></filter> <filter-mapping><filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Hibernate session lost Solution--><filter><filter-name>opensessioninview</filter-name>< filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class></ filter><filter-mapping><filter-name>opensessioninview</filter-name><url-pattern>/* </url-pattern></filter-mapping><!--struts2 Configuration--><filter><filter-name>struts2</ filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</ filter-class></filter><filter-mapping><filter-name>struts2</filter-name>< Url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file> Index.jsp</welcome-file></welcome-file-list></web-app>

Database.properties

hibernate.dialect=org.hibernate.dialect.mysqldialectdriverclassname=com.mysql.jdbc.drivervalidationquery= SELECT 1url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8username=rootpassword= Roothibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=true

Spring-hibernate.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:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!--using c3p0 data source, MySQL database--><bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "><!--MySQL5-->< Property Name= "Driverclass" value= "${driverclassname}" ></property><property name= "JdbcUrl" value= "${ URL} "></property><property name=" user "value=" ${username} "></property><property name=" Password "value=" ${pasSword} "></property><property name=" maxpoolsize "value=" "></property><property name=" Minpoolsize "value=" 1 "></property><property name=" Initialpoolsize "value=" 1 "></property>< Property Name= "MaxIdleTime" value= "></property></bean><!--session factory--><bean Id=" Sessionfactory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/><property name=" hibernateproperties "><props><prop key=" Hibernate.hbm2ddl.auto ">${hibernate.hbm2ddl.auto}</prop><prop key=" Hibernate.dialect ">${ Hibernate.dialect}</prop><prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop><prop key= "Hibernate.format_sql" >${hibernate.format_sql}</prop></props></property><!--annotation Mode configuration --><property name= "Packagestoscan" ><list><value>com.iss.model</value></list> </property></bean><!--Configuring Transaction--><bean name= "Txmanager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name=" sessionfactory "ref=" Sessionfactory "></property></bean><tx:annotation-driven transaction-manager=" TxManager "/> </beans>

Spring.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:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "><!--enable annotations--><context:annotation-config/><context: Component-scanbase-package= "Com.iss.action,com.iss.dao.impl,com.iss.service" ></context:component-scan ><!--Introducing the Properties file Hibernate property configuration--><context:property-placeholder location= "Classpath:database.properties"/ ><!--automatically scan DAO and service Pack (auto injection)--><!--<context:component-scan base-package= "com.iss*"/>-->< /beans> 

Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts><!--give action to spring container management--><constant name=" Struts.objectfactory "value= "Spring"/><!--mode for the development mode, modify the XML configuration file will automatically load, project delivery time set to false, so as not to affect performance--><constant name= "Struts.devmode" value= "True"/><constant name= "Struts.configuration.xml.reload" value= "true"/><!--character Set encoding--><constant Name= "struts.i18n.encoding" value= "Utf-8"/><package name= "Defaultpackage" namespace= "/" extends= " Struts-default "><action name=" useraction "class=" com.iss.action.UserAction "method=" Saveuser "><result Name= "Success" >/success.jsp</result><result name= "error" >/error.jsp</result></action ></package></struts>

Contents of each package in src/main/java/

Com.iss. Action. Useraction

Package Com.iss.action;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Com.iss.model.user;import Com.iss.service.userservice;import Com.opensymphony.xwork2.ActionSupport, @Controller ("Useraction") public class Useraction extends Actionsupport { Private user User;public User GetUser () {return user;} public void SetUser (user user) {this.user = user;} Private userservice<user> userservice;////@Autowired//public void Setuserservice (userservice<user> UserService) {//This.userservice = userservice;//} @Autowiredprivate userservice userservice;public String saveuser () { System.out.println (UserService); userservice.save (user); return "Success";}}

Com.iss.dao.UserDao

Package Com.iss.dao;import Com.iss.model.user;public Interface userdao<t> {public User save (T o);}

Com.iss.dao.impl.UserDaoImpl

Package Com.iss.dao.impl;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.repository;import Com.iss.dao.userdao;import Com.iss.model.User; @Repository ("Userdao") public class Userdaoimpl implements userdao< user> {@Autowiredprivate sessionfactory sessionfactory;////public void Setsessionfactory (sessionfactory Sessionfactory) {//this.sessionfactory = sessionfactory;//}////public sessionfactory getsessionfactory () {//return SE ssionfactory;//} @Overridepublic User save (User O) {Session session = Sessionfactory.getcurrentsession (); Session.save ( o); return o;}}

Com.iss.model.User

Package Com.iss.model;import Java.io.serializable;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import Javax.persistence.Id;import javax.persistence.Table; @Entity @table (name = "User") public class user implements Serializable {private int id;private String name; @Id @generatedvalue (strategy = generationtype.auto) 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;}}

Com.iss.service.UserService

Package Com.iss.service;import Com.iss.model.user;public Interface UserService {Public user save (user user);

Com.iss.service.impl.UserServiceImpl

Package Com.iss.service.impl;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.iss.dao.userdao;import com.iss.model.user;import Com.iss.service.UserService; @Service ("UserService") @ Transactionalpublic class Userserviceimpl implements UserService {@Autowiredprivate userdao<user> userdao;@ Overridepublic user Save (user user) {//TODO auto-generated method Stubreturn userdao.save (user);}}

index,jsp

<body><form action= "Useraction" method= "POST" ><table width= "207" border= "0" align= "center" ><tr ><TD colspan= "2" align= "center" nowrap= "nowrap" > User registration </TD></TR><TR><TD width= "68" nowrap= "nowrap" > User name </TD><TD width= "127" nowrap= "nowrap" ><label> <inputname= "User.Name" Type= "text" id= "username" size= "/> </label></td></tr><tr><td colspan=" 2 "align=" Center "nowrap=" nowrap "><label><input type=" submit "value=" register "/> <input type=" reset "value=" refill "/ ></label></td></tr></table></form></body>

success.jsp

<body>${user.name}</body>

After you have finished writing, right-click on the project to select Run as MVN install,build success and import the project into the Tomcat server and enter the address test in the browser address bar!






Spring-struts2-hibernate maven Integration

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.