发表于: 2017-12-21 21:43:59
2 714
今天完成的事情:
跟师弟讨论了关于restful风格的事情,发现了什么才是真的restful风格,收益颇丰,对任务的代码进行了重构,另外尝试了使用json-taglib,虽然不知道是干嘛的....
package com.controller;
import com.bean.Student;
import com.bean.StudentGet;
import com.bean.StudentPut;
import com.service.IService;
import com.util.ChangeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.List;
/**
* @author Arike
* Create_at 2017/12/14 11:55
*/
@Controller
@RequestMapping("/student")
public class MyController {
@Autowired
private IService servicelmpl;
private Student student;
private Logger logger = Logger.getLogger(MyController.class);
//增加方法
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String post(StudentGet sg) {
student = ChangeUtil.studentChange(sg);
student.setCreate_at(System.currentTimeMillis() / 1000);
long begin = System.currentTimeMillis();
servicelmpl.insertStudent(student);
logger.info("增加耗时:" + ((double) (System.currentTimeMillis() - begin)) / 1000 + "秒");
return "redirect:list";
}
//查询所有方法
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
long begin = System.currentTimeMillis();
List<Student> list = servicelmpl.selectAll();
List<StudentPut> list2 = new ArrayList<>();
for (Student student1 : list) {
list2.add(ChangeUtil.timeChange(student1));
}
model.addAttribute("studentList", list2);
logger.info("查询耗时:" + ((double) (System.currentTimeMillis() - begin)) / 1000 + "秒");
return "list";
}
//删除方法
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Long id) {
long begin = System.currentTimeMillis();
servicelmpl.deleteStudent(id);
logger.info("删除耗时:" + ((double) (System.currentTimeMillis() - begin)) / 1000 + "秒");
return "redirect:list";
}
//下面2个是更新的方法
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String getdata(@PathVariable("id") Long id, Model model) {
Student s = servicelmpl.getStudentById(id);
model.addAttribute("newstudent", s);
return "update";
}
@RequestMapping(value = "/id", method = RequestMethod.PUT)
public String update( Long id, StudentGet sg) {
student = ChangeUtil.studentChange(sg);
student.setId(id);
long begin = System.currentTimeMillis();
student.setUpdate_at(System.currentTimeMillis() / 1000);
servicelmpl.updateStudent(student);
logger.info("更新耗时:" + ((double) (System.currentTimeMillis() - begin)) / 1000 + "秒");
return "redirect:list";
}
@RequestMapping(value = "/json1", method = RequestMethod.GET)
public String json(ModelMap modelMap){
List<String> list = new ArrayList<>();
list.add("123");
list.add("234");
list.add("567");
modelMap.addAttribute("list", list);
return "json1";
}
}
使用restful风格只能使用/{}这种方式接受参数,这样在页面进行点击就会跳转到一样的url,但是功能却不相同,判断新增还是删除就根据我们定义的restful方法来了,之前我一直搞的是假的rest
可以看到url已经变成了student/这种,因为删除直接回到主页面我就不演示了,点击删除和修改的url是同一个.
json-taglib
@RequestMapping(value = "/json1", method = RequestMethod.GET)
public String json(ModelMap modelMap){
List<String> list = new ArrayList<>();
list.add("123");
list.add("234");
list.add("567");
modelMap.addAttribute("list", list);
return "json1";
}
在controller里面这种方式是使用的添加model的方式传递,和增加预定值一样.在jsp文件中改成了用json标签来进行数据的接收.
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<%--
Created by IntelliJ IDEA.
User: yyj
Date: 2017/12/21
Time: 19:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<json:object>
<json:property name="123" value="${list}"/>
</json:object>
</body>
</html>
在页面中显示为
在页面中显示的就是以json格式显示数据.
json还有另外一种方式获取,还没写,明天再尝试.
明天计划的事情:
做分页查询.以及姓名模糊查询
遇到的问题:
各种跳转上的问题.
更新一个奇葩问题:
在jetty上能没有问题运行的项目,放到tomcat就疯狂报错.
收获:
spring mvc 真好玩.
评论