This article will share with you an article on Spring MVC getting started learning notes. I hope this tutorial will be helpful to you.
This article uses simple code to clearly describe the basic steps of Spring MVC development.
Step Description: 1. in the web. configure dispatcherservlet2in xml. Write the WEB-INF file in the xx-servlet.xml (xx represents the name in the <servlet-name> of DispatcherServlet) 3. create a Controller 4. configure controller5in the xx-servlet.xml file. Configure view parser in the xx-servlet.xml file 6. create jsp7. test the page in the browser
Detailed project directory structure steps: 1. Configure DispatcherServlet in web. xml
2. Compile WEB-INF files in the xx-servlet.xml
| The Code is as follows: |
Copy code |
<Servlet> <Servlet-name> web1 </servlet-name> <Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class> <Load-on-startup> 1 </load-on-startup> </Servlet> <Servlet-mapping> <Servlet-name> web1 </servlet-name> <Url-pattern> *. htm </url-pattern> </Servlet-mapping> |
3. Create a Controller)
4. Configure the Controller in the xx-servlet.xml File
| The Code is as follows: |
Copy code |
Public class HomeController implements Controller {
@ Override Public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
Return new ModelAndView ("home", "message", this. greeting ); }
Private String greeting; Public void setGreeting (String greeting ){ This. greeting = greeting; }
} |
5. Configure view parser in xx-servlet.xml files
6. Create a jsp
| The Code is as follows: |
Copy code |
<? 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: p = "http://www.springframework.org/schema/p" Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <! -- Configure the view Parser: automatically maps based on the name --> <Bean name = "viewResolver" Class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> <Property name = "prefix"> <Value>/WEB-INF/jsp/</value> </Property> <Property name = "suffix"> <Value>. jsp </value> </Property> </Bean> <! -- Controller ing file --> <Bean name = "/home.htm" Class = "controller. HomeController"> <Property name = "greeting"> <Value> welcome to Spring MVC </value> </Property> </Bean> <Bean name = "homeController" Class = "controller. HomeController"> <Property name = "greeting"> <Value> welcome to Spring MVC! </Value> </Property> </Bean> </Beans> |
| The Code is as follows: |
Copy code |
<Body> $ {Message} <! -- Message is placed in requestScope --> </Body>
|
7. Test the page in the browser: (Note the url format)