Spring MVC annotation development and use details, mvc use Details
MVC annotation-based development is annotation-based class development for processors. For each defined processor, you do not need to register it in xml.
You only need to annotate classes and methods in the code to complete registration.
Define Processor
@ Controller: The current class is a processor.
@ RequestMapping: the current method is a processor method, and the method name is random. It processes and responds to requests.
@ Controller public class MyController {@ RequestMapping (value = "/hello. do ") public ModelAndView doControl (HttpServletRequest request, HttpServletResponse response) {ModelAndView mv = new ModelAndView (); mv. addObject ("message", "execution method"); mv. setViewName ("welcome, neil! "); Return mv ;}}
You can restrict requests in RequestMapping, for example
Copy codeThe Code is as follows: @ RequestMapping (value = "/hello. do", params = {"name = neil ","! Age "}, method = RequestMethod. POST)
Method = RequestMethod. POST: The submission type is POST.
Params = {"name = neil ","! Age "} the parameter name must be included in the request and the value is neil. The parameter age cannot be included.
Accept Request Parameters
If the parameter names in the request are the same as those in the processor method, you can directly obtain them.
@RequestMapping(value="/hello.do") public ModelAndView doControll(String name, int age) { System.out.println("Name : " + name + ", Age: " + age); ModelAndView mv = new ModelAndView(); return mv; }
If the parameter names are inconsistent, you must use @ RequestParam to locate the parameter.
@ RequestParam has three attributes
- Name/value: Specifies the name of the request parameter.
- Required: whether the parameter is required. If it is false, it indicates whether the parameter is acceptable.
- DefaultValue: Specifies the default value of the current parameter when no parameter is included in the request.
Copy codeThe Code is as follows: doControll (@ RequestParam (name = "username") String name, @ RequestParam (name = "userage") int age)
PATH variable @ PathVariable
The parameters in the processor method can come from the parameters carried by the request, or from the URI variable, that is, the PATH variable.
Like the preceding normal parameters, if the PATH variable name is inconsistent with the parameter name that receives the value, you must specify the PATH variable name through the parameter.
Pay attention to username and name, age and age as follows.
@RequestMapping(value="/{username}/{age}/hello.do") public ModelAndView doControll(@PathVariable("username") String name, @RequestParam int age) { System.out.println("Name : " + name + ", Age: " + age); ModelAndView mv = new ModelAndView(); return mv; }
Processor method Return Value
The @ Controller annotation is used for the processor. The return values of the method are usually as follows:
- ModelAndView
- Void
- Object, custom type Object
- String
1. Return ModelAndView
After the processor method is complete, you need to jump to other resources, and you need to transfer data between the resources to jump, then return ModelAndView.
Public ModelAndView doControll () {ModelAndView modelAndView = new ModelAndView (); // pass the data modelAndView. addObject ("name", "neil"); modelAndView. setViewName ("/user. do "); return modelAndView ;}
2. Return Void
After the request is processed, you do not need to jump. You can place the processor to return the void, for example, Ajax asynchronous request response.
If you need to redirect, you can also perform sendRedirect or forward operations through the ServletAPI.
3. Return the Object
The processor can return the Object. In this case, it does not appear as a logic view, but directly displays data on the page.
To return the Object, use the @ ResponseBody annotation to put the converted JSON data into the response weight.
@RequestMapping(value="/hello.do") @ResponseBody public ModelAndView doControll() { return new Student("neil", 998); }
Data retrieved by the front-end
FR.ajax({ url: "hello.do", complete: function(data) { alert(data.name + " " + data.age); } })
You can also return the List, Map, and so on.
@RequestMapping(value="/hello.do") @ResponseBody public ModelAndView doControll() { List<Student> list = new ArrayList<Student>(); list.add(new Student("a", 11)); list.add(new Student("b", 22)); list.add(new Student("c", 33)); return list; }
FR.ajax({ url: "hello.do", complete: function(data) { $(data).each(function(index)) { alert(data[index].name + data[index].age); } } })
4. Returns a String.
There are three possible scenarios for returning strings:
- Logical view name
- Redirect redirection
- Forward
Logical view name
The string returned by the processor can specify the logical view name and convert it to the physical view Address through view parser resolution.
The actual access path = "prefix" + logical view name + "suffix"
If you do not specify the prefix or suffix, you can directly return the physical view name, as shown in figure
return "/WEB-INF/admin/welcome.jsp"
Redirect redirection
return "redirect:/admin/next.action";
This is equivalent to response. sendRedirect (). After forwarding, the address bar of the browser changes to the address after forwarding.
Because a new request is initiated, the original parameter cannot be passed to the next url during forwarding,
If you want to transmit parameters, You can splice the Parameters & a = 1 & B = 2 behind the url.
Forward
return "forward:/admin/forward.action";
It is equivalent to request. getRequestDispatcher (). forward (request, response). After forwarding, the browser address bar is still the original address.
The forwarding does not execute the new request and response, but shares a request and response with the previous request.
Parameters can be reused directly before forwarding.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.