SpringMVC是春天的一个组件,所以我们在使用SpringMVC的时候也会使用春天到
使用环境
-
<李> JDK: 1.8 李>
<李> Tomcat: 9.0.3李>
<李>春:5.2.8李>
<李>编译器:IDEA2019
李>
<强> 1,导包强>
需要引入spring web和Spring-webmvc两个包,可以到maven仓库里面去下载或者使用maven依赖
<强> 2,ApplicationContext。xml配置(春天的核心配置文件)强>
-
<李> ApplicationContext。xml文件需要放在- inf下,并且需要把名字改为拦截的serlvet-name + servlet,比如我这边的拦截名字为mvc,所以我需要把配置文件名改为mvc-Servlet.xml李>
<李>如果不放在- inf下,需要在web . xml文件中进行路径配置(如下web . xml文件中的初始标签配置)李>
<李>注意命名空间的问题李>
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;豆类xmlns=" http://www.springframework.org/schema/beans " xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:上下文=" http://www.springframework.org/schema/context " xmlns: mvc=" http://www.springframework.org/schema/mvc " 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd”; & lt; !——开启春天注解驱动——比; & lt;上下文:component-scan基础包=" com.cjh "/比; & lt; !——开启mvc注解驱动——比; & lt; mvc: annotation-driven> & lt;/mvc: annotation-driven> & lt;/beans>
<强> 3,web . xml配置强>
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; web 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 "比; & lt; servlet>& lt; servlet-name> mvc & lt; servlet-class> org.springframework.web.servlet.DispatcherServlet & lt; init-param>& lt; param-name> contextConfigLocation & lt; !——说明春天核心配置文件的位置——比; & lt; param-value>类路径:ApplicationContext.xml & lt;/init-param>& lt;/servlet>& lt; servlet-mapping>& lt; servlet-name> mvc & lt; url-pattern> * .do & lt;/servlet-mapping> & lt;/web-app>
<强> 4,java的实现强>
控制器类
@ controller @RequestMapping (“userController.do”) 公开课用户控件{ 公共用户控件(){ System.out.println(“控制器创建了”); } @RequestMapping 公共空白测试(){ System.out.println(“控制器:测试方法执行了”); } }
index . jsp
% @ & lt;页面contentType=" text/html;utf - 8字符集=java“语言=%比; & lt; html> & lt; head> & lt;元charset=皍tf - 8”比; & lt;元name=笆哟啊蹦谌?翱矶?设备宽度,初始=1.0”比; & lt; title> cai金hong & lt; style> & lt;/style> & lt;/head> & lt; body> & lt; a href=" https://www.yisu.com/zixun/userController.do " rel=巴獠縩ofollow”在测试& lt;/a> & lt;/body> & lt;/html>
请求和响应流程:
-
<李>当点击测试超链接时,浏览器向服务器发送userController.do的资源请求李>
<李>服务器接收到之后,找到类上面带有@RequestMapping (userController.do)注解的对象李>
<李>找到了之后,查找方法上面带有@RequestMapping (“xxx”)注解的方法李>
如果只有一个方法,可以不用写名字,直接写RequestMapping
如果有多个方法,需要注明方法名
-
<李>找到了之后,执行方法,并将处理信息响应回给浏览器(该代码中没有返回值)
李>
本篇文章只讲了一下最基本的时候,下一篇文章会详细的说的SpringMVC请求和响应的处理! ! !
以上就是SpringMVC——配置与使用的示例的详细内容,更多关于SpringMVC——配置与使用的资料请关注其它相关文章!