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("名称")