springbootq请求访问_springboot获取URL请求参数的多种方式

1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。

/*** 1.直接把表单的参数写在Controller相应的方法的形参中

*@paramusername

*@parampassword

*@return

*/@RequestMapping("/addUser1")publicString addUser1(String username,String password) {

System.out.println("username is:"+username);

System.out.println("password is:"+password);return "demo/index";

}

url形式:http://localhost/demo/addUser1?username=lixiaoxi&password=111111 提交的参数需要和Controller方法中的入参名称一致。

2、通过HttpServletRequest接收,post方式和get方式都可以。

/*** 2、通过HttpServletRequest接收

*@paramrequest

*@return

*/@RequestMapping("/addUser2")publicString addUser2(HttpServletRequest request) {

String username=request.getParameter("username");

String password=request.getParameter("password");

System.out.println("username is:"+username);

System.out.println("password is:"+password);return "demo/index";

}

3、通过一个bean来接收,post方式和get方式都可以。

(1)建立一个和表单中参数对应的bean

public classUserModel {privateString username;privateString password;publicString getUsername() {returnusername;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}

}

(2)用这个bean来封装接收的参数

/***通过一个bean来接收

*@paramuser

*@return

*/@RequestMapping("/addUser3")publicString addUser3(UserModel user) {

System.out.println("username is:"+user.getUsername());

System.out.println("password is:"+user.getPassword());return "demo/index";

}

4、通过@PathVariable获取路径中的参数

**

* 4、通过@PathVariable获取路径中的参数*@param username*@param password* @return

*/@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)publicString addUser4(@PathVariable String username,@PathVariable String password) {

System.out.println("username is:"+username);

System.out.println("password is:"+password);return "demo/index";

}

例如,访问http://localhost/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。

5、使用@ModelAttribute注解获取POST请求的FORM表单数据

Jsp表单如下:

用户名:
密 码:

Java Controller如下:

/*** 5、使用@ModelAttribute注解获取POST请求的FORM表单数据

*@paramuser

*@return

*/@RequestMapping(value="/addUser5",method=RequestMethod.POST)public String addUser5(@ModelAttribute("user") UserModel user) {

System.out.println("username is:"+user.getUsername());

System.out.println("password is:"+user.getPassword());return "demo/index";

}

6、用注解@RequestParam绑定请求参数到方法入参

当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)

/*** 6、用注解@RequestParam绑定请求参数到方法入参

*@paramusername

*@parampassword

*@return

*/@RequestMapping(value="/addUser6",method=RequestMethod.GET)public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {

System.out.println("username is:"+username);

System.out.println("password is:"+password);return "demo/index";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RequestMapping("/upload") public R upload(@RequestParam("file") MultipartFile file,String type) throws Exception { if (file.isEmpty()) { throw new EIException("上传文件不能为空"); } String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); File path = new File(ResourceUtils.getURL("classpath:static").getPath()); if(!path.exists()) { path = new File(""); } File upload = new File(path.getAbsolutePath(),"/upload/"); if(!upload.exists()) { upload.mkdirs(); } String fileName = new Date().getTime()+"."+fileExt; File dest = new File(upload.getAbsolutePath()+"/"+fileName); file.transferTo(dest); /** * 如果使用idea或者eclipse重启项目,发现之前上传的图片或者文件丢失,将下面一行代码注释打开 * 请将以下的"D:\springbootq33sd\src\main\resources\static\upload"替换成你本地项目的upload路径, * 并且项目路径不能存在中文、空格等特殊字符 */ // FileUtils.copyFile(dest, new File("D:\springbootq33sd\src\main\resources\static\upload"+"/"+fileName)); /修改了路径以后请将该行最前面的//注释去掉/ if(StringUtils.isNotBlank(type) && type.equals("1")) { ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile")); if(configEntity==null) { configEntity = new ConfigEntity(); configEntity.setName("faceFile"); configEntity.setValue(fileName); } else { configEntity.setValue(fileName); } configService.insertOrUpdate(configEntity); } return R.ok().put("file", fileName); }请解释上述代码的逻辑
05-05

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值