1. When importing Jar packages, add Spring-aop-4.2.2.release.jar on the XML configuration (required for Annotations)
2. When writing a controller, the annotation need to do the relevant configuration is the red part, and XML is to implement the Controller interface
(a) when the annotation is configured
Package Com.spring.hello;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.servlet.modelandview;import org.springframework.stereotype.Controller; @Controllerpublic class Hellocontroller { @RequestMapping ("/hello")public Modelandview Hello (httpservletrequest req,httpservletresponse resp) { modelandview mv=new modelandview (); Mv.addobject ("msg", "first annotation"); Mv.setviewname ("Hello"); return mv; }}
(b) when XML is configured
Package Com.spring.hello;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.controller;public class Hellocontroller implements Controller{@Overridepublic Modelandview HandleRequest (httpservletrequest arg0,httpservletresponse arg1) throws Exception {//TODO auto-generated Method Stubmodelandview mv=new Modelandview () mv.addobject ("msg", "first springmvc"); mv.setviewname ("Hello"); return MV ;}}
3. The Springmvc file annotation does not require
<!-- configuration hanldermapping --><bean class= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "></bean><!-- Configuration Handleradapter--><bean class= "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" >< /bean>
But to take
<!-- configuration request and Processor--><bean name= "/hello.do" class= "com.spring.hello.HelloController" ></bean >
Modified into
<!-- configuration request and Processor--><context:component-scan base-package= "com.spring.hello" ></context: Component-scan>
The difference between the XML configuration of SPRINGMVC and the example of annotation configuration