内容简介:Spring获取请求参数非常简单,只要用到如果不指定请求代码是下面这样:
Spring获取请求参数非常简单,只要用到 @RequestParam 注解就可以了
如果不指定请求 method 的话,无论是 get 还是 post 参数都可以轻易获取到
代码是下面这样:
package com.learn.springMVCDemo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class RequestController {
@RequestMapping("requestDemo")
public String doRequest(Model model, @RequestParam(name="info", defaultValue = "some information") String info) {
model.addAttribute("info", info);
return "hello";
}
}
复制代码
在这段代码中 @RequestParam 注解的括号中 name 表示参数名, defaultValue 指明默认的参数值。
GET方法请求这个页面: 访问地址 http://localhost:8080/LearnSpringMVCDemoFirst_war/requestDemo?info=helloWorldFromGETMethod
运行效果:
POST方法请求这个页面:
访问地址 http://localhost:8080/LearnSpringMVCDemoFirst_war/
提交表单
运行结果
二、页面重定向
重定向的时候只要在返回的时候加上 redirect 就可以了:
package com.learn.springMVCDemo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RedirectController {
@RequestMapping("redirect")
public String redirectDemo(Model model) {
model.addAttribute("message", "redirectInfo");
//重定向
return "redirect:/demo";
}
}
复制代码
上面的代码就是重定向到 /demo 页面了。
访问地址: localhost:8080/LearnSpringMVCDemoFirst_war/redirect
三、获取URI路径(@PathVariable)
package com.learn.springMVCDemo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PathVariableController {
@RequestMapping("path/{prefix}/{name}/{suffix}")
public ModelAndView PathDemo(Model model,
@PathVariable(value = "prefix") String prefix,
@PathVariable(value = "name") String name,
@PathVariable(value = "suffix") String suffix) {
ModelAndView mv = new ModelAndView("hello");
mv.addObject("message","website URI path");
model.addAttribute("prefix", prefix);
model.addAttribute("name", name);
model.addAttribute("suffix", suffix);
return mv;
}
}
复制代码
在 @RequestMapping 的括号中固定一个以上的路径名称,然后给分固定的路径名称直接加上大括号,在请求映射的方法参数中加上 @PathVariable 注解的参数中,按照先后顺序一一对应就可以获取到路径名称。
运行结果:
四、不返回视图直接返回字符串
@RequestMapping("ResponseBody")
@ResponseBody
public String RequestBody() {
return "request body message!!!";
}
复制代码
在请求映射方法前直接加上 @ResponseBody 注解,那么返回的就不是视图,而直接是ResponseBody
运行结果:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Python基础入门_5面向对象基础
- Apache Flink 零基础入门(一):基础概念解析
- Apache Flink 零基础入门(一):基础概念解析
- React 入门 (基础概念)
- OALP基础入门
- 入门:基础语法(五)函数
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Applications (Hacking Exposed)
Joel Scambray、Mike Shema / McGraw-Hill Osborne Media / 2002-06-19 / USD 49.99
Get in-depth coverage of Web application platforms and their vulnerabilities, presented the same popular format as the international bestseller, Hacking Exposed. Covering hacking scenarios across diff......一起来看看 《Web Applications (Hacking Exposed)》 这本书的介绍吧!