springmvc page biography worth method, there are five.

springmvc by value, there are 5 ways (this article is reproduced),

1.request get the value:

@RequestMapping("/request.action")
public String request(HttpServletRequest request){
    String value= (String) request.getAttribute("value");
    String val=request.getParameter("value");
    return "index";
}

 

The request getParameter and getAttribute What difference does it make?


getAttribute : obtaining a value setAttribute set value range session can be set to Object, the object, the string; getAttribute value acquired internal web container, having a shared forwarding relationship between the web component values; means for server-side redirect


getParameter : get value from web-form form post / get, or pass over the url, only String string; getParameter value obtained is passed to end web server, and the data acquisition http submitted over; for client-side redirects.


2. Use a path variable parameter binding @PathVariable page url path for the jump page

@Controller
public class BaseController {

    @RequestMapping("/goUrl/{folder}/{file}")
    public String goUrl(@PathVariable String folder,@PathVariable String file){
        return  folder+"/"+file;
    }
}

 

3. came through @RequestParam binding parameters page, with effect

String id = request.getParameter ( "id") is the same:

@RequestMapping("/test.action")
public void test(@RequestParam("id") String id){
    System.out.println("id:"+id);
    
}

4. The automatic injection, entity class attributes setter, getter methods, distal form corresponding to the form name attribute name of an entity, directly back to the parameter can be automatically bound by the attribute class entity class.

For example: the entity class

public class Content {
   

   private String content;
public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
} 
}

 

form form:

<form action="<%=request.getContextPath()%>/content" method="post" enctype="multipart/form-data">

    商品描述:<textarea  name="content" rows="2" cols="20"></textarea><br>
   
  
    <input type="submit" value="提交"/>
</form>

Background receive data:

@RequestMapping("/content")
public  void contetn(Content content){
    System.out.println("content:"+content.getContent());
}

 

5. RequestBody accept front came json array objects. ResponseBody the data returned

@RequestMapping("/test.action")
@ResponseBody
public void test(@RequestBody List<Content> list){
  for (Content content:list){
      System.out.println(content.toString());
  }

}

 

Guess you like

Origin www.cnblogs.com/sdgtxuyong/p/12470271.html