Get the App
SLTechnology News&Howtos  ›  Development  › 

SpringMVC asks how to realize the data sharing of domain objects.

Shulou Source: shulou.com Published: 2022-06-01 19:32:16 09月29日 Update

This article mainly explains "SpringMVC asks how to realize the data sharing of domain objects". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "SpringMVC asks how to realize the data sharing of domain objects".

SpringMVC supports placeholders in paths.

You can pass parameters through the path. Restful style. Use {} as a placeholder to specify parameters in the path and the @ PathVariable annotation to specify parameters in the parameter list.

Passed the parameter @ RequestMapping ("/ test/ {id}") public String test (@ PathVariable ("id") Integer id) {System.out.println (id); return "index";}

If a placeholder is used, the request address must have a value, otherwise a 404 error will be reported.

Get request parameters

Get it using ServletAPI (basically not used)

@ RequestMapping ("/ testParam") public String Param (HttpServletRequest request) {String userName = request.getParameter ("userName"); String password = request.getParameter ("password"); return "index";}

It is obtained from the formal parameters of the controller (in the case of the same parameter name).

Passed the parameter @ RequestMapping ("/ testParam") public String testParam (String username,String password) {System.out.println ("username:" + username+ ", password:" + password); return "index";}

RequestParam

Create a mapping relationship between the request parameters and the controller parameters.

Value

Required

DefaultValue

Accept request parameters using entity classes

@ RequestMapping ("/ testPojo") public String testPojo (User user) {System.out.println (user); return "index";}

Configure filters to deal with garbled code problems

CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceResponseEncoding true CharacterEncodingFilter / * Domain objects share data

Use native ServletAPI to share data to request domain objects (no)

@ RequestMapping ("/ test") public String test (HttpServletRequest request) {request.setAttribute ("hello", "hello"); return "index";}

Working with ModelAndView objects

The return value type is ModelAndView

/ / use ModelAndView object @ RequestMapping ("/") public ModelAndView toIndex (HttpServletRequest request) {ModelAndView mav = new ModelAndView (); / / set shared data mav.addObject ("result", "mavResult"); / / set view name / / view name = logical view name. Mav.setViewName ("index"); return mav;}

Working with Model objects

Model is an interface, so you can't go to new like ModelAndView.

/ / use Model object @ RequestMapping ("/") public String toIndexModel (Model model) {/ / set shared data model.addAttribute ("result", "ModelResult"); return "index";}

Use Map collections

/ / use Map object @ RequestMapping ("/") public String toIndexModel (Map map) {/ / set shared data map.put ("result", "MapResult"); return "index";} use ModelMap

An instance of ModelMap is automatically created by the mvc framework and passed in as a controller method parameter, which does not need and cannot be created on its own.

If you create it yourself, you cannot share data.

/ / use ModelMap object @ RequestMapping ("/") public String toIndexModel (ModelMap modelMap) {/ / set shared data modelMap.addAttribute ("result", "ModelMapResult"); return "index";} at this point, I believe you have a deeper understanding of "SpringMVC asks how to realize the data sharing of domain objects". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Object data parameter method name controller view path control no middle finger content method formal parameter learning practical deeper same unable to own garbled Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Xiaomi Linux Docker