SpringMVC


新建项目:SpringmvcDemoLiuJie

导包:MySQL驱动包、框架基础包、添加MyBatis核心配置文件

添加web

右键SpringmvcDemoLiuJie项目名——Add Framework Support——Versions4.0——Create web.xml(自动创建web.xml)——ok

 配置SpringMVC

引入依赖

<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.13.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
dependencies>

刷新Maven自动下载

配置静态资源导出

<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>

Spring核心配置文件

main——resources——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
">



beans>

添加SpringMVC配置内容

1.加载注解驱动

注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例,

可以用annotation-driven配置自动完成注入


<mvc:annotation-driven/>

2.静态资源过滤


<mvc:default-servlet-handler/>

3.视图解析器——自动添加前后缀


<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>

controller包:HelloController

 配置Spring容器自动扫描包

applicationContext.xml


<context:component-scan base-package="controller"/>

编写jsp

WEB—INF包:

新建jsp包—hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


Title


${msg}

编写web.xml

1.配置前端控制器


<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>

2.配置初始化参数


<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>

3.设置启动级别


<load-on-startup>1load-on-startup>

4.设置SpringMVC拦截请求


<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>

5.配置中文乱码过滤器


<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>

 运行web项目

打包:file——Project Structure打开项目构建管理框

删除默认包

 添加WAR包

点+号——Web Application:Exploded——From Modules——依次点击ok

配置TomCat

点击 Add Configuration… 进入运行配置框

点击 Configure 选择我们自己的TomCat

点击 Deployment -> + 号 -> Artifact——自动加入导好的包

Application context 改成/是为了方便在浏览器输入路径

 运行Tomcat

 在浏览器输入 http://localhost:8080/hello