Spring Mvc——Controller中常规方法示例

来源:互联网 发布:论网络诈骗的法律规范 编辑:程序博客网 时间:2024/05/17 23:33


转载自:http://blog.csdn.net/lhc1105/article/details/50960244

一,简单无参数地址访问

   首先来看下类标记:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Created by LiuHuiChao on 2016/3/21. 
  3.  */  
  4. @Controller  
  5. @RequestMapping("/hello")  
  6. public class HelloMvcController {  

    简单进行类中方法的访问:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*简单访问示例*/  
  2.     @RequestMapping("/mvc")  
  3.     public String helloMvc() {  
  4.         return "home";  
  5.     }  

二,使用Servlet api方式获取参数


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* request方式获取参数 */  
  2.     @RequestMapping("/views3")  
  3.     public String viewCourse3(HttpServletRequest request) {  
  4.         Integer courseId = Integer.valueOf(request.getParameter("courseId"));  
  5.         System.out.println(courseId);  
  6.         return "home";  
  7.     }  


三,通过方法参数方式获取


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 本方法处理 /hello/view?courseId=123 */  
  2.     @RequestMapping(value = "/views", method = RequestMethod.GET)  
  3.     public String viewCourse(@RequestParam("courseId") Integer courseId) {  
  4.         System.out.println(courseId);  
  5.         return "home";  
  6.     }  

四,restful 风格URL参数获取


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* restful 风格URL示例 */  
  2.     /* 本方法处理 /hello/view/{courseId} */  
  3.     @RequestMapping(value = "/views/{courseId}", method = RequestMethod.GET)  
  4.     public String viewCourse2(@PathVariable("courseId") Integer courseId,  
  5.             Map<String, Object> model) {  
  6.         System.out.println("restful风格URL示例测试---" + courseId);  
  7.         // model.put("course",courseId);  
  8.         return "home";  
  9.     }  

五,重定向操作


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*重定向操作*/  
  2.     @RequestMapping(value = "/save", method = RequestMethod.POST)  
  3.     public String doSave(@ModelAttribute Course course) {  
  4.         // 再此处进行save操作  
  5.         return "redirect:views/" + course.getCourseId();// 重定向  
  6.     }  

六,接收上传的文件


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*上传文件示例*/  
  2.     @RequestMapping(value="/doUpload",method=RequestMethod.POST)  
  3.     public String doUploadFile(@RequestParam("file") MultipartFile file){  
  4.           
  5.         if(!file.isEmpty()){  
  6.             System.out.println("请在这里写入对文件的操作");  
  7.             //写入文件等操作。。。。  
  8.         }  
  9.           
  10.         return "success";  
  11.     }  

   另外,还需要在spring mvc的配置文件中加入:


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置上传文件 -->  
  2.    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <property name="maxUploadSize" value="209715200"/>  
  4.     <property name="defaultEncoding" value="UTF-8"/>  
  5.     <property name="resolveLazily" value="true"/><!-- 延迟加载 -->  
  6.      
  7.    </bean>  


1 0
原创粉丝点击