Section 1 Coordinates of SpringMVC in knowledge system

Before we learned about frameworks, a combination of technologies like JavaSE+JDBC+JavaWeb+SQL could implement any business logic. Why learn a framework like SSM?

Quite simply, MyBatis encapsulates JDBC and SpringMVC encapsulates JavaWeb. JavaWeb and SpringMVC provide solutions for the presentation layer (or presentation layer), while JDBC and MyBatis are solutions for the persistence layer. So where is Spring? The IOC container in Spring manages components for our entire project, and the various beans, aspect classes, transaction notifications, and so on that we configure are components that work in the container. Our tasks as developers are really two things: developing components and assembling components.



The second section compares the code before and after using SpringMVC

Get the request parameters using the native ServletAPI:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String userName = request.getParameter("userName");
    System.out.println("userName="+userName);
}
Copy the code

Get the request parameters using SpringMVC:

@RequestMapping("/user/login")
public String login(@RequestParam("userName") String userName){
    System.out.println("userName="+userName);
    return "result";
}
Copy the code

This is just one example of how using SpringMVC really simplifies presentation layer development.

This HelloWorld gives you a taste of the overall flow of SpringMVC. The first step is to create the DynamicWebProject and the second step is to import the jar package

Commons logging - 1.1.3. Jar spring aop -- 4.0.0. The jar spring - beans - 4.0.0. The jar spring - the context - 4.0.0. The jar Spring - the core - 4.0.0. RELEASE. Jar spring - expression - 4.0.0. The jar spring - web - 4.0.0. The jar Spring - webmvc - 4.0.0. RELEASE. The jarCopy the code

The third step is to configure SpringMVC’s front-end controller, DispatcherServlet, in web.xml

<servlet>
	<servlet-name>DispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>DispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>
Copy the code

Step 4 Create the Spring configuration file spring-mvC.xml in the SRC directory and specify the package to be scanned automatically

<context:component-scan base-package="com.mvc.controllers"/>
Copy the code

Note: The name of the Spring configuration file must be the same as classpath: in web.xml, otherwise it will not be found. Step 5 Create the HelloHandler class under the auto-scanned package

import org.springframework.stereotype.Controller;
@Controller
public class HelloHandler {

}
Copy the code

Step 6 Declare a method in the HelloHandler class to handle the request

@requestMapping ("/hello") Public String hello() {system.out.println ("SpringMVC handled this request! ); return "/WEB-INF/pages/result.jsp"; }Copy the code



Step 9 Visit the test

Console output:

SpringMVC handles this request!



Take a look at other similar projects and get a sense of the overall flow of using SpringMVC

Why is “/” instead of “/*” in urL-pattern? SpringMVC supports two styles in url-pattern configuration: REST style:

/


*. Action

Paths starting with “/” are considered REST style, and “/*” is not written in the REST style specification. The only case where there is a mapping “/” is here in SpringMVC, and everything else is as usual.