springMvc注解@RequestParam用法解析

本文链接:https://blog.csdn.net/sinat_35626559/article/details/78317124


一、获取指定的参数(必填)

controller 中的写法


    @RequestMapping("/fetchItemDetail.do")
    @ResponseBody
    public String fetchItemDetail(@RequestParam String userName) {
        JSONObject json = new JSONObject();
        System.out.println("userName = [" + userName + "]");
        return json.toJSONString();
    }

在上述 controller 方法中用 @RequestParm String userName 指明当前方法接收的参数名称,则在当前 url 请求中必须携带该参数,若前台请求中,未传入当前指定的参数,后台返回 400 错误,如下图


若前台传入多个参数,而后台controller 只设置了一个参数,当请求到达后台 controller 后,后台只会接收当前设定的参数。

二,获取指定的参数(非必填)

controller 中的写法


    @RequestMapping("/fetchItemDetail.do")
    @ResponseBody
    public String fetchItemDetail(@RequestParam(required = false) String userName) {
        JSONObject json = new JSONObject();
        System.out.println("userName = [" + userName + "]");
        return json.toJSONString();
    }

若当前参数非必填,则将 @RequestParm 中设置 require = false  即可。


三、获取指定参数(设默认值)

controller 中的写法


    @RequestMapping("/fetchItemDetail.do")
    @ResponseBody
    public String fetchItemDetail(@RequestParam(defaultValue = "哈哈哈") String userName) {
        JSONObject json = new JSONObject();
        System.out.println("userName = [" + userName + "]");
        return json.toJSONString();
    }
不管当前参数是否为必填,如需要给当前设默认值,则在 @RequestParm 中设置 defaultValue = "需要的默认值" 即可,这样,当前台没有传入该参数时,后台会默认给一个设置的默认值。


四、给指定参数取别名

因为某些原因,当前台 url 请求传送上来的参数和我们后台设置的接收参数名称不一样时,可以用 @RequestParm(name = "前台传入的名称")  进行参数名称转换,SpringMVC 自动将值注入到 后面controller 中设置的接收参数中,如下面代码


    @RequestMapping("/fetchItemDetail.do")
    @ResponseBody
    public String fetchItemDetail(@RequestParam(name = "name") String userName) {
        JSONObject json = new JSONObject();
        System.out.println("userName = [" + userName + "]");
        return json.toJSONString();
    }


上述方法中参数的设置,前台 url 参数传入的参数名称:name  ,后台SpringMVC 会自动将 name中的值注入到后台设置的真实接收参数 userName 中。

以上几种可以在实际运用中进行组合使用。


————————————————
版权声明:本文为CSDN博主「_修铁路的」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_35626559/article/details/78317124

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Spring MVC进行文件上传时,需要注意以下几点: 1. 首先,在Spring MVC配置文件中启用multipart解析器。可以通过在servlet-context.xml或applicationContext.xml文件中添加以下配置来完成: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="com.example" /> <mvc:annotation-driven /> <!-- 配置multipart解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000000"/> </bean> </beans> ``` 这里我们使用了CommonsMultipartResolver类来处理文件上传,还可以通过maxUploadSize属性设置上传文件的最大大小。 2. 在Controller中编写处理文件上传的方法,并使用@RequestParam注释来接收上传的文件。例如: ```java @RequestMapping(value="/upload", method=RequestMethod.POST) public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // 文件上传逻辑处理 } catch (Exception e) { e.printStackTrace(); } } return "redirect:/successUrl"; } ``` 在上面的代码中,我们使用@RequestParam注释将上传的文件绑定到MultipartFile对象中,并使用getBytes()方法获取文件内容。 3. 最后,在表单中添加文件上传控件,并指定form的enctype属性为“multipart/form-data”。例如: ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> ``` 这样就完成了文件上传的全部过程。注意,在实际应用中,还需要对上传的文件进行安全检查,例如检查文件类型、文件大小等,以确保应用的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值