Initial SpringMVC and springmvc initialization Loading
1. Configure the front-end controller in web. xml
<! -- Register a central processor --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <! -- Set the context parameter name --> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </init-param> <! -- Mark whether the container is instantiated at startup --> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping>
2. Develop a processor
Public class MyController implements Controller {@ Override protected ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView mv = new ModelAndView (); mv. addObject ("Learning", "learning! "); // Logical view name mv. setViewName ("/WEB-INF/jsp/index. jsp "); return mv ;}}
3. Configure SpringMVCxml
<! -- Register the processor --> <bean id = "hello. do" class = "cn. happy. action. MyConverter"> </bean>
View parser
<! -- View parser --> <! -- <Bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> -->
4. jsp
Ii. annotation-based development
1. Use @ Controller and @ RequestMapping () to implement the welcome program.
@ Controllerpublic class MyController {@ RequestMapping (value = "/hello. do ") public String doFirst (HttpServletRequest request, HttpServletResponse response) {ModelAndView mv = new ModelAndView (); mv. addObject ("hello", "SpringMVC! "); Mv. setViewName (" index "); return mv ;}
2. One processing class defines multiple processor Methods
Public class MyMultiActionController {@ RequestMapping (value = "/first. do ") public ModelAndView doFirst (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView mv = new ModelAndView (); mv. addObject ("Learning", "learning! "); Mv. setViewName ("WEB-INF/jsp/index. jsp "); return mv;} @ RequestMapping (value ="/Second. do ") public ModelAndView doSecond (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView mv = new ModelAndView (); mv. addObject ("eat", "Learn! "); Mv. setViewName (" WEB-INF/jsp/index. jsp "); return mv ;}}
Annotation-based namespace Development
<! -- View parser --> <! -- <Bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> -->
3. Usage of wildcards in annotation-based development requests
@ RequestMapping (value = "/second. do ") public String doSecond () {System. out. println ("second =================="); return "index" ;}@ RequestMapping (value = "/* third. do ") // * Indicates 0 or multiple characters. The public String doThird () {return" index ";} is displayed at the end of third ";} @ RequestMapping (value = "/fourth *. do ") // * Indicates 0 or more characters. The public String doFourth () {System. out. println ("fourth =================="); return "index" ;}@ RequestMapping (value = "/**/fiveth. do ") // ** multi-level paths are required between hr and fiveth, or public String doFiveth () {return" index ";} can be accessed without paths ";} @ RequestMapping (value = "/*/sixth *. do ") // * in the middle of hr and sixth, the level-1 path must be written, and the level-1 public String doSixth () {System. out. println ("sixth =================="); return "index ";}}
4. annotation-based development request method definition
@ RequestMapping
RequestMapping is an annotation used to process request address ing and can be used for classes or methods. Used on the class to indicate that all methods in the class that respond to requests use this address as the parent path.
The RequestMapping annotation has six attributes. Next we will divide it into three types for explanation.
1. value, method;
Value: Specifies the actual request address. The specified address can be in URI Template mode (which will be described later );
Method: Specifies the method type of the request, such as GET, POST, PUT, and DELETE;
The value of the method attribute is RequestMethod, which is an enumeration constant. The common values are RequestMethod. GET and RequestMethod. POST.
2. consumes and produces;
Consumes: specifies the Type of Content submitted for request processing, such as application/json, text/html;
Produces: specifies the type of returned content. It is returned only when the (Accept) type in the request header contains the specified type;
3. params, headers;
Params: specifies that the request must contain certain parameter values for processing.
Headers: specifies that the request must contain some specified header values before the method can process the request.
5. Request parameters included in the request
@Controller@RequestMapping("/hr")public class MyController { @RequestMapping(value="/first.do",method=RequestMethod.POST) public String doFirst(String s,Model model) { model.addAttribute("uname", uname()); return "index"; }
Jsp page
// Automatic Parameter Assembly <form action = "$ {pageContext. request. contextPath}/first. do "method =" post "> <input name =" username "/> <br/> <input type =" submit "value =" submit "/> </form>
Correction Request Parameter name the name here is consistent with the attribute value of the page form element name to achieve automatic assembly
@Controllerpublic class MyController { @RequestMapping(value="/first.do",method=RequestMethod.POST) public String doFirst(@RequestParam("uanme")String s,Model model) { System.out.println("uname", uname()); model.addAttribute("uname",uname()); return "index"; }
Object Parameters
@RequestMapping("/hr")public class MyController { @RequestMapping(value="/first.do",method=RequestMethod.POST) public String doFirst(UserInfo info,Model model) { model.addAttribute("uname", info.getUname()); return "index"; }
// Automatic Parameter Assembly <form action = "$ {pageContext. request. contextPath}/first. do "method =" post "> <input name =" username "/> <br/> <input type =" submit "value =" submit "/> </form>