package cn.jiyun.controller;
import cn.jiyun.common.DataGridView;
import cn.jiyun.pojo.Emp;
import cn.jiyun.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Controller
public class EmpController {
@Autowired
public EmpService empService;
/*@Autowired
public RestTemplate restTemplate;*/
/* @Autowired//选cloud包
public DiscoveryClient discoveryClient;*/
/* @RequestMapping(“list”)
@ResponseBody
public List list(Teacher teacher){
//List list = restTemplate.getForObject("http://localhost:8090/show", List.class);
//
String json = JsonUtil.obj2String(list);
System.out.println(“json串”+json);
System.out.println("===============================");
List list1 = JsonUtil.string2Obj(json, List.class);
System.out.println("反序列化+"+list1.toString());*//*
*//* //获取服务集群
List serviceInstanceList = discoveryClient.getInstances("shengchan");
//获取当前服务实例
ServiceInstance instance = serviceInstanceList.get(0);
//请求地址动态拼接
String url="http://"+instance.getHost()+":"+instance.getPort()+"/show";*//*
//负载均衡
String url="http://shengchan/show";
List list = restTemplate.getForObject(url, List.class);
System.out.println("================="+"消费者");
return list;
}*/
@RequestMapping(value = "show")
@ResponseBody
public DataGridView show(Emp emp, Model model){
List list=empService.show(emp);
//model.addAttribute("list",list);
return new DataGridView(list);
}
//添加
@RequestMapping("toadd")
public String toadd(){
return "add";
}
//去页面
/* @RequestMapping("/{show}")
public String toshow(@PathVariable(“show”)String show){
return show;
}*/
//去页面
@RequestMapping("toshow")
public String show2(){
return "show";
}
@RequestMapping("add")
public String add(@Valid Emp emp, BindingResult bindingResult,Model model) throws ParseException {
if(!bindingResult.hasErrors()){
String date = emp.getDate();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date birthday = format.parse(date);
emp.setBirthday(birthday);
empService.add(emp);
return "redirect:show";
}
else{
List list = bindingResult.getAllErrors();
for (int i = 0; i < list.size(); i++) {
FieldError error = (FieldError )list.get(i);
String field = error.getField();
String message = error.getDefaultMessage();
System.out.println(field+"出错"+"错误信息:"+message);
HashMap map = new HashMap<>();
map.put(field,message);
model.addAllAttributes(map);
}
return "add";
}
}
//修改
@RequestMapping("update")
public String update(Emp emp) throws ParseException {
String date = emp.getDate();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date birthday = format.parse(date);
emp.setBirthday(birthday);
empService.update(emp);
return "redirect:show";
}
@RequestMapping("toupdate")
public String toupdate(Emp emp, Model model) throws ParseException {
Emp e=empService.findByid(emp.getEid());
Date birthday = e.getBirthday();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
String s = format.format(birthday);
e.setDate(s);
model.addAttribute("e",e);
return "update";
}
@RequestMapping("deletebyid")
public String deletebyid(Emp emp, Model model) throws ParseException {
empService.deletebyid(emp.getEid());
return "redirect:show";
}
}
html页面