SpringMVC (1) -- getting started with springmvc
SpringMVC schematic diagram:
Steps:
Component introduction:
- Front-end controller (DispatcherServlet): receives the request and responds to the result.
-
HandlerMapping: searches for handler Based on the url.
- Handler: (this is written by ourselves ).
-
HandlerAdapter: we need to follow this rule.
-
ViewResolver: parses the logical view into a positive view. Real view = prefix + logical view name + suffix
-
View: You must develop the View by yourself.
1. annotation-based HelloWorld
1. Import the jar package
2. configure a front-end controller (DispatcherServlet) in the web. xml file)
Web. xml
1 <servlet> 2 <servlet-name> dispatcher </servlet-name> 3 <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> 4 <! -- 5 default configuration file address:/WEB-INF/servlet name-servlet. xml. If you need to specify 6 <init-param> 7 <param-name> contextConfigLocation </param-name> 8 <param-value> classpath: file name </param-value> 9 </init-param> 10 --> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name> dispatcher </servlet-name> 14 <! -- 15 First :*. html ,*. action is to filter. 16 At the end of xxx. Second:/all the addresses will enter this for parsing. For static resources, you need to add the configuration 17. Third:/* He is abnormal and even the jsp page comes in, the error "XXXXX" is returned. Do not use 18 --> 19 <url-pattern>/</url-pattern> 20 </servlet-mapping>.
3. Configure HandlerMapping, HandlerAdapter, and ViewResolver)
Dispatcher-servlet.xml
1 <! -- Processor --> 2 <bean class = "org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping"> </bean> 3 <! -- Processor adapter --> 4 <bean class = "org. springframework. web. servlet. mvc. SimpleControllerHandlerAdapter"> </bean> 5 <! -- Configure ViewResolver --> 6 <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> 7 <property name =" prefix "value ="/WEB-INF/jsp/"> </property> 8 <property name =" suffix "value = ". jsp "> </property> 9 </bean>
In fact, the HandlerAdapter and HandlerMapping mentioned above do not need to be configured here, because SpringMVC will load these configurations by default.
Use the default annotation DRIVER: <mvc: annotation-driven/>. The default annotation driver contains many parameter binding methods.
Using annotations, we cannot scan Handler one by one, so we use component scanning:
<Context: component-scan base-package ="Com. test"> </Context: component-scan>
HelloWorld. java
1 package com. test. controller; 2 3 import java. util. hashMap; 4 import java. util. map; 5 6 import org. springframework. stereotype. controller; 7 import org. springframework. web. bind. annotation. requestMapping; 8 import org. springframework. web. bind. annotation. requestMethod; 9 import org. springframework. web. servlet. modelAndView; 10 11 @ Controller12 @ RequestMapping (value = "/hello") 13 public class HelloWorld {14 @ RequestMapping (value = "/hello ") // The access path is 15 public ModelAndView hello () {16 Map <String, String> map = new HashMap <String, String> (); 17 map. put ("name", "silly"); 18 ModelAndView mv = new ModelAndView ("hello", map); 19 return mv; 20} 21 22}
Now, the basic configuration is complete.
Next we will introduce several basic Annotations:
- Narrow ing requests (you can define the root path on the class and the sub-path on the method). At this time, the path you access is: root path + sub-path.
- Display the http Request method: @ requestMapping (value = "/user", method = "{RequestMethod. POST }"). Error message after restriction:
2. @ Controller
Methods return values:
-
- Return ModelAndView: return a logical view name and data in the request.
- Return String:
- Return void: (The usage is the same as that of servlet)
1 public void test_void(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {2 request.getParameter("");3 request.getRequestDispatcher("").forward(request, response);4 }