springmvc是Spring中的web模块,它需要导入spring的核心包和依赖包。
springmvc底层是servlet,按照学习servlet的方式去学习它
话不多说 直接上干货 创建web工程导包
第一步:导包
spring核心包和依赖包
commons-logging-1.1.1.jar
spring-beans-4.1.2.RELEASE.jar
spring-context-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
spring-expression-4.1.2.RELEASE.jar
spring测试包
spring-aop-4.1.2.RELEASE.jar
spring-test-4.1.2.RELEASE.jar
jsp,servlet包
jsp-api.jar
servlet-api.jar
sprinmvc包
spring-web-4.1.2.RELEASE.jar
spring-webmvc-4.1.2.RELEASE.jar
第二步:配置前端控制器
这个前端控制器是别人写好的,我们拿来用
而且前端控制器就是一个Servlet虽然不需要写,但是需要去配置
步骤:
1.按照配置Servlet的方式在web.xml中配置前端控制器
注意名字最好是去复制【webmvc包】
2.在classpath下复制一个Spring配置文件过来,改名为spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</beans>
3.一定要把里面的内容全部删除,否则启动就会报错
4.在前端控制器的<init-param>中加载此配置文件
配置内容:
<!-- 前端控制器 -统一接收请求 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建的时候要自动加载初始化参数 - 加载SpringMvc的配置文件 -->
<init-param>
<!-- 指定配置文件的路径 -->
<!-- 不要随便写,因为框架要根据这个名字获取value值,如果名字写错了,它就获取不了,可以在springwebmvc的jar包里面去拷贝全限定名,如下图: -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 将这个Servlet的实例化和初始化提前到到服务器启动的时候【默认是在第一次收到请求的时候】,有助于提高运行效率 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern><!-- /hello.do -->
</servlet-mapping>
第三步:编写控制器
编写业务代码
public class HelloController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//handleRequest - 处理请求
System.out.println("来了,老弟!!!");
return null;
}
}
步骤
写一个类去实现Controller接口
重写方法,内容先直接写一个输出
第四步:去spring-mvc。xml匹配请求
<bean id="/hello.do" class="cn.itsource.controller.HelloController"></bean>
第五步:跳转响应
控制器代码:
public class HelloController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//handleRequest - 处理请求
System.out.println("来了,老弟!!!");
//响应
ModelAndView mv = new ModelAndView();//这个对象用来保存数据和数据 ,方便传输
mv.addObject("name", "james"); //设置响应的数据name
mv.addObject("age", 20); //设置响应的数据age
mv.setViewName("/WEB-INF/hello.jsp");//设置响应视图的路径 WEB-INF下的资源不能直接访问,必须通过转发
return mv;
}
}
第六步:编写逻辑视图
在WEB-INF下新建hello.jsp,并编写内容
<body>
<h1>来了,老弟!!!</h1>
<h2>你的名字是:${name }</h2>
<h2>你的年龄是:${age }</h2>
</body>
最后:测试
localhost/hello.do
执行流程分析:
请求 – 交给前端控制器【DispatcherServlet】 – spring-mvc.xml进行匹配 – 映射到处理器处理请求
前端控制器匹配方式【】
后缀匹配(*.xx)
匹配的范围太小
/*
匹配所有请求
会将css,html,jsp都匹配上
一般用在过滤器
/
匹配所有请求
不会拦截.jsp
但是会拦截静态资源
css,html,images
为什么会拦截静态资源
tomcat中有一个默认的Servlet专门处理静态资源,它的匹配方式就是/
我们在我们的工程中也写了/,所以就将默认的servlet给替换了,tomcat中默认的Servlet就无效了
目前最流行的
操作步骤:
1. 将前端控制器的<url-patten>的匹配方式改成/
2.在web根路径下WebContent新建index.jsp,写上一句话
3.重新部署访问 -- jsp可以访问,图片【静态资源不能访问】
4.设置静态资源放行
加约束mvc - 建议复制
2.在spring-mvc.xml中加上配置:
<!-- 静态资源放行 = 静态资源可以不拦截 -->
<mvc:default-servlet-handler/>
启动tomcat默认的Servlet配置
5.重新部署 -- 访问 - 图片可以正常显示
配置注意:web.xml 前端控制器这个参数名称千万注意,必须是这个名字。
<!-- 不要随便写,因为框架要根据这个名字获取value值,如果名字写错了,它就获取不了 -->
<param-name>contextConfigLocation</param-name>