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