In the process of learning Spring Mvc, it is necessary to first understand several key parameters:
@Controller:
Annotated on the class, this class will program a controller, and when the project starts Spring, it will automatically scan for this class and perform the corresponding URL routing mapping.
@Controllerpublic class UserAction{ } @RequestMapping
Specify the URL mapping path. If RequestMapping is configured on the controller, the specific request method also configures the path. The mapped path is the overlay of the two paths. Commonly used maps such as: RequestMapping("url.html")
Configure the mapping path:
@Controllerpublic class UserAction { @RequestMapping(value = "/get_alluser.html") public ModelAndView GetAllUser(String Id) { }} The above configuration mapping
http://***:8080:web1/get_alluser.html:
If you add @RequestMapping(value = "/user") to @Controller, the map path becomes
http://***:8080:web1/user/get_alluser.html
@ResponseBody
Return the string corresponding to the annotation method directly
@RequestParam
Automatically map the parameters corresponding to the URL to the values above the Action, and RequestParam defaults to required parameters.
@PathVariable
Get the URL mapping parameters of the specified format of @RequestMapping configuration
/* * Direct output HTML, or JSON string* Request path: * /web1/urlinfo/getcontent.html?key=rhythmk * /web1/urlinfo/getcontent.json?key=rhythmk * */ @ResponseBody @RequestMapping(value = "/getcontent.**") public String GetContent( @RequestParam("key") String key, @RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) { System.out.println("getcontent is called"); String result = "Return content directly - key:" + key + ",key2:" + key2; System.out.println(result); return result; } /* * RequestMapping supports Ant-style URL configuration: * Request path: * /urlinfo/geturlant/config.html?key=adddd */ @ResponseBody @RequestMapping(value = "/geturlant/**.html") public String getUrlAnt(HttpServletRequest request) { String result = "?The following parameters are: " + request.getQueryString(); return result; } /* * Configure the URL of the specified format and map to the corresponding parameters* Request path: /web1/urlinfo/geturlparam/12_123.html * */ @RequestMapping(value = "/geturlparam/{id}_{menuId}.html") public ModelAndView getUrlParam(@PathVariable("id") String id, @PathVariable("menuId") String menuId) { ModelAndView mode = new ModelAndView(ShowMsg); mode.addObject("msg", "Geted Id:" + id + ",menuId:" + menuId); return mode; } /* * Only Post requests are received*/ @ResponseBody @RequestMapping(value = "/posturl.html", method = RequestMethod.POST) public String UrlMethod(@RequestParam String id) { return "Only Post request, the Id obtained:" + id; } /* * Write cookies * */ @RequestMapping("/writecookies.html") public ModelAndView writeCookies(@RequestParam String value, HttpServletResponse response) { response.addCookie(new Cookie("key", value)); ModelAndView mode = new ModelAndView(ShowMsg); mode.addObject("msg", "cookies written successfully"); return mode ; } /* * Get the corresponding key value through @CookieValue* */ @RequestMapping("/getcookies.html") public ModelAndView getCookie(@CookieValue("key") String cookvalue) { ModelAndView mode = new ModelAndView(ShowMsg); mode.addObject("msg", "cookies=" + cookvalue); return mode; } /* * Pass the Servlet Api as a parameter in * You can use HttpServletResponse directly in the action, HttpServletRequest * */ @RequestMapping("/servlet.html") public String Servlet1(HttpServletResponse response, HttpServletRequest request) { Boolean result = (request != null && response != null); ModelAndView mode = new ModelAndView(); mode.addObject("msg", "result=" + result.toString()); return ShowMsg; } /* * Instantiate the object according to the parameters passed in the URL* * For example: http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad * */ @RequestMapping("getobject.html") public ModelAndView getObject(UserInfo user) { String result = "User ID:" + user.getUserId().toString() + ",Username:" + user.getUserName().toString(); ModelAndView mode = new ModelAndView(ShowMsg); mode.addObject("msg", "result=" + result.toString()); return mode; } Implement page jump:
/* * Implement page jump* /web1/urlinfo/redirectpage.html * */ @RequestMapping("/redirectpage.html") public String RedirectPage() { return "redirect:getcookies.html?r=10"; } Return JSON directly
The requested URL must end in .json, otherwise exception
Failed to load resource: the server responded with a status of 406 (Not Acceptable): The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
Return entity:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)public class UserInfo { private Integer UserId; public Integer getUserId() { return UserId; } public void setUserId(Integer userId) { UserId = userId; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } private String UserName; }Return action
@ResponseBody @RequestMapping("/getuser.json") public UserInfo GetUser() { System.out.println("getuser"); UserInfo model=new UserInfo(); model.setUserId(100); model.setUserName("Wang Kun"); return model; } ask:
/web1/urlinfo/getuser.json
Output:
{"userId":100,"userName":"Wang Kun"}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.