Spring+springmvc+hibernate Environment Construction and configuration

Source: Internet
Author: User

The construction and configuration of the Javaweb Project Spring+springmvc+hibernate framework environment is given, which makes it easy to create the project quickly. The following is an example of my project for the configuration, different project specific structure can be done by the structure, as for the configuration of the content will need to follow the specific requirements of the project to change part of the content. It is important to note that this project is a project that is completed on the premise of using annotations.

A Project directory

First of all, this project is a MAVEN project, using the Spring+springmvc+hibernate framework, the front-end template engine with thymeleaf,html code in the structure of the Templates folder.

The overall project structure is as follows:

In Src/main/java put each layer of code, Hibernate.cfg.xml also exist under this path, and spring, SPRINGMVC configuration file is stored under/web-inf/configs/spring, Of course, these files can also be placed in other directories, only need to configure the path in Web. XML, see the configuration of Web. Xml.

Two Configuration of the Pom.xml

Each dependent content is written in a note, which relies on or does not need to be relied on simply adding or removing the dependency in 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>SYSU</groupId><artifactId>MovieHub</artifactId> <packaging>war</packaging><version>0.0.1-snapshot</version><name>moviehub Maven webapp</name><url>http://maven.apache.org</url><!--Define version variables that depend on the library--><properties>< commons-lang.version>2.6</commons-lang.version><!--Spring version number--><spring.version>4.3.3. release</spring.version>

Three Configuration of Web. xml

The Web. XML configures the path of the spring, SPRINGMVC configuration file, and can also configure various interceptor listeners, etc. if the project requires it.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app 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 "><display-name>MovieHub</display-name><!-- Spring application context, understanding hierarchical ApplicationContext--><context-param><param-name>contextconfiglocation</ Param-name><param-value>/web-inf/configs/spring/beans.xml</param-value></context-param> <listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class ></listener><!--Dispatcherservlet, the core of Spring MVC--><servlet><servlet-name> mvc-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class><!--dispatcherservlet Corresponding context configuration, default to/web-inf/$servlet-name$-servlet.xml--><init-param ><param-name>contextconfiglocation</param-name><param-value>/web-inf/configs/spring/mvc-dispatcher-servlet.xml</ param-value></init-param><load-on-startup>1</load-on-startup></servlet>< servlet-mapping><servlet-name>mvc-dispatcher</servlet-name><!--Mvc-dispatcher Intercept all requests-- <url-pattern>/</url-pattern></servlet-mapping></web-app>

Four Spring's configuration

Because of the annotations, spring's configuration beans.xml is very simple and only needs to be configured with the scan annotations, and the rest is done by annotations. So, using annotations is simple!

<?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" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" 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/conte Xt/spring-context-3.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sp Ring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP -3.2.xsd "default-autowire=" byname "> <context:component-scan base-package=" com "></context:component -scan> </beans>

Five Configuration of the Springmvc

SPRINGMVC configuration is also required to configure the note driver, while the static resource mapping, template engine configuration is also configured in the Mvc-dispatcher-servlet.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" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/co ntext http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schem A/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "><!-- This profile is used by Dispatcherservlet, which is named Mvc-dispatcher, to provide its associated spring MVC configuration--><!--enable the spring annotation-based di so that the user can The powerful features of spring are used in MVC. Activate @Required @Autowired, JSR s @PostConstruct, @PreDestroy and @Resource, and other annotations--><context:annotation-config/ > <!--enable MVC annotations--<mvc:annotation-driven/><!--DISPATCHERSERVLET context, which only manages beans of the @controller type, ignoring other types of beans, such as @service--><context:component-scan base-package= "com" >< Context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/></context The:component-scan><!--expands the annotation driver to bind the request parameters to the controller parameters--><mvc:annotation-driven/><!--static resource mappings-->< Mvc:resources mapping= "/rs-plugin/**" location= "/static/rs-plugin/"/><mvc:resources mapping= "/images/**" location= "/static/images/"/><mvc:resources mapping= "/css/**" location= "/static/css/"/><mvc:resources mapping= "/js/**" location= "/static/js/"/><mvc:resources mapping= "/fonts/**" location= "/static/fonts/"/> <mvc:resources mapping= "/favicon.ico" location= "/static/favicon.ico"/><!--template engine--><bean id= " Templateresolver "class=" Org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver "><property Name= "prefix" value= "/web-inf/templates/"/><property name= "suffix" value= ". html"/><!--HTML is the default value, added here for the sake of clarity. --><property name= "Templatemode" value= "HTML"/><!--Template cache is true by default.                    Set to False if the want--><!--templates to is automatically updated when modified. --><!--Cache--><!--<property name= "cacheable" value= "true"/>--><property name= " Characterencoding "value=" UTF-8 "/></bean><!--Springtemplateengine automatically applies         Springstandarddialect and--><!--enables Spring ' s own Messagesource message resolution mechanisms. --><bean id= "Templateengine" class= "Org.thymeleaf.spring4.SpringTemplateEngine" ><property name= " Templateresolver "ref=" Templateresolver "/><property name=" Enablespringelcompiler "value=" true "/></ Bean><bean id= "Viewresolver" class= "Org.thymeleaf.spring4.view.ThymeleafViewResolver" ><property name= "Templateengine" ref= "Templateengine"/><propertY name= "characterencoding" value= "UTF-8"/></bean></beans> 

Six Hibernate configuration

Because of the use of annotations, hibernate configuration is also very simple, only need to configure a variety of properties, including database user name password, url, dialect, etc., as well as mapped entity classes, and so on, the other by the annotations to be done.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">

Spring+springmvc+hibernate Environment Construction and configuration

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.