This article introduces the use of Eclipse+maven to build the spring+springmvc+hibernate project, to log in as an example:
1. Create a MAVEN project
2. Change Maven project to dynamic website
3, Build Spring+springmvc+hibernate project 1 Create MAVEN projects Click to view: http://blog.csdn.net/tiandixuanwuliang/article/ Details/79250504#t0 2 The MAVEN project into a dynamic web site
Please click to view: http://blog.csdn.net/tiandixuanwuliang/article/details/79250504#t1
The project diagram is as follows:
3 start to build Spring+springmvc+hibernate project 3.1 Configure Maven dependency, write the following 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/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.welljoint</groupId> <artifactId>
Testssh2</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < ;spring.version>4.2.6.release</spring.version> </properties> <dependencies> <!-- Springframework 4 Dependencies Begin-<dependency> <groupid>org.springframework</ Groupid> <artifactId>spring-core</artifactId> <version>${spring.version}</ Version> </dependency> <dePendency> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-CONTEXT&L t;/artifactid> <version>${spring.version}</version> </dependency> <DEP Endency> <groupId>org.springframework</groupId> <artifactid>spring-web</ar tifactid> <version>${spring.version}</version> </dependency> <dependen Cy> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</arti factid> <version>${spring.version}</version> </dependency> <dependency > <groupId>org.springframework</groupId> <artifactid>spring-orm</artifacti D> <version>${spring.version}</version> </dependency> <!--springframe Work 4 dependencies End-<!--hibernate configuration Begin--<dependency> <groupid>org.hibernate</ Groupid> <artifactId>hibernate-core</artifactId> <version>3.6.9.final</ver
sion> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.9.final</version
> </dependency> <!--hibernate configuration End--<!--the drive pack for MySQL database start-to- <dependency> <groupId>mysql</groupId> <ARTIFACTID>MYSQL-CONNECTOR-JAVA&L T;/artifactid> <version>5.1.6</version> </dependency> <!--introduction of JSTL Package-- > <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifact Id>jstl-api</artifActid> <version>1.2</version> </dependency> <!--introducing Servlets-- <dependency> <groupId>javax.servlet</groupId> <ARTIFACTID>JAVAX.SERVL Et-api</artifactid> <version>3.1.0</version> </dependency> <!--introduction Database connection pool-<dependency> <groupId>commons-dbcp</groupId> <artifacti d>commons-dbcp</artifactid> <version>1.4</version> </dependency> &L T;dependency> <groupId>commons-pool</groupId> <artifactid>commons-pool</ar tifactid> <version>1.6</version> </dependency> </dependencies> </pro Ject>
3.2 Please check that maven dependencies are imported into the project.Please click to view: HTTP://BLOG.CSDN.NET/TIANDIXUANWULIANG/ARTICLE/DETAILS/79250504#T4
3.3 Create a new com.welljoint.entity package in Src/main/java; Create a new User.java bean in this bag.
Package com.welljoint.entity;
/** *
@version
* @time 2018-2-28 pm 6:02:03
* @describe: User's Bean */public
class User {
private Integer ID;
Private String userName;
private String password;
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
this.id = ID;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
this.username = userName;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
this.password = password;
}
@Override public
String toString () {
return "User [id=" + ID + ", username=" + UserName + ", password="
+ P Assword + "]";
}
}
3.4 Create a View folder in the Web-inf directory, and create a login.jsp in the folder with the following code:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Then create a login_success.jsp page under the View folder with the following code:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
3.5 Create a new com.welljoint.rest package in Src/main/java; Create a new Logincontroller.java in this package as a controller
Package com.welljoint.rest;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Com.welljoint.entity.User;
/** *
@version
* @time 2018-2-28 pm 5:57:07
* @describe: Controller
* /@Controller//Note for controller
@ Requestmapping (value= "/login") //Intercept request with/login public
class Logincontroller {
@RequestMapping ( method=requestmethod.get) Public
String GET () { //used to return a page
"login"; Returns a pointer to the login.jsp page
}
@RequestMapping (method=requestmethod.post) public
String POST (user user) { //To process the user's login request
return "login_success";
}
}
3.6 Create a new Spring-mvc.xml file in src/main/resources with the following code:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "H Ttp://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-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework . org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://w Ww.springframework.org/schema/mvc/spring-mvc-3.0.xsd > <!--scan all classes in a Web package to complete bean creation and automatic dependency injection-< ; Context:component-scan base-package= "Com.welljoint"/> <!--activating annotation-based configuration @RequestMapping, @ExceptionHandler, data binding , @NumberFormat, @DateTimeFormat, @ContRoller, @Valid, @RequestBody, @ResponseBody <mvc:annotation-driven/> <!--view Layer configuration--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--configuration jstl expression-- <property name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/> <!--prefix- -<property name= "prefix" value= "/web-inf/view/"/> <!--suffix---<property name = "suffix" value= ". jsp"/> </bean> <!--Configure static resources--<mvc:resources location= "/res/" Mappin g= "/res/**"/> </beans>
3.7 Configure Web. XML with the following code:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_
ID "version=" 3.0 "> <display-name>testSSH2</display-name> <!--requests are given to Dispatcherservlet processing <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframe Work.web.servlet.dispatcherservlet</servlet-class> <init-param> <PARAM-NAME>CONTEXTC Onfiglocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </
init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern&gT </servlet-mapping> <!--Add Chinese garbled filter--<filter> <filter-name>characterencodingfilt Er</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</ filter-class> </filter> <filter-mapping> <filter-name>characterencodingfilter</fil
Ter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--clear JSP spaces--
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <trim-directive-whitespaces>true</trim-directive-whitespaces> </jsp-property-group> </j Sp-config> </web-app>
If you follow the above step, you can already run the effect, the effect diagram is as follows:
Any input click Login, the following image:
3.8 Write Service layer: Create a new Com.welljoint.service package in Src/main/java; Create a new Loginservice.java interface in this package as a service layer
Package com.welljoint.service;
Public interface Loginservice {public
int login (String username,string password);
}
Then create a new Loginserviceimpl.java in the Com.welljoint.service package as a service layer interface implementation
Package com.welljoint.service;
Import Org.springframework.stereotype.Service;
@Service public
class Loginserviceimpl implements loginservice{