MVC annotation development means a class development based on annotations. For each defined processor, there is no need to register in the xml.
Registration is completed by just annotating classes and methods in the code.
Define the processor
@Controller: The current class is a processor
@RequestMapping: The current method is a processor method, the method name is arbitrary, and the request is processed and responded to.
@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; }} RequestMapping can be restricted, for example
Copy the code as follows: @RequestMapping(value="/hello.do", params={"name=neil", "!age"}, method=RequestMethod.POST)
method=RequestMethod.POST Limited submission type to POST
params={"name=neil", "!age"} The parameter name must be carried in the limited request, the value is neil, and the parameter age cannot be carried.
Accept request parameters
If the parameter name in the request is the same as the parameter name of the processor method, then it can be obtained directly.
@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 need to locate it through @RequestParam.
@RequestParam has three properties
The code copy 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 variables in the URI, that is, path variables.
Like the normal parameters mentioned above, if the path variable name is inconsistent with the parameter name that receives its value, you need to indicate the path variable name through the parameters.
As shown below, note username and name, age and age.
@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 returns value
The following four commonly used processors with @Controller annotation are:
1, Return to ModelAndView
After the processor method is completed, it needs to jump to other resources and pass data between the redirected resources, then return ModelAndView.
public ModelAndView doControll(){ ModelAndView modelAndView = new ModelAndView(); // The passed data modelAndView.addObject("name", "neil"); modelAndView.setViewName("/user.do"); return modelAndView; }2, Return to Void
After the request is processed, there is no need to jump, and the processor can be put back to void, such as Ajax asynchronous request response.
If you need to jump, you can also use the ServletAPI to sendRedirect or forward.
3, return Object
The processor can return an Object object, which does not appear as a logical view, but is used to display data directly on the page.
Returning an Object object, you need to 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); } The front-end gets data
FR.ajax({ url: "hello.do", complete: function(data) { alert(data.name + " " + data.age); } })Similarly, you can return the collection List, Map, etc.
@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, return to String
There may be three scenarios for returning a string:
Logical view name
The string returned by the processor can specify the logical view name and convert it into a physical view address through the view parser resolution.
The final real access path = "prefix" + logical view name + "suffix"
If you do not specify the pre-suffix, you can also directly return the physical view name, such as
return "/WEB-INF/admin/welcome.jsp"
Redirect redirect
return "redirect:/admin/next.action";
It is equivalent to response.sendRedirect(). After forwarding, the browser's address bar becomes the forwarded address.
Since a new request is initiated, the original parameters cannot be passed to the next url when forwarding.
If you want to pass parameters, you can splice parameters after url &a=1&b=2
Forward forwarding
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.
Forwarding does not execute the new request and response, but shares a request and response with the request before forwarding.
Parameters can be reused directly before forwarding.
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.