Spring MVC getting started learning notes

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.