DispatcherServlet。 Model-View-Controller(模型-视图-控制器)模式,它是一种软件架构模式。 早期的JavaWeb的项目应用。

MVC 模式将应用程序划分成模型(Model)、视图(View)、控制器(Controller)等三层。



需求:
创建 Maven project。


准备创建类。
DemoApplication.class@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
Phone.classpublic class Phone {private Integer id;private String brand;public Phone(Integer id, String brand, String colour, String county) {this.id = id;this.brand = brand;}public Phone() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}@Overridepublic String toString() {return "Phone{" +"id=" + id +", brand='" + brand + '\'' +'}';}
} PhoneController.class@RestController
@RequestMapping("/phone")
public class PhoneController {@RequestMapping("/info")public String info(){Phone phone = new Phone(13, "Max", "紫色", "USA");String s = phone.toString();return s;}@RequestMapping("/info2")public Phone info2() {Phone phone = new Phone();phone.setId(18);phone.setBrand("phone");return phone;}//TODO 测试: 可以返回基本数据类型到页面么? 如数组?@RequestMapping("/arr")public int[] arr() {int[] a = {1,2,3,4};return a;}
}
启动SpringBoot,访问地址, http://localhost:8080/phone/info,http://localhost:8080/phone/info2
Phone{id=13, brand='Max'},{"id":18,"brand":"phone"}。http://localhost:8080/phone/add?id=1&name=周杰伦&age=38。@PathVariable来获取请求路径中的参数值,@PathVariable用来绑定值http://localhost:8080/phone/add/1/周杰伦/44页面的名称和后台形参的名称要保持一致。
User.class 类public class User {private int id;private String name;private int age;private double price;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", price=" + price +'}';}
} CetController.class 类@RestController
@RequestMapping("/phone")
public class GetController {//1. 解析get获取的Url地址值,如: key=value 中的value@Test //使用单元测试public void get(){//http://localhost:8080/phone/insert?id=1&name=周杰伦&age=38String s = "http://localhost:8080/phone/add?id=1&name=周杰伦&age=38";String[] split = s.split("\\?")[1].split("&");for (String data:split) {//[id=1, name=周杰伦, age=38] ,然后按照等号切获取data[1],就是value值String value = data.split("=")[1];System.out.println(value); // 1, 周杰伦,38.}}/*2.如果使用框架处理问题。http://localhost:8080/phone/get2?id=1*/@RequestMapping("get2")public int get2(int a){ //页面访问值id 需要和 形式参数保持一致。return a;}//2.1 http://localhost:8080/phone/get3?name=周杰伦&age=44@RequestMapping("get3")public String get3(String name,int age){return "请求的参数name= :"+name+ " 年龄age= "+ age;}/*** 3. 假设有参数很多了该怎么办?* 框架能给我们做什么 ?* 这样如何解析: http://localhost:8080/phone/get4?id=1&name=周杰伦&age=44&price=2.2*/// public String get4(int id,String name,int age,double price){}@RequestMapping("get4")public User get4(User u){return u;}
}
Title
点我,得到你想要的
主要简化了get请求的提交,如果是restful 请求,需要使用 @RequestParam(“页面名称”), 获取路径中传递的值。
RunApp.class 该类为资源包里,或者其他类的上一级或者同级 。 不同包则无效。
@SpringBootApplication
public class RunApp {public static void main(String[] args) {SpringApplication.run(RunApp.class, args);}}
创建 Controller.class ,形式参数上与 通过{ }获取路径中传递来的值。
@RestController
@RequestMapping("user")
public class Controller {//restful 请求需要 在RequestMapping 中加入{}参数//并且需要在 形式参数上@PathVariable 绑定值@RequestMapping("info/{name}/{age}")public String info(@PathVariable String name,@PathVariable Integer age){return "返回的结果 name= "+name+" 年龄 age= "+age;}//2.restful解析// 封装对象,自动赋值访问。@RequestMapping("info2/{name}/{age}/{conutry}")public Person info2(Person person){return person;}
}
前端页面
Title
restful解析restful解析2

Students.html ,因为post需要以表单的形式进行提交。 
student