The difference between the XML configuration of SPRINGMVC and the example of annotation configuration

Source: Internet
Author: User

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

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.