SPRINGMVC Overview
SPRINGMVC is one of the best MVC frameworks, with a set of MVC annotations that let Pojo be the controller that handles requests without any excuses.
SPRINGMVC supports restful URL requests with a loosely coupled pluggable component structure that is more scalable and flexible than other MVC frameworks.
Use idea to create a SPRINGMVC project with the following project structure:
Helloworldcontroller is a controller that handles requests:
PackageCom.led.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;/** * @authorAlan * @date 2018/5/23 22:07 * @Controller: Indicates that this is a controller * @RequestMapping: The path of the map request * The return value resolves to the actual physical view through the view resolver, for Internalre Sourceviewresolver View parser, will do the following: * through the prefix + returnval + suffix way to get the actual physical view, and then do the forwarding operation */web-inf/views/success.jsp*/@Controller Public classHelloworldcontroller {@RequestMapping ("/helloworld") PublicString HelloWorld () {System.out.println ("Hello World"); return"Success"; }}
Web. XML (the overall configuration file for the project):
<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1"> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/applicationcontext.xml</Param-value> </Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <!--Configure Dispatcherservlet - <servlet> <Servlet-name>Dispatcher</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Dispatcher</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping></Web-app>
Dispatcher-servlet.xml (SPRIMGMVC related configuration file):
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " > <!--Configure automatic scanning of packages - <Context:component-scanBase-package= "Com.led.controller"/> <!--Configure the View resolver to resolve the handler method return value to the actual view - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"value= "/web-inf/views/"/> < Propertyname= "suffix"value= ". jsp"/> </Bean></Beans>
Applicationcontext.xml (Spring's overall configuration file):
<? XML version= "1.0" encoding= "UTF-8" ?> < 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 " ></beans>
success.jsp (View rendering page):
<%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><HTML><Head> <title>Title</title></Head><Body> <h4>Success page</h4></Body></HTML>
Run the project, the browser input http://localhost:8080/springmvc/, click on the hyperlink, will show
Success page
SPRINGMVC operation schematic diagram:
The HelloWorld of SPRINGMVC