Spring3.X @MVC - (2) Annotation function of So Easy

Foreword:

1. There are a total of 10 sections, that is, 10 blogs to talk about Spring's MVC, covering almost all the content in Spring MVC.

 

2. The example I created is a stadium reservation system. The example I have tested and debugged is a Maven project, including a Parent project: wsheng-spring-base and a sub-Module: wsheng-spring-mvc.

 

3. You can directly import the maven project in Eclipse, and the two projects will be introduced at the same time.

 

4. If you are impatient, you don't have to study further, because there are many examples on the Internet, but they are all about Spring MVC, and you can get started quickly, but if you want to really understand many details in Spring MVC , you can slowly read the blog (from the first section to the tenth section), if you have any questions, please let me know.

 

5. The way to learn is that you can first import the source code into eclipse, and then according to the content on the blog, compare the source code, and slowly digest it. This is a long process, but it will help you understand many details of Spring MVC.

===================================================================================

 

 

On  the basis of Spring3.X @MVC - (1) Important configuration file http://josh-persistence.iteye.com/blog/1873138 , then discuss the reference of Spring MVC. 

 

6) Activate Spring MVC annotation scanning

    a: <context:component-scan> start Spring's component scan function

    b:<context:component-sacn> scans Java classes or methods with @Controller, @RequestMapping.

Note: @RequestMapping is divided into class level and method level. You can register one separately in court-servlet.xml

DefaultAnnotationHandlerMapping and an instance of AnnotationMethodHanlderAdapter.

 

7) Use the annotation @Controller to create a SpringMVC controller and use @RequestMapping for URL routing.

     Annotation-based classes can be arbitrary classes without implementing ad hoc interfaces or extending special parent classes. Annotate such a class with @Controller. The @RequestMapping annotation can be applied at the class level or at the method level. The first mapping strategy is to map a special URL pattern to a controller class, and then map specific HTTP methods to methods in each handler.

@Controller

@RequestMapping("/welcome")

public class WelcomeController {

    @RequestMapping(method = RequestMethod.GET)

    public String welcome(Model model) {

        Date today = new Date();

model.addAttribute("today",today);

        return "welcome";

    }

}

The above code intuitively shows that the data today that needs to be displayed on the View layer is added to the Model layer. The @RequestMapping(method = RequestMethod.GET) annotation is used to decorate the welcome method with the controller's default HTTP GET handler. It is worth noting that if no default HTTP GET handler method is declared, a SeveltException will be thrown. So Spring MVC controllers can at least function as a method for URL routing and default HTTP GET handlers.

 

8) The @Autowired annotation makes it unnecessary to inject properties using XML files.

<bean class="A">

      <property name="b" ref="b"/>

</bean>

 

9) Create a JSP view

Spring MVC supports a variety of views for different presentation technologies, including: JSPs, HTML, PDF, Excel (XLS), XML, JSON, Atom and RSS feeds, JasperReports and other third-party view implementations.

 

10) Mapping requests with @RequestMapping:

When the DispatcherServlet receives a web request, it tries to send the request to a different controller class declared with the @Controller annotation. This scheduling process will scan for methods annotated with @RequestMapping.

a: Map multiple URLs to the same method:

@RequestMapping(value={"/member/remove","/member/delete"}, method=RequestMethod.GET)

public String removeMember(@RequestParam("memberName") String memberName){  }

 

b: Mapping class, @RequestMapping annotation supports the use of wildcards (*)

@Controller

@RequestMapping("/member/*")

public class MemberController  {

 

@RequestMapping("display/{user}")

public String removeMember(@PathVariable("user") String user) {

...

}

 

@RequestMapping

public void memberList() {

1. The method is annotated with @RequestMapping, but the URL value is missing. Because the /member/* URL wildcard is used at the class level, this method can be seen as

Almighty method execution. Any URL request (eg /member/abcdefg or /member/randomroute) will trigger this method.

 

2. The return value of void, which makes the handler method default to the view of the same name, which is memberList.

}

 

}

 

11) Map 8 HTTP types

By default, the @RequestMapping annotation assumes that all requests are of type HTTP GET, which is the most common case in web applications. If you need to specify the POST type, you can use

@RequestMapping(method=RequestMethod.POST)或者

@RequestMapping(value="processUser" method= RequestMethod.POST)

Attachment: There are 8 types of HTTP request types: HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT.

 

After reading the above introduction, you may ask, where are the URL extensions .HTML and .JSP?

          You may have noticed that none of the URLs specified in the @RequestMapping annotation have file extensions such as .html or .jsp. This is good practice consistent with MVC design, but not widely adopted.

         Controllers should not be associated with any type of extension that represents a view technology ( such as HTML or JSP ). That's why the controller returns the logical view and declares that the matching URL should have no extension.

        Today, applications often have to provide the same content in different formats such as XML, JSON, PDF or XLS (Excel). Checking the extension provided in the request (if any) and determining which view technology to use should be left to the view resolver.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326984237&siteId=291194637