作者:IQBB_LongGang | 来源:互联网 | 2023-10-09 20:26
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接https:space.bilibili.com95256449?spm_id_f
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接
https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2
使用注解开发
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">
?
springmvc
class>org.springframework.web.servlet.DispatcherServletclass>
contextConfigLocation
classpath:springmvc-servlet.xml
1
?
springmvc
/
?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
https://www.springframework.org/schema/aop/spring-aop.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">
?
package="com.rzp.controller"/>
default-servlet-handler/>
?
class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
hello
使用注解以后:
不再需要继承Controller接口,直接增加Controller,就相当于在spring中注册了。
因为原来的url输入是在springmvc-servlet.xml中配置的,现在我们不在xml中配置,而是增加RequestMapping注解,在这里写上要输入的url。
原来最终输出的页面地址我们封装到ModeAndView对象中,现在我们直接返回一个字符串就可以了,这个字符串就和原来的ModeAndView.setView方法一样,会被视图解析器拼接处理最终找到我们的页面。
package com.rzp.controller;
?
?
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
?
//加了Controller注解的类,这个类中所有的方法,如果返回值是String而且有具体的页面可以跳转,就会被视图解析器解析。
?
@Controller
// 如果在类上添加,则url要输入..../hello/h1,否则就直接写..../h1就可以了
//@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/h1")
public String hello(Model model){
//封装数据
model.addAttribute("msg","HelloSpingMVCAnnotation!");
return "hello"; //会被视图解析器处理
}
//多个页面的时候,可以直接添加一个方法就可以了
@RequestMapping("/h2")
public String hello2(Model model){
//封装数据
model.addAttribute("msg","HelloSpingMVCAnnotation2!");
return "hello2"; //会被视图解析器处理
}
?
<%@ page cOntentType="text/html;charset=UTF-8" language="java" %>
${msg}
?
使用注解开发以后,就彻底的比servlet优化,我们不再需要每个类都要在xml文件里面注册,开发更加简单了。而两个xml文件都是固定的。
与注册xml对比
RESTful风格
对于下面这个Controller,有输入参数a和b,要正确显示页面,我们的url中就需要通过?a=1&b=2
传入参数
@Controller
public class RestfulStyile {
//原来的方式要在URI最后增加?并且录入参数 http://localhost:8080/s04/add?a=1&b=2
@RequestMapping(value = "/add")
public String test0( int a, int b, Model model){
int res = a+b;
model.addAttribute("msg","结果为"+res);
?
return "test";
}
}
或者某些URI会暴露我们的动作(例如下面左边的URI),而RESTful风格就是避免这种做法,比如上面的?a=1&b=2
应该变成/1/2,或者下面的右边的URI。
GET /rest/api/getDogs --> GET /rest/api/dogs 获取所有小狗
GET /rest/api/addDogs --> POST /rest/api/dogs 添加一个小狗
@Controller
public class RestfulStyile {
//RestFul风格:http://localhost:8080/s04/add/a/b
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int res = a+b;
model.addAttribute("msg","结果为"+res);
?
return "test";
}
}
?
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
例如:
@Controller
public class RestfulStyile {
@GetMapping(value = "/add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int res = a+b;
model.addAttribute("msg","结果为"+res);
?
return "test";
}
}
?