1、优点
2、model和modelandview
后端从控制层直接返回前端需要的数据
ModelAndView对象有两个作用:
(1) 设置转向地址,这也是ModelAndView和ModelMap的主要区别。
(2)将控制器方法中处理的结果数据传递到结果页面,也就是把在结果页面上需要的数据放到ModelAndView对象中即可,其作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。
3、get和post
1、@Controller @requestMapping
使用@Controller 标记一个类是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到。控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的方法参数灵活的获取到。
2、@resource
在controller中在需要注入的service上加上@Resource。Controller层注入的是Service接口,而不是ServiceImpl实现类
3、@ResponseBody
将java对象转为json格式的数据。将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据,需要注意的呢,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
4、@RequestParam
@RequestParam是传递参数的。
用于将请求参数区域的数据映射到控制层方法的参数上。
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:参数的默认值,如果请求中没有同名的参数时,该变量默认为此值。注意默认值可以使用SpEL表达式,如"#{systemProperties[‘java.vm.version’]}"
5、@Param
为SQL语句中参数赋值而服务的。
使用 resultType : 主要针对于从数据库中提取相应的数据出来。
用parameterType : 主要针对于 将信息存入到数据库中。
//跳转添加社区页面
@RequestMapping("/community/add")
public String communityAdd() {
return "admin/community/add";
}
//保存社区信息到数据库
@RequestMapping(value = "/community/save", method = RequestMethod.POST)
public String communitySave(String communityname, Model model) {
Community modelX = new Community();
modelX.setCommunityname(communityname);
modelX.setCreated(MainUtils.getTime());
//modelX.setStatus(1 + "");
communityService.insert(modelX);
model.addAttribute("message", "添加成功!");
return "admin/community/add";
}
四、删
//删除社区,并返回成功信息,Post
@RequestMapping("/community/delete1")
//@ReponseBody为后端向前端返回json数据
@ResponseBody
public String communityDelete(@RequestParam(value = "id", required = true) int id) {
communityService.delete(id);
return "success";//返回添加成功的信息
}
五、查(实现分页)
select * from community limit #{page},#{limit}
//get返回列表视图
@RequestMapping("/community/list")
public String communityList() {
return "admin/community/list";
}
//post返回所有的列表
@RequestMapping("/community/listData")
@ResponseBody
public String listData(@RequestParam(value = "page",required = false,defaultValue = "1")int page,
@RequestParam(value = "limit",required = false,defaultValue = "5")int limit) {
List communityList = communityService.getAllCommunity();
int count=communityList.size();
Map map=new HashMap();
map.put("code", 0);//修改成功,登录超时
map.put("msg", "");
map.put("count",count);
List communityList2=communityService.queryInfox(page,limit);
map.put("data",communityList2);
return JSON.toJSONString(map);
}
六、改
//跳转到编辑页面
@RequestMapping("/community/edit")
public String communityEdit(String id, Model model) {
Community community = communityService.getCommunityById(Integer.parseInt(id));
model.addAttribute("model", community);
return "admin/community/edit";
}
//更新社区信息post
@RequestMapping(value = "/community/update", method = RequestMethod.POST)
public String communityUpdate(String communityname, int id, Model model) {
Community modelX = communityService.getCommunityById(id);
modelX.setCommunityname(communityname);
communityService.update(modelX);
//model.addAttribute("inpost", true);
model.addAttribute("model", modelX);
model.addAttribute("message", "更新成功!");
return "admin/community/edit";
}



