Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report