Spring MVC Basic Configuration @controller and @requestmapping explanation

Source: Internet
Author: User

One: Configure Web. xml

1) Problem: There are multiple configuration files in the spring project Mvc.xml Dao.xml

2) Resolution: in Web. xml

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/xxx/*.xml</param-value>

XXX represents an XML file path *.xml represents any file with a suffix named xml

</init-param>

3) You can use commas to separate

II: Controller

In 1:spring MVC, controller is considered to be the C-control layer in MVC.

2: Canonical named Class name Xxxcontroller

3: If not based on annotations: This class needs to inherit Commandcontroller see Spring Help

If based on annotations: Add @controller before the class name

4: Add: Precede the class name with this annotation, when spring starts or the Web service starts spring will automatically scan all packages (of course, this can be set)

Function: To tell the server that this class is a CX in MVC this class can receive user requests to process user requests

Three: Question: How to receive user requests

1: Assume that the user request URL is: http://localhost:8080/ project name/xxx.do do not do all the same regardless of this do you can use anything

2: At this point, Spring begins to find the corresponding method of processing the request in all C

3: In any class that can be C, in front of the method definition that you want to process this request

@RequestMapping ("/xxx") this time xxx is the user's request for XXX

Public String Method Name ()

{

Return "BBB";//Returns a string that is used to determine the V-view layer of MVC to be displayed

}

Four: The above is configured for MVC C that is, the control layer returns a string in the method used to determine the V-view layer of MVC

Question: How to get Spring to scan classes to build associations

1: The assumption in the configuration file (Spring) is Mvc.xml

2: Join <context:component-scan base-package= "package path"/>

Five: How the problem returns to the view layer V

1: In this case, you need to add a view parser to the configuration file.

2: Use JSP-based view here

3: Add bean Configuration in Spring-mvc.xml

<bean class= "Org.s...f....web.servlet.view.internalresourceviewresolver" >

<property name= "prefix" value= "/web-inf/views"/> Prefix

<property name= "suffix" value= ". jsp"/> suffix

</bean>

Through the above 5 steps, basically complete the configuration of C V

1:spring C in general from the class name Xxxcontroller in this class need to add @controllerx flag This class can be used as the request processing class is the control class

2: This control class can have a lot of methods, which method is used to process the user request, just before that method to add @RequestMapping ("/xxxxx Request Path")

3: When the request is processed, the return value determines that the user will jump to that page after the processing is completed.

4: For example

@RequestMapping ("/xxx") this time XXX is the front-end request of XXX

Public String Method Name ()

{

Return "BBB";//Returns a string that is used to determine which of the V-View layers to display for MVC

}

Return BBB prefix is added based on the view processor configuration in the configuration file, suffix

Page will jump to/web-inf/views/aaa.jsp

Spring MVC Basic Configuration @controller and @requestmapping explanation

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.