MVC annotation-style development that is based on class processor developed annotation, defined for each processor, without registration in xml.
Just in the code by annotating the class and methods, to complete the registration.
Defined processor
@Controller: the current class processor
@RequestMapping: Current methods for the processor method, method name at random, and in response to the request is processed.
@Controller public class MyController { @RequestMapping(value = "/hello.do") public ModelAndView doControl(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView(); mv.addObject("message", "执行方法"); mv.setViewName("welcome, neil!"); return mv; } }
Requests may be limited in RequestMapping, e.g.
@RequestMapping(value="/hello.do", params={"name=neil", "!age"}, method=RequestMethod.POST)
method = RequestMethod.POST defined type is filed POST
params = { "name = neil", "! age"} request must carry parameters defined name, value neil, the parameter can not carry age
Accept the request parameters
If the parameter name and the parameter name request processor consistent method, it may 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 name is inconsistent, it needs to be positioned by @RequestParam.
There are three properties @RequestParam
- name / value: name of the parameter designation request.
- required: whether the parameter must, if it is false, it means that there are no parameters available.
- defaultValue: When the request does not carry the parameter, the current parameter default values.
doControll(@RequestParam(name = "username") String name, @RequestParam(name = "userage") int age)
Path variable @PathVariable
Processor process parameters, can be derived from the parameters carried in the request, from the URI may be a variable, i.e. the path variable.
As in the above normal parameters, if the variable name and the path name which receives the parameter values do not match, to be noted that the path name of the variable parameter.
As shown below, the username note 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; }
Method returns a value processor
@Controller annotation using a processor, the method returns the value commonly used are the following four:
- ModelAndView
- Void
- Object, custom type objects
- String
1, return ModelAndView
After completion handler method, it is necessary to jump to other resources, and the need to transfer data between the resources jump, ModelAndView to return.
public ModelAndView doControll(){ ModelAndView modelAndView = new ModelAndView(); // 传递的数据 modelAndView.addObject("name", "neil"); modelAndView.setViewName("/user.do"); return modelAndView; }
2, return Void
After processing the request, no jumps, can put the processor returns void, e.g. Ajax asynchronous request-response.
If desired jump may be performed by operating the forward or sendRedirect ServletAPI.
3, return Object
Object processor may return the object, this time not appear as a logical view, but directly in the page data for display.
Object Returns objects, annotation requires @ResponseBody, the JSON response data converted into weight.
Front-end data acquisition
FR.ajax({ url: "hello.do", complete: function(data) { alert(data.name + " " + data.age); } })
Similarly, you can return a collection 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; }
Front-end data acquisition
FR.ajax({ url: "hello.do", complete: function(data) { $(data).each(function(index)) { alert(data[index].name + data[index].age); } } })
4, return String
Returns a string There are three possible scenarios:
- Logical view name
- Redirect Redirect
- Forward Forwarding
Logical view name
Processor returns the string may be specified logical view names, by which the view parser address into a physical view.
The final real access path = "prefix" logical view + name + "suffix"
If you do not specify the prefix and suffix, it may be returned directly to the physical view name, such as
return "/WEB-INF/admin/welcome.jsp"
Redirect Redirect
return "redirect:/admin/next.action";
Equivalent response.sendRedirect (), after forwarding address bar of the browser into a forwarding address.
Since the new initiate a request, when forwarding the original parameters can not be passed to the next url,
To pass parameters back splice can url parameter & a = 1 & b = 2
Forward Forwarding
return "forward:/admin/forward.action";
Equivalent request.getRequestDispatcher (). Forward (request, response), after forwarding the browser address bar or the original address.
Forwarding is not performed and the new request and response, but before forwarding the request and a share request and response.
Parameters can be directly reused before forwarding.
That's all for this article, we want to be helpful to learn, I hope you will support script home.
Documents from: https://www.jb51.net/article/136822.htm
You may also be interested in the article:
- Spring MVC framework built configuration and Detailed
- SpringMVC file upload file upload example
- SpringMVC + MyBatis declarative transaction management
- Spring MVC file upload example
- SpringMVC of exemplary simple CRUD (SSM integration)
- SpringMVC return json data in three ways
- springMVC page jump several ways Summary
- SpringMVC interceptor does not intercept the three treatments ways and means of static resources
- Ajax asynchronous implementation in JSON format data Context Request SpringMVC
- Reviewing the Old Spring MVC series of tutorials from scratch