Third lesson: Configuring SPRINGMVC using annotations

Source: Internet
Author: User

Part I: Program structure

Part II: Configuration and Testing

1. Configure the Scan package

      <!---      <base-package = " Cn.shxy.web.controller "/>

2. Enable annotations

      <!---      <class = " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/>       <class= " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/>

3. Write the request code

 PackageCn.shxy.web.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.servlet.ModelAndView;/*** Controller class *@authorJohn **/@Controller Public classAnnotationcontroller {/*** Request Path Configuration *@return     */@RequestMapping (Value= "/user/hello", method =requestmethod.get) PublicModelandview Hello () {return NewModelandview ("/index", "msg", "Zhang San"); } @RequestMapping (Value= "/test/index", method =requestmethod.get) PublicModelandview Index () {return NewModelandview ("/index", "MSG", "John Doe"); }}

4. Testing

Open Browser, browse path: Http://localhost:8080/mvc1/test/index

Part III: annotation optimization

1. Enable annotation optimization

      <!---      < />

2, @RequestMapping optimization

@RequestMapping ("/test/hello")    public  String Hello (httpservletrequest request) {        Request.setattribute ("msg", "Zhang San");         return "/index";    }

3. Request Path Optimization

 PackageCn.shxy.web.controller;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;/*** Controller class *@authorJohn **/@Controller @requestmapping ("/USER") Public classAnnotationcontroller {/*** Request Path Configuration *@return     */@RequestMapping ("/test/hello")     PublicString Hello (httpservletrequest request) {Request.setattribute ("MSG", "Zhang San"); return"/index"; } @RequestMapping ("/gxt/index")     PublicString Index (httpservletrequest request) {Request.setattribute ("MSG", "Zhang San"); return"/index"; }}

Third lesson: Configuring SPRINGMVC using annotations

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.