A, springMVC entry
-
SpringMVC Introduction
MVC is the Web application is divided into three, namely:- A controller (Controller): is responsible for receiving and processing the request, the response client;
- Model (Model): data model, the business logic;
- View (View): Presentation Model, interact with the user;
SpringMVC corresponds to a sub-frame of the spring, so the spring can be naturally SpringMVC used in combination without integration.
Access process:
-
SpringMVC core components
Package | name | effect |
---|---|---|
DispatcherServlet | Front Controller | Used to control execution of other components, a unified scheduling, reduce coupling of the various components |
Handler | processor | Complete the specific business logic |
Handler Mapping | Maps the request to Handler | |
HandlerInterceptor | Processor interceptors | It is an interface to intercept processing |
HandlerExecutionChain | Processor execution chain | Including a Handler and a except |
Handler Adapter | Processor Adapter | DispatcherServlet perform different Handler by HandlerAdapter |
ModelAndView | Loading model data and view (logical view) as the processing result information returned to the Handler DispatcherServlet | |
ViewResolver | View resolvers | The logical view resolved into a physical view, and then return the results to the client |
- SpringMVC workflow
- Client requests submitted to DispatcherServlet
- Looking from the one or more HandlerMapping DispatcherServlet controller, to find a processing request Controller
- DispatcherServlet submit requests to Controller
- After Controller call business logic processing returns ModelAndView
- DispatcherServlet looking for one or more ViewResolver view resolver, find the specified view ModelAndView
- View is responsible for displaying the results to the client
Second, to achieve a springMVC
-
Xml-based way to achieve springMVC
-
Creating a Maven project
-
Import dependencies required springMVC
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies>
-
In
web.xml
the configurationDispatcherServlet
<!--配置springMVC的servlet:DispatcherServlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定springMVC文件的路径--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> </servlet> <!--拦截所有的请求--> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
-
Achieve a Handler
/** * 自己去实现Handler * 要继承 Controller 类 */ public class MyHandler implements Controller { @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { //装载模型数据和逻辑视图 ModelAndView modelAndView = new ModelAndView(); //添加模型数据 modelAndView.addObject("name","tom"); //添加逻辑视图,这里由视图解析器将逻辑视图进行解析,所以不需要加前后缀 modelAndView.setViewName("show"); return modelAndView; } }
-
Configuration
SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置一个HandlerMapping,将URL请求映射到--> <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!--配置 /test 请求对应的Handler--> <prop key="/test">testHandler</prop> </props> </property> </bean> <!--配置Handler--> <bean id="testHandler" class="MyHandler"></bean> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
-
Implementation view show.jsp
-
Run the project, access / test
-
-
SpringMVC achieve a comment on the way
-
Add an automated scan the springMVC.xml
<!--将AnnotationHandler自动扫描到IOC容器中--> <context:component-scan base-package="com.springdemo.controller"></context:component-scan>
-
Achieve Handler
@Controller public class AnotationHandler { @RequestMapping("/test") public ModelAndView test(){ //装载模型数据和逻辑视图 ModelAndView modelAndView = new ModelAndView(); //添加模型数据 modelAndView.addObject("name","tom"); //添加逻辑视图,这里由视图解析器将逻辑视图进行解析,所以不需要加前后缀 modelAndView.setViewName("show"); return modelAndView; } }
Other business methods can also be used two ways:
- Use Model by value, String parsed view
@Controller public class AnotationHandler { @RequestMapping("/test") public String test(Model model){ model.addAttribute("name", "Tom"); return "show"; } }
- Use Map by value, String parsed view
@Controller public class AnotationHandler { @RequestMapping("/test") public String test(Map<String,String> map){ map.put("name", "Tom"); return "show"; } }
- Use Model by value, String parsed view
-
Original Address: SpringMVC frame 01 -------- use IDEA to build SpringMVC environment Panya's blog