Spring Boot實現了自動配置,降低了項目搭建的複雜度。
Spring框架需要進行大量的配置,Spring Boot引入自動配置的概念,讓項目設置變得很容易。
Spring Boot本身並不提供Spring框架的核心特性以及擴展功能,只是用於快速、敏捷地開發新一代基於Spring框架的應用程序。
集成了大量常用的第三方庫配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot應用中這些第三方庫幾乎可以零配置的開箱即用(out-of-the-box),大部分的Spring Boot應用都只需要非常少量的配置代碼,開發者能夠更加專注於業務邏輯。
Spring MVC 是一個框架;Spring Boot 是一套快速開發整合包。
現在開始舉例:映射
index.html
@RestController
public class IndexController {
@GetMapping("/")
ModelAndView index() {
ModelAndView ret=new ModelAndView("index");
List<Kv> services=new ArrayList<>(2);
services.add(Kv.by("link","config").set("name","aa"));
services.add(Kv.by("link","control").set("name","bb"));
ret.addObject("services", services);
return ret;
}
}
RestController = responseBody + controller的組合,返回的數據是json格式。
當前文件名稱是:index.html
/config/其他文件
@RestController
@RequestMapping("/config/xx")
public class CfgSetBroadCastController {
@GetMapping
public ModelAndView index(){
ModelAndView ret=new ModelAndView("adapterCfg/xx");
Map<String, String> paramMap = getBroadCastParam();
ret.addObject("bcastParam", paramMap);
return ret;
}
@RequestMapping(value="/updatexx", method = RequestMethod.POST)
@ResponseBody
public Object updateBroadCast(HttpServletRequest request){
Map<String, Object> map = new HashMap<String,Object>();
map.put("data", aa);
return map;
}
1.當前java文件的映射地址爲 IP:port/config/xx
2.index()方法中,返回的信息爲bcastParam,信息返回頁面在adaterCfg/xx.html
3.updateBroadCast()方法的路勁爲 IP:port/cofing/xx/updatexx
4.updateBroadCast()方法中返回的數據 Map
參數設置
@Controller
@RequestMapping("/config")
public class CfgIndexController {
@Resource(name="adpaterCfgService")
private AdapterCfgService adpaterCfgService = new AdapterCfgService();
@GetMapping
public ModelAndView index() {
ModelAndView ret=new ModelAndView("adapterCfg/index");
List<Record> records = adpaterCfgService.getAllMenu();
ret.addObject("allMenu", records);
return ret;
}
@RequestMapping("/{name}")
public String equipStatus(@PathVariable(name = "name") String name){
return "adapterCfg/"+name;
}
@RequestMapping("/brAreaSelector")
public String brAreaSelector(){
return "adapterCfg/brAreaSelector";
}
}
1.此java文件的路勁爲 IP:port/config
2.service文件需要註解 @Resource(name="service的名稱")
3.index()文件所指頁面 IP:port/adapter/index.html,返回數據 allMenu
4.equipStatus()方法,@PathVariable(name = "name") String name --- 參數設置,返回內容是頁面地址 IP:port/config/adaptercfg/參數name
簡單的service文件
@Service("adpaterCfgService")
public class AdapterCfgService {
public List<Record> getAllMenu(){
List<Record> records = Db.find("select * from text");
return records;
}
}
1.service文件需要註解 @Service("名稱")