发表于: 2017-08-25 23:02:53

1 849


今天完成的事情: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";
}

 rest风格主要就是简洁明快 调用四个方法 put get post delect

用了@PathVariable绑定参数到方法里 

因为涉及到form表单只支持 post get 所以用了下面这种写法


<form action="del/{id}" method="post">
   <input type="hidden" name="_method" value="DELETE"/>

使用了一个隐藏域 把post转化成了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>

@Autowired
private StudentDao studentDao;

接口实现里面 用这个注解注入业务层的bean

但是页面jsp  js 写起来完全不对

<a href="sucess2">Hello World</a>

超链接 Hello World 是可以点击的超链接  点进去是发送了一个/sucess2的请求 

根据web.xml里面拦截器的配置拦截 交给相应的控制类  控制类调用了方法 得到返回值 再根据视图解析器来决定返回

 那一个页面 也就是jsp

明天计划的事情:放弃js 只用jsp最简单的标签来尝试 
遇到的问题:js看不懂 看了json 也没有理解 
收获:对常用的一些注解明白了它们的作用 

           了解了基本的调用 从起始的jsp页面 添加链接 进行转发

            调用相应的方法 根据返回值 最调到相应的页面

             了解了pojo也可以直接绑定 







禅道链接:http://task.ptteng.com/zentao/my-task.html


返回列表 返回列表
评论

    分享到