Dabio together to learn the third time of-SPRINGMVC (annotated use detailed)

Source: Internet
Author: User

Briefly:in the previous article, some concepts of adapters and mappers were introduced, this article mainly introducesUse of SPRINGMVC annotations, let's start with a simple annotation program and slowly introduce some common annotations (@Controller, @Component, @Service, @Repository, @RequestMapping, @InitBinder, @ Requestparam, @PathVariable, @RequestBody, @ResponseBody)。 First annotated item 1. Create a project, add a jar package, write Web. Xml to add the jar packages given in the first article, the project structure is as follows:

Web. XML is written as follows, which has already been introduced, and is directly mapped here.

2. Write Springmvc-servlet.xml join annotation support
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http Www.springframework.org/schema/tx/spring-tx-3.1.xsd "><!--0. Specify scan @Controller, @Component, @Service, @ Paths for beans such as repository--><context:component-scan base-package= "Com.billstudy.springmvc"/> <!--1. Annotation mapper--><bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/><!--2. Annotation adapter-- <bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- Tip: The above 1, 2 can be directly used <mvc:annotation-driven/&gt, this configuration already contains the above two bean configuration, so can also implement the effect of annotations-<!--3. The final path of the View parser is: prefix + Controller returns logical view name + suffix such as: method returns/hello, then the actual path is:/web-inf/jsp/hello.jsp--><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/web-inf /jsp "/><property name=" suffix "value=". JSP "/></bean></beans>
3. Writing Controller Classes
Package Com.billstudy.springmvc.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;/** * Springmvc/spring Enterprise Development Common Annotations use demo * @author Bill * @since V1.0 2015/01/23 *//** marked as controller class, can be scanned by Spring, generally such annotations are used on the class, and this phase Like: * @Service: Generally used in the MVC design of the M (model) layer, that is, the business layer * @Repository: Generally used for the DAO layer, that is, the data access layer * @Component: Only a component (Bean), compared to the generalization, when we Write a class not good to locate it in the MVC layer, you can use this * @Controller: generally used in the MVC design of the C (Controller) layer, that is, the control layer * **/@Controllerpublic class annotationdemocontroller{@RequestMapping ("/annotationtest01")/** define access rules **/public Modelandview annotationTest01 ( HttpServletRequest request,httpservletresponse response) {Modelandview result = new Modelandview (); result.setviewname ("/annotationtest01");//The viewname here, we think of it as the logical view name, eventually combined with the view parser, the path is:/web-inf/jsp/ AnnotationTest01.jspresult.addObject ("msg", "noteThe solution was used successfully! "); return result;}}

4. Deployment, testing
Look at the time is a little late, first to this, roughly write to 11.30 should also be written, most of the following comments tomorrow after work to write ...

Dabio together to learn the third time of-SPRINGMVC (annotated use detailed)

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.