Bo Master Fresh Graduates, Java professional, but in the school also did not listen to what classes, usually up to half want to go on, usually also play crazy.
Start learning Springmvc today. "Blogging for the first time"
The origin of all Things (HelloWorld) First, create a MAVEN project (familiarize yourself with it)
Then click Finish, then organize the structure of the project,
Second, import SPRINGMVC required package (which is why I use Maven, too lazy to pack)
Load these core packages and the dependent packages will be downloaded automatically.
Third, configuration XML
<?xml Version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "HT Tp://java.sun.com/xml/ns/javaee "xmlns:web=" Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "Xsi:sch emalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" & Gt <display-name>archetype Created Web application</display-name> <!--loading the core of Dispatcherservlet SPRINGMVC Through the filtering request path to the filtering path to Dispatcherservlet processing, through the handlemapping check if there is no corresponding controller, if any, then call the controller. -->
<servlet> <servlet-name>dispatcher</servlet-name> <SERVLET-CLASS>ORG.SPRINGFRAMEWORK.W eb.servlet.dispatcherservlet</servlet-class> <!--custom Select an XML path (default/web-inf/dispatcher-servlet.xml)---& Lt;init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config /spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </SERVL Et> <!--Set the Dispatchservlet pattern, by mapping the Dispatchservlet to/, the default servlet handles all requests, including static resources-and <servlet-mapping& Gt <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </ Servlet-mapping> <!--Character Set filters--<filter> <filter-name>encodingfilter</filter -name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <Param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-par am> <init-param> <param-name>forceEncoding</param-name> <param- value>true</param-value> </init-param> </filter> <filter-mapping> & Lt;filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-m Apping> </web-app>
Iv. Configuration spring-servlet.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmln S:mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" Xsi:schemalo Cation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m Vc.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/conte Xt/spring-context.xsd "default-lazy-init=" true "> <!--Add annotation driver--<mvc:annotation-driven /> <!--Default scanned package path--<context:component-scan base-package= "Com.song"/> <!--Mvc:view -controller can be controll without the needThe ER handles the request, turning to the set view-<!--set up like this, and if the request is/, it is resolved directly to/index.jsp---<mvc:view-cont Roller path= "/" view-name= "index"/> <!--as access home address--<!--view Resolver--<bean class= "Org.springfram Ework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" ContentType "value=" text/html "/> < ;p roperty name= "prefix" value= "/web-inf/views/"/> <property name= "suffix" value= ". JSP"/></bean></ Beans>
Here, Springmvc's view parser is divided into a number of different types, each with its own features, such as Internalresourceviewresolver is a controller that supports JSTL.
V. Preparation of Controller
Package Com.song.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;// The Declaration class is a controller, which is initialized by the spring container scan, or it can use the same function as @component @controller //Change Controller mapping request @requestmapping ("Hello") public class Hellocontroller {//Requestmapping:action@requestmapping on Method ("index") //To handle login request public at foreground Modelandview index () {Modelandview Modelandview = new Modelandview (); Modelandview.addobject ("Message", "helloworld!!"); /setviewname: Specifies the view name Modelandview.setviewname ("index") corresponding to the action, return Modelandview;}
@controller: Declares that the class is a controller, is scanned by the XML that was just configured, and instantiates
@RequestMapping: As you can see, the person who learns struts knows that the access path project name is followed by a hello to access the controller, plus the index before the method to access the method, such as
127.0.0.1:8080/springmvc/hello/index
Modelandview: Model View, the Model view defines the view parser that will be given to Spring-servlet.xml to parse to the "prefix/model view name/suffix" and pass the information stored in the model view into the JSP
VI. The controller writes the view as soon as it is written.
Create a index.jsp under/web-inf/view
The JSP knows to write a ${message} to receive the values saved in the Model view!
"Ok!"
Results:
I hope I write these can urge me to study, but also hope to help some people want to learn SPRINGMVC but not the person. "Not enough, please point out."
The helloworld! of my SPRINGMVC learning path