The approach described above is the most basic, the most primitive method
When there are several additions and deletions and other operations, the use is not very convenient, so the development of the use of annotations in the way of development (a URL corresponding to a controller)
Basic steps, not verbose
1. Import a prerequisite package
2. Configure the Web. xml file, and all processing is handled by Dispatcherservlet
3.spring-servlet.xml
Annotated development is used here
(1) It will automatically scan the Com.qzp.web class file below;
(2) The View resolver is responsible for parsing the view, and adding the prefix suffix
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" 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.xsdhttp://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "><mvc:annotation-driven/>< Context:component-scan base-package= "Com.qzp.web"/><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/web-inf /jsp/"/><property name=" suffix "value=". JSP "/></bean></beans>
4.Controller
A few notes (1) @controller declares that it is a controller, (2) @RequestMapping declares which URL this method corresponds to, and (3) The last return is the view name, which together with the prefix and suffix in the View parser determines which page to jump to
Package Com.qzp.web;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class Myfirstcontroller {// requestmapping indicates which URL is used to correspond to @requestmapping ({"/hello", "/"}) public String HelloWorld () { System.out.println (" First spring MVC "); This is the name of the view return "Hello";} A controller can map multiple @RequestMapping ("/hello") public String Welcome () {return "Welcome"; } }
5.hello.jsp located under web-inf/jsp/
<body> Annotation-based spring MVC </body>
When you access the URL value of Hello, by Myfirstcontroller processing, Myfirstcontroller will return hello, by the View parser processing, add prefixes and suffixes, and eventually return the results to the user.
[2] Spring MVC Learning Notes