1.springmvc的作用:解耦合,简化web开发,对传递过来的参数进行封装(底层还是使用getParameter("xxx"))
2.特点:可以无缝与spring进行整合.
springmvc入门:
1.创建一个web工程
2.导入springmvc的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
3.配置一个servlet,作为springmvc的中央控制器
<!--配置一个servlet,作为springmvc的中央控制器-->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置加载springmvc配置文件的初始化参数
因为项目默认只会加载webapp下的文件,
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- 服务器启动时需要实例化这个servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!-- 配置url地址的拦截方式
1.后缀模式: *.form 拦截所有后缀为.form的请求
2.根路径模式: / 拦截所有请求
3.注意/ 和 /* 的区别:/拦截所有请求(动态和静态)
/*只能拦截动态资源请求
-->
<url-pattern>*.form</url-pattern>
</servlet-mapping>
从客户端发来的请求,根据我们配置的中央控制的拦截模式,拦截住请求,此时,后续的工作交给springmvc处理
1.找到后台的执行方法(通过组件handlerMapping)
2.调用此方法(通过组件handlerAdapter)
3.方法执行完毕后,返回视图页面(通过我们自己配置的视图解析器),根据方法的返回值拼接前缀和后缀,找到视图
4.配置springmvc的核心配置文件
头文件信息配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
</beans>
配置注解实例化对象扫描包
<!--配置扫描包-->
<context:component-scan base-package="com"></context:component-scan>
配置视图解析器
<!-- 配置互联网资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀-->
<property name="prefix" value="/document/"></property>
<!-- 配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
5.开发一个controller
package com.woniu;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping({"user"})
public class UserController {
@RequestMapping({"/hello.form"})
public String hello(Map<String, Object> map) {
map.put("message", "你好");
return "hello";
}
}
@RequestMapping("/list.form")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
ArrayList<User> users = new ArrayList<>();
users.add( new User("jack",12));
users.add( new User("bob",22));
users.add( new User("tom",42));
users.add( new User("rose",32));
mv.addObject("users",users);
mv.setViewName("list");
return mv;
}
tomcat9插件配置
<build>
<plugins>
<!--Tomcat9的插件-->
<plugin>
<groupId>org.opoo.maven</groupId>
<artifactId>tomcat9-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!--如果不配置path,则默认的上下文路径当前项目名-->
<!--<path>/</path>-->
<!--tomcat的端口-->
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>