发表于: 2017-09-26 23:10:47

2 754


今天完成的事
写了一个rest风格的接口
显示总信息的列表
@RequestMapping("/student")
public String list(Map<String,Object>map){
map.put("students",studentService.studentAll());
return "list";
}
//增加
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String save(Student student){
studentService.studyInsert(student);
return "redirect:/student";
//删除
@RequestMapping(value = "/del/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
studentService.studyDelete(id);
return "redirect:/student";
//修改
@RequestMapping(value = "/up/{id}",method = RequestMethod.GET)
public String input(@PathVariable("id")Integer id,Model model){
Student student=studentService.selectById(id);
model.addAttribute("student",student);
return "in";
}
用了@PathVariable绑定参数到方法里 
因为涉及到form表单只支持 post get 所以用了下面这种写法
使用了一个隐藏域把post转化为delete
form action="del/{id}" method="post">
<input type="hidden" name="_method" value="DELETE"/>
在web.xml里设置过滤器
<filter>    
 <filter-name>springCharacterEncodingFilter</filter-name>  
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
 <init-param>        <param-name>forceEncoding</param-name>      
  <param-value>true</param-value>  
   </init-param>   
  <init-param>      
 <param-name>encoding</param-name>      
  <param-value>UTF-8</param-value>    
 </init-param> 
  </filter> 
  <filter-mapping>     
 <filter-name>springCharacterEncodingFilter</filter-name>     
 <url-pattern>/*</url-pattern>   </filter-mapping>
用到的知识点
@PathVariable
@PathVariable是用来对指定请求的URL路径里面的变量 eg: Java代码 @RequestMapping(value = "form/{id}/apply", method = {RequestMethod.PUT, RequestMethod.POST}) {id}在这个请求的URL里就是个变量,可以使用@PathVariable来获取 @PathVariable和@RequestParam的区别就在于:@RequestParam用来获得静态的URL请求入参
隐藏域
隐藏域的作用是自己想默认传递一些值,但又不想让自己或别人不小心修改到它,例如在修改一篇文章时<form name="form1" action="" method="post"><input type="text" name="title" id="title" value="这里是从数据库里读出来的标题"><textarea name="content" id="content">这里是从数据库里读出来的内容</textarea><input type="hidden" name="sid" id="sid" value="这里是从数据库里读出来的ID"><input type="submit" value="修改"></form>这里的隐藏域就是要把SID隐藏起来,如果你不小心修改了它,你再次提交回数据库里就会出错
明天计划的事
尝试写个简单的jsp管理页面,实现增删改差
问题
收获
告诉自己,做事情要专注,要有计划



返回列表 返回列表
评论

    分享到