[2] Spring MVC Learning Notes

Source: Internet
Author: User

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

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.