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


转载自: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值