文章插图
文章插图
初识SpringMVC
实现步骤:
新建一个web项目导入相关jar包编写web.xml,注册DispatcherServlet编写springmvc配置文件接下来就是去创建对应的控制类 , controller最后完善前端视图和controller之间的对应测试运行调试使用springMVC必须配置的三大件:
处理器映射器、处理器适配器、视图解析器
通常,我们只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可,而省去了大段的xml配置
注解实现SpringMVC常见注解
@[email protected]@[email protected]
控制器package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;[email protected]@[email protected]("/test2")public class [email protected]("/t2")public String index(Model model){//Spring MVC会自动实例化一个Model对象用于向视图中传值model.addAttribute("msg", "ControllerTest2");//返回视图位置return "test";}}
@Controller是为了让Spring IOC容器初始化时自动扫描到;@RequestMapping是为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/test2/t2;标准maven依赖<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.reliable</groupId><artifactId>SpringMVC2</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>springmvc-04-controller</module></modules><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build></project>
一、配置pom.xml<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringMVC2</artifactId><groupId>com.reliable</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springmvc-04-controller</artifactId><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build></project>
二、配置web.xml<?xml version="1.0" encoding="UTF-8"?><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"><!--1.注册servlet--><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--通过初始化参数指定SpringMVC配置文件的位置,进行关联--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc-servlet.xml</param-value></init-param><!-- 启动顺序,数字越小,启动越早 --><load-on-startup>1</load-on-startup></servlet><!--所有请求都会被springmvc拦截 --><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
三、配置springmvc-servlet.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 --><context:component-scan base-package="com.kuang.controller"/><!-- 让Spring MVC不处理静态资源:html 等--><mvc:default-servlet-handler/><!--支持mvc注解驱动[email protected]?[email protected]必须向上下文中注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapter实例这两个实例分别在类级别和方法级别处理 。而annotation-driven配置帮助我们自动完成上述两个实例的注入 。<mvc:annotation-driven /> 完成了映射和适配(支持用注解完成)--><mvc:annotation-driven /><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="http://www.mnbkw.com/WEB-INF/jsp/" /><!-- 后缀 --><property name="suffix" value="http://www.mnbkw.com/jxjc/167851/.jsp" /></bean></beans>
RestFul 风格概念Restful就是一个资源定位及资源操作的风格 。不是标准也不是协议,只是一种风格 。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制 。
功能资源:互联网所有的事物都可以被抽象为资源资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作 。分别对应 添加、 删除、修改、查询 。
RestFulController(@PathVariable)
package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class RestFulController [email protected]("/commit/{p1}/{p2}")public String index(@PathVariable int p1, @PathVariable int p2, Model model){int result = p1+p2;//Spring MVC会自动实例化一个Model对象用于向视图中传值model.addAttribute("msg", "结果:"+result);//返回视图位置return "test";}//映射访问路径,[email protected](value = "http://www.mnbkw.com/hello",method = {RequestMethod.GET})public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}}
使用method属性指定请求类型用于约束请求的类型,可以收窄请求范围 。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等 。
//映射访问路径,[email protected](value = "http://www.mnbkw.com/hello",method = {RequestMethod.GET})public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}
除了添加method,还可以使用注解@[email protected]@[email protected]@PatchMapping
【springmvc依赖注入原理 springmvc依赖注入注解】
//映射访问路径,[email protected](value = "http://www.mnbkw.com/hello")public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}
- 减肥不能依赖药物
- 『依赖或组无法启动win10』window10怎么打开光盘
- 酵素有依赖性吗
- 梦幻情侣名字大全
- 简单四招 强健臂膀
- 男人依赖一个人的表现:如何让一个男人更爱你
- 女生说依赖我怎么说
- 女生依赖一个人的表现:女生依赖一个男生的表现
- 三种奇特减肥法 摆脱对减肥药的依赖
- 闲言命理:别只依赖八字命运