MAVEN builds springmvc+spring+hibernate environment

Source: Internet
Author: User

This time no longer use STRUTS2 as a controller, using spring's own SPRINGMVC framework.

First of all, overwrite the Pom.xml file, do not need struts2 related jar.

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.sgl</groupId> <artifactId>MSSH</artifactId> &LT;PACKAGING&G    t;war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mssh Maven webapp</name> <url>http://maven.apache.org</url>    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </prope Rties> <dependencies> <!--log4j--<dependency> <groupid>log4j&lt        ;/groupid> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactid& Gt;junit</artifactid> <version>4.11</version> </dependency> <!--SERVL ET API-<dependency> <groupId>javax.servlet</groupId> <artifactid& Gt;javax.servlet-api</artifactid> <version>3.1-b09</version> <scope>provided </scope> </dependency> <!--mysql driver--<dependency> &LT;GROUPID&G T;mysql</groupid>           <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> </dependency> <!--mchange c3p0 data Source--<dependency> <groupid>com.mcha Nge</groupid> <artifactId>c3p0</artifactId> <version>0.9.5-pre2</version&        Gt            </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.2.RELEASE</version> </ dependency> <dependency> <groupId>org.springframework</groupId> <arti Factid>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</artifacti            d> <version>3.2.2.RELEASE</version> </dependency> <dependency>            <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupi D>org.springframework</groupid> <artifactId>spring-expression</artifactId> < version>3.2.2.release</version> </dependency> <dependency> <groupid>or G.springframework</groupid> <artifactId>spring-orm</artifactId> <version>3.2 .2.RELEASE&LT;/VERSION&GT </dependency> <dependency> <groupId>org.springframework</groupId> &lt ;artifactid>spring-webmvc</artifactid> <version>3.2.2.RELEASE</version> </depend ency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifacti        D>jackson-mapper-asl</artifactid> <version>1.9.12</version> </dependency>            <!--Hibernate--<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.0.Final</version> </depe Ndency> </dependencies> <build> <!--maven Plugins--<plugins> <p lugin> <artifactId>maven-war-plugin</artifactId> </plugin> <p     Lugin>           <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> &LT;ENCODING&G                t;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> <ver Sion>2.7.2</version> <configuration> <forkMode>once</forkMode> &lt        ;argline>-dfile.encoding=utf-8</argline> </configuration> </plugin> </plugins> </build></project>

Delete Struts.xml, create a new spring-mvc.xml.

Spring-mvc.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"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.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><!--automatically scans all classes under the controller package so that it thinks spring MVC controller--<context:component-scan base- Package= "Com.sgl.controller"/> <!--avoid IE when performing Ajax, return JSON appears download file--<bean id= "Mappingjacksonhttpmessageconverter"class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > <property name= "            Supportedmediatypes "> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!--start the annotation feature of spring MVC, complete the mapping of requests and annotations Pojo-<beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name= " Messageconverters "> <list> <ref bean=" Mappingjacksonhttpmessageconverter "/><! ---JSON converter--</list> </property> </bean> <!--resolution of the Model view name, that is, adding a prefix to the Model view name- <beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/"/&gt        ; <property name= "suffix" value= ". jsp"/> </bean></beans>

Delete the action package, create a new controller package, build Usercontroller.java

Usercontroller.java

 PackageCom.sgl.controller;Importjava.util.Date;ImportJava.util.UUID;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.sgl.model.User;ImportCom.sgl.service.UserService; @Controller Public classUsercontroller {@AutowiredPrivateUserService UserService; @RequestMapping ("/reg")     PublicString Register () {return"Index"; } @RequestMapping ("/USER")     Publicmodelandview addUser (user user) {Modelandview Mav=NULL;        User.setid (Uuid.randomuuid (). toString ()); User.setregtime (NewDate ()); Try{userservice.adduser (user); //request.setattribute ("user", user);Mav =NewModelandview (); Mav.setviewname ("Success"); Mav.addobject ("User", user); Mav.addobject ("MSG", "registered successfully, can go to the landing"); returnMav; } Catch(Exception e) {mav.setviewname ("Error"); Mav.addobject ("User",NULL); Mav.addobject ("MSG", "Registration failed"); returnMav; }    }}

Finally, modify the Web. xml file, delete the configuration of struts2, and increase the SPRINGMVC configuration.

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</para M-name> <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value> </conte Xt-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>en Codingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--SPRINGMVC configuration-<servlet> &LT;DESCRIPTION&G t;spring MVC servlet</description> <servlet-name>springMvc</servlet-name> <servlet-clas S>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <desc            ription>spring MVC configuration file </description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startu P>1</load-on-startup> </servlet> <!--block requests with HTML suffix--<servlet-mapping> <s Ervlet-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping > <session-config> <session-timeout>10</session-timeout> </session-config> <   Welcome-file-list>     <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app> 

After all the configuration files are completed, the project structure is as follows:



Perform maven install, no errors, deploy to server, start server, browser input http://localhost:8080/MSSH/reg.html Enter

As for why to enter this address, please carefully review the configuration of the Usercontroller.java @requestmapping annotation, which is a spingmvc annotation, which no longer details its interception-forwarding process.


Click Register, note the change of the browser address:



Springmvc+spring+hibernate implementation of user registration is complete. One of the user's password I did not write, forget to persist to the database, the need can be added in the User.java password attribute and Set/get method. After all, this MSSH project simply demonstrates how to build an SSH framework, not a real project.

MAVEN builds springmvc+spring+hibernate environment

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.