Spring3 MVC Annotations (i)---annotations basic configuration and @controller and @RequestMapping common explanations

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: the class needs to inherit Commandcontroller or many others 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 MVC C 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 request XXX regardless of do

Public String Method Name ()

{

Return "AAA";

Returns a string that is useful to note which is the V-view layer used to determine which MVC to display

}

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: FIX: At this point, you need to include the view parser in the configuration file, there are many kinds of, too many

2: Here with Jsp/jstl based view

3: Add bean Configuration in Web. 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>

Summary: 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 @controller 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; The return value after the request has been processed determines that the user will jump to that page after the processing is complete this is important

4: For example

@RequestMapping ("/xxx") this time xxx is the user request XXX regardless of do

Public String Method Name ()

{

Return "AAA";

Returns a string that is useful to note which is the V-view layer used to determine which MVC to display

}

Return AAA prefix suffix based on the view processor configuration in the configuration file

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

Spring3 MVC Annotations (i)---annotations basic configuration and @controller and @RequestMapping Common interpretation (RPM)

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.