SPRINGMVC Quick Start
SPRINGMVC Introduction
SPRINGMVC is the Lightweight web framework for the request-driven type of the Web MVC design Pattern that spring provides for the presentation layer, and it functions like Struts2. But more convenient and more efficient than Struts2. is the current mainstream web framework.
HelloWorld
Simulate a simple business logic, page request after the jump to the HelloWorld page.
First, create a SPRINGMVC maven project. (If you do not create a Maven project, refer to: Eclipse+maven Create WebApp Project)
The project structure is as follows:
First Step: Configure Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.springmvc</groupId> <artifactId> Springmvc</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging > <properties> <spring.version>4.1.3.RELEASE</spring.version> </properties> & lt;dependencies> <!--spring begin--> <dependency> <groupid>org.springframework& Lt;/groupid> <artifactId>spring-webmvc</artifactId> <version>${spring.version}< ;/version> </dependency> <dependency> <groupid>org.springframework</group Id> <artifactId>spring-context</artifactId>
<version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> & lt;version>${spring.version}</version> </dependency> <dependency> <groupi D>org.springframework</groupid> <artifactId>spring-core</artifactId> <version&
Gt;${spring.version}</version> </dependency> <!--spring end--> </dependencies> </project>Step two: Because this is a Web project, we need to configure its Web.xml file, and configure Dispatcherservlet like a servlet. Intercepts all requests, but doing so intercepts the static resources, which are described later in this article.
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>springmvc</ display-name> <!--configuration Dispatcherservlet--> <servlet> <servlet-name>dispatcher</servlet-name > <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--designation The location and name of the SPRINGMVC configuration file are SPRINGMVC the format and location of the configuration file name:/web-inf/servlet-name + "-servlet.xml" can omit the following Init-param code. The servlet-name here is dispatcher. namely/web-inf/dispatcher-servlet.xml--> <load-on-startup>1</load-on-startup> </servlet> < Servlet-mapping> <servlet-name>dispatcher</servlet-name> <!--servlet Knowledge Point: Process all requests--> <URL-PA
Ttern>/</url-pattern> </servlet-mapping></web-app>
Step Three: (
Core Knowledge pointsTo create a dispatcher-servlet.xml file that can be customized or conform to the spring rules (as noted in the second step). First configure the automatic scan package so that the annotation for the configuration bean takes effect. The MVC annotation driver is then configured so that SPRINGMVC annotations take effect. Finally, configure the view parser to stitch the return value of the method to the URL path of the jump.
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" 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-4.0.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!--Configure the Automatically scanned package--> <context: Component-scan base-package= "Com.itdragon.springmvc"/> <!--configuration View parser--> <bean class= "org.springframework . web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf/views/"></" property> <property name= "suffix" value= ". jsp" ></property> </bean> <! --Configuration annotation driver--> <mvc:annotation-driven/> </beans>
Step Fourth: Business logic code, which simply prints and returns a HelloWorld string. Let the view parser be configured as a Jump helloworld.jsp page.
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
@Controller public
class Helloworldcontroller {
/**
* Browser Access Http://localhost:8080/springmvc/helloworld
* The corresponding method is found by matching the URL of the mapping request with the @requestmapping annotation.
* the "HelloWorld" string is returned after the method is executed. Through the View parser resolution mechanism (prefix + returnval + suffix)
* Request to return to the jump page/web-inf/views/helloworld.jsp * * *
@RequestMapping ("/ HelloWorld ") Public
String HelloWorld () {
System.out.println (" ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ HelloWorld ");
return "HelloWorld";
}
Everything is ready, Project package compiled, start Tomcat. Encounter an exception:
Java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet
says that the Dispatcherservlet class was not found, but the jar package was actually imported. Actually, after creating the MAVEN project, you need to manually add the dependent jar to the classpath. The workaround is as follows:
Project Right-click-->properties-->deployment Assembly-->add-->java Build Path entries--> Import all dependent jar packages, restart Tomcat can.
After you start Tomcat again, there is no problem. Browser access: HTTP://LOCALHOST:8080/SPRINGMVC carriage return.
Console printing:
^^ ^^ ^^ ^^ ^^ ^^ the ^^ HelloWorld
Here, a simple SPRINGMVC project is over.
More dry goods wait for you to get http://www.itit123.cn/