发表于: 2018-01-29 23:52:03
1 643
完成
1.补写了restful风格的接口
浏览器使用 method 属性设置的方法将表单中的数据传送给服务器进行处理。共有两种方法:POST 方法和 GET 方法。
对这两种方法写restful接口,只需要新增method即可。
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String Edit(Integer id, Model model) {
Student student = studentService.selectById(id);
model.addAttribute("student", student);
return "edit";
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String Edit2(Student student) {
studentService.update(student);
return "redirect:table";
}
对PUT,DELETE方法需要在web.xml文档新加过滤器
<!-- 用来过滤rest中的方法,在隐藏域中的put/delete方式,注意 由于执行顺序原因 一定要放在编码过滤器下面,否则会出现编码问题 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
<init-param>
<param-name>methodParam</param-name>
<param-value>_method</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
相应的的jsp文件中新增这句
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_method" value="DELETE">
控制器中PUT接口
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public String Add() {
return "add";
}
@RequestMapping(value = "/add", method = RequestMethod.PUT)
public String Add2(Student student) {
studentService.insert(student);
return "redirect:table";
}
Tip:此例,我们可以通过浏览器查看request method,进行add操作后,右键选检查。
往下拉,看到使用的是PUT方法。
但是接下来的DELETE接口我今天没有跑通,但是用GET方法可以
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String Delete(Integer id) {
studentService.delete(id);
return "redirect:table";
}
问题出在空指针上,应该是数据传值过程中出现了问题,明天我再看看。
最后我又写了一个查询id语句
@RequestMapping(value = "/look", method = RequestMethod.GET)
public String Search() {
return "search";
}
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String Search2(Integer id, Model model) {
Student student = studentService.selectById(id);
model.addAttribute("student",student);
return "result";
}
@RequestMapping("/return")
public String Return() {
return "redirect:table";
}
result.jsp
<c:if test="${studnet==null}">
你傻呀,不会看看号码再查啊?!
</c:if>
<hr>
<c:if test="${student!=null}">
<form action="${pageContext.request.contextPath}/return" method="post">
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th>身份证号码</th>
<th>你的名字</th>
</tr>
<tr>
<td>${student.id}</td>
<td>${student.user_name}</td>
</tr>
</table>
</form>
</c:if>
这里有个问题,我查询已存在的id,出现如下问题,可是这句话是不该出现的,我暂时找不到问题所在,其他的操作都没有问题。
2.安装了postman,感觉这是测试接口的软件,没什么好讲的。
问题
上面已指出
收获
对rest风格的接口理解加深
计划
任务2剩余的知识
评论