一、创建项目
1、创建Maven项目并导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
2、添加Web项目支持
由于Idea2023和其他版本不太一样,需要双击Shift输入 add framework support 双击添加Web应用版本支持
3、创建类继承HttpServlet
1、创建HelloServlet类继承HttpServlet,重写doget,dopost方法
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//调用doget方法
doGet(req,resp);
}
}
4、配置web.xml
5、Jsp文件编写
6、404错误:发布中没有包
二、SpringMVC运行原理
SpringMVC底层工作原理:
- DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求DispatcherServlet接收请求并拦截请求。
- 假设url为 : http://localhost:8080/SpringMVC/hello
拆分为:
(1)服务器域名:http://localhost:8080
(2)web站点:/SpringMVC
(3)hello表示控制器:/hello - 通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。
- HandlerMapping为处理器映射。DispatcherServlet自动调用HandlerMapping,HandlerMapping根据请求url查找Handler(处理器)。
三、深入理解运行原理
1、创建新的项目,保证含有springmvc依赖包
2、确保发布版本中也有依赖包
没有的话创建lib文件夹引入依赖
1、选择项目-》右键File-》项目结构Structure-》
3、配置DispatcherServlet(请求分发器)
DispatcherServlet是springmvc的核心,用户发出请求,DispatcherServlet接收请求并拦截请求;
1 配置web.xml:完成DispatcherServlet,关联resource配置文件
<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">
<!--配置DispatcherServlet:springmvc核心;请求分发器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个SpringMvc的resource配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:</param-value>
</init-param>
</servlet>
<!--映射-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、spring的配置文件(管理bean)
获得视图解析器、映射器、适配器,绑定跳转url
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--01处理器映射器HandlerMapping:查找访问的url控制器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--02处理器适配器HandlerAdapter:controller将处理好的数据返回给HandlerAdapter-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--03视图解析器ViewResolver:将后端处理好的数据和视图传给DispatchServlet,DS再交给ViewResolver先解析一遍,确认无误再传给前端
必须熟悉,以后还要学模版引擎Thymeleaf/Freemarker...
1 获取ModeAndView的数据
2 解析ModeAndView的视图名字
3 拼接视图名字,找到对应的视图 WEB-INF/jsp/hello.jsp
4id="internalResourceViewResolver"别的地方如果用的话可以引用id
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--配置视图文件的前后缀以便拼接-->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="/test" class="com.bipt.HelloController"></bean>
</beans>
5、HelloController类实现Controller接口
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv =new ModelAndView();
//2 调用业务层,这里没有,就不写
//3 封装对象,放在mv中添加
mv.addObject("msg", "Hello SpringMvc");
//4 封装要跳转的视图,WEB-INF/jsp/hello.jsp
mv.setViewName("test");
return mv;
}
}