IDEA Maven+SpringMVC+Mybatis HTML+Ajax MySql Tomcat登录Demo(一)
- 开发工具:IDEA
- 前端技术:HTML+JQuery+Ajax
- 后端技术:Maven+SpringMVC+MyBatis
- 数据库:MySql
本节内容记录了创建项目、配置SpringMVC、配置了Tomcat启动、创建了SpringMVC控制器,算是后端项目的搭建。
一、创建项目
打开 IDEA—>NewProject
选择Maven–>next
选择填写对应组织–>对应报名–>next
确认项目名和moudle名–>选择路径
二、配置Web
这里已经创建好了Maven项目,下一步对刚创建的Module配置Web,点击当前的模块,点击File,选择Project Structure,
修改DEployment的Path:[路径\SpringMVC_MyBatis_Login\src\main\webapp\WEB-INF\web.xml]
配置Web资源路径,在webapp目录。
三、配置Tomcat启动项目
配置Tomcat:
启动之后就可以看到我们的index.html了。
四、配置SpringMVC
步骤:
1.添加依赖spring-webmvc
2.web.xml配置DispatcherServlet
3.配置applicationContext.xml文件
4.开发控制器
1.添加依赖spring-webmvc
<?xml version="1.0" encoding="UTF-8"?>
<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.bruce</groupId>
<artifactId>chen</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引用阿里镜像-->
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
2.web.xml配置DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<!--
用于拦截http请求,并根据请求url调用Controller
-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数 applicationContext.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--需在resource下创建applicationContext.xml文件-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--启动时自动创建Spring IOC容器
并初始化DispatcherServlet-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/代表拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3:在resource创建applicationContext.xml文件
同时配置src的包名,我创建了com.bruce.chen
applicationContext.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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bruce.chen"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven >
<!--解决中文返回乱码-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--<!– Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory –>-->
<!--<mvc:resources mapping="/css/**" location="WEB-INF/css/" />-->
<!--<mvc:resources mapping="/js/**" location="WEB-INF/js/" />-->
<!--<mvc:resources mapping="/html/**" location="WEB-INF/html/" />-->
<!--<mvc:resources mapping="/xml/**" location="WEB-INF/html/" />-->
</beans>
4.开发控制器
创建控制类
package com.bruce.chen.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class LoginControl {
/**
* 设置 请求方式 POST对应PostMapping;GET对应GetMapping
* @return
*/
@ResponseBody
@PostMapping(value = "/loginForm")
public String loginformBackString(){
return "登录成功";
}
}
为了能演示目前阶段,需要把前端页面和逻辑写出来。写在下一节。