@RequestMapping 注解为控制器指定可以处理哪些 URL 请求. 三、SpringMVC 常用注解
@Controller
负责注册一个 bean 到 spring 上下文中。
@RequestMapping
注解为控制器指定可以处理哪些 URL 请求。
@RequestBody
该注解用于读取 Request 请求的 body 部分数据,使用系统默认配置的 HttpMessageConverter 进行解析,然后把相应的数据绑定到要返回的对象上 ,再把 HttpMessageConverter 返回的对象数据绑定到 controller 中方法的参数上。
@ResponseBody
该注解用于将 Controller 的方法返回的对象,通过适当的 HttpMessageConverter 转换为指定格式后,写入到 Response 对象的 body 数据区。
@ModelAttribute
五、自动装箱 1. 编写一个 Person 实体类
- //match automatically
- @RequestMapping("/person")
- public String toPerson(String name,double age){
- System.out.println(name+" "+age);
- return "hello";
- }
2. 在 Controller 里编写方法
- package test.SpringMVC.model;
- public class Person {
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- private String name;
- private int age;
- }
六、使用 InitBinder 来处理 Date 类型的参数
- //boxing automatically
- @RequestMapping("/person1")
- public String toPerson(Person p){
- System.out.println(p.getName()+" "+p.getAge());
- return "hello";
- }
七、向前台传递参数
- //the parameter was converted in initBinder
- @RequestMapping("/date")
- public String date(Date date){
- System.out.println(date);
- return "hello";
- }
- //At the time of initialization,convert the type "String" to type "date"
- @InitBinder
- public void initBinder(ServletRequestDataBinder binder){
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
- true));
- }
前台可在 Request 域中取到 "p" 八、使用 Ajax 调用
- //pass the parameters to front-end
- @RequestMapping("/show")
- public String showPerson(Map<String,Object> map){
- Person p =new Person();
- map.put("p", p);
- p.setAge(20);
- p.setName("jayjay");
- return "show";
- }
前台用下面的 Jquery 代码调用:
- //pass the parameters to front-end using ajax
- @RequestMapping("/getPerson")
- public void getPerson(String name,PrintWriter pw){
- pw.write("hello,"+name);
- }
- @RequestMapping("/name")
- public String sayHello(){
- return "name";
- }
九、在 Controller 中使用 redirect 方式处理请求
- $(function() {
- $("#btn").click(function() {
- $.post("mvc/getPerson", {
- name: $("#name").val()
- },
- function(data) {
- alert(data);
- });
- });
- });
十、文件上传 1. 需要导入两个 jar 包 2. 在 SpringMVC 配置文件中加入
- //redirect
- @RequestMapping("/redirect")
- public String redirect(){
- return "redirect:hello";
- }
3. 方法代码:
- <!-- upload settings -->
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="102400000">
- </property>
- </bean>
4. 前台 form 表单
- @RequestMapping(value="/upload",method=RequestMethod.POST)
- public String upload(HttpServletRequest req) throws Exception{
- MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
- MultipartFile file = mreq.getFile("file");
- String fileName = file.getOriginalFilename();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
- FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
- "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
- fos.write(file.getBytes());
- fos.flush();
- fos.close();
- return "hello";
- }
十一、使用 @RequestParam 注解指定参数的 name
- <form action="mvc/upload" method="post" enctype="multipart/form-data">
- <input type="file" name="file">
- <br>
- <input type="submit" value="submit">
- </form>
- @Controller
- @RequestMapping("/test")
- public class mvcController1 {
- @RequestMapping(value="/param")
- public String testRequestParam(@RequestParam(value="id") Integer id,
- @RequestParam(value="name")String name){
- System.out.println(id+" "+name);
- return "/hello";
- }
- }
来源: http://www.2cto.com/kf/201703/616758.html