在记录这次控制器编写前,对于Spring的感觉就是经常提这样代码好简洁,这样好方便,这个是用来干嘛的诸如之类的话。
What is Spring ?这是我想问自己的,一直认为是简化代码利于工程的开源框架,还不够。里面包含的太多了,人称Spring全家桶可不是闹着玩的。。。。。。
毕竟入门,先来区分一下这三者:Spring MVC和Spring Boot都属于Spring,Spring MVC 是基于Spring的一个 MVC 框架,而Spring Boot 是基于Spring的一套快速开发整合包,详细点的话可以看简书上的这篇文章 https://www.jianshu.com/p/42620a0a2c33, 个人理解是Spring Boot 就是 Spring 的plus版本,那么SpringMVC呢?
- M 代表 模型(Model)
模型是什么呢?我想到了固定的这个词, 模型就是固定的数据,就是 dao,bean。这一层就是把相同的业务封装到一起,它的功能主要是做数据的持久化,就是跟数据库相关的操作。比如把老虎跟狮子的相关信息保存到数据库或从数据库取出或删除。 - V 代表 视图(View)
视图是什么呢? 就是网页, JSP,用来展示模型中的数据。用户可以通过可视化的页面把要请求的信息传递给后台然后返回想要的结果展示。 - C 代表 控制器(controller)
控制器是什么? 控制器的作用就是把不同的数据(Model),显示在不同的视图(View)上,Servlet 扮演的就是这样的角色。它就像是一个路由器,连接着视图页面和业务模块。根据不同的请求,它把用内户想要的信息从对应的业务模块获取然后反馈到不同的页面给用户或者是将请求转发给其他controller

1 package tacos.web; 2 3 import java.util.Arrays; 4 import java.util.List; 5 import java.util.stream.Collectors; 6 import javax.validation.Valid; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PostMapping; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.ResponseBody; 13 import org.springframework.web.bind.annotation.RestController; 14 import org.springframework.validation.Errors; 15 import lombok.extern.slf4j.Slf4j; 16 import tacos.Ingredient; 17 import tacos.Ingredient.Type; 18 import tacos.Taco; 19 @Slf4j 20 @Controller 21 @RequestMapping("/design") //类级别注解 22 public class DesignTacoController { 23 24 25 @GetMapping 26 public String showDesignForm(Model model) { 27 Listingredients= Arrays.asList( 28 new Ingredient("MEDIUM","Medium",Type.COMPONENT), 29 new Ingredient("SMALL","Small",Type.COMPONENT), 30 new Ingredient("BIG","Big",Type.COMPONENT), //份量 31 new Ingredient("MILD","Mild",Type.FLAVOR), 32 new Ingredient("HOT","Hot",Type.FLAVOR), 33 new Ingredient("MEDIUM","Medium",Type.FLAVOR), //口味 34 new Ingredient("BBALL","Beefball",Type.SIDEDISH), 35 new Ingredient("FANS","Fans",Type.SIDEDISH), 36 new Ingredient("POT","Potato",Type.SIDEDISH), 37 new Ingredient("SAU","Sausage",Type.SIDEDISH), 38 new Ingredient("COR","Coriander",Type.SIDEDISH), 39 new Ingredient("COLA","Cola",Type.DRINK), 40 new Ingredient("JUICE","Juice",Type.DRINK), 41 new Ingredient("WATER","Water",Type.DRINK) 42 ); 43 44 Type[] types = Ingredient.Type.values(); 45 for(Type type : types) { 46 model.addAttribute(type.toString().toLowerCase(), 47 filterByType(ingredients, type)); 48 } 49 model.addAttribute("design",new Taco()); 50 return "design"; 51 } 52 53 private List filterByType(List ingredients, Type type){ 54 return ingredients 55 .stream() 56 .filter(x -> x.getType().equals(type)) 57 .collect(Collectors.toList()); 58 //stream方法获取指向当前Collection对象的流对象,filter将对流中元素进行过滤 59 //结合lambda表达式,需要在filter参数中实现一个类似于比较器的Predicate对象,返回一个boolean类型返回值 60 //只有返回为true的Collection中的元素才会进入到list中。 61 } 62 63 @PostMapping 64 public String processDesign(Taco design) { 65 log.info("processing design:" +design); 66 return "redirect:/orders/current"; 67 } 68 69 }
这里首先想对几个注解做一下解释说明
@Sl4j:这是一个日志类库注解。
@Controller:这里想与@RestController用法做个对比,当你想要返回一个视图的时候应该用前者@Controller,而单纯的返回字符串应该用后者。有关JSON类型数据等自己在Spring中运用到再进行补充。
@RequestMapping(" "):这个开始我自动划到黑魔法,现在熟练度提升一点:当启动项目后,在浏览器地址栏访问8080时,所需要的路径,也就是后面应该跟的html文件名(也就是return 返回的视图名,这里特地注意:.html的文件名应该与前两者统一!!! 当初本人就是不细心花费了大半天时间才发现文件名手抖打错了)
其中的一些基础foreach,枚举泛型等在此就不过多阐述。还有倒数第二个方法可以留意一下,注释已经给出。
对于此项目这个控制器的讲解目前到这了,等用到后面,会对整个控制器来个大礼包。
现在都是逻辑处理,下一篇我们就可以验收一个小成果了(张伟别打我)