Spring MVC Review Note 02

Source: Internet
Author: User

1. Non-annotated processor mapper and adapter

1.1 Non-annotated processor mapper

Processor Mapper: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

Another mapper: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

Multiple mappers can coexist, and the front-end controller determines which mapper the URL can map to, letting the correct mapper handle it.

1.2 Non-annotated processor adapters

Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter requires written handler to implement the Controller interface.

Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter requires written handler implementation Httprequesthandler interface.

You can use this method to set the data format for the response, such as in response to JSON data, by modifying the response

/*

Response.setcharacterencoding ("UTF-8");

Response.setcontenttype ("application/json; CharSet=UTF-8 ");

Response.getwriter (). Write ("json string"); */

2. dispatcherserlvet.properties

The front-end controller loads the processing mapper, adapter, view resolver, and other components from the top file, using the default load if not configured in Springmvc.xml.

3. Processor Mapper and adapter for annotations

Use the org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping annotation mapper before spring3.1.

Use the org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping annotation mapper after spring3.1.

Use the Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter annotation adapter before spring3.1.

Use the Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter annotation adapter after spring3.1.

3.1 Configure the annotation mapper and adapter.

<!-- Use Mvc:annotation-driven instead of the top annotation mapper and annotation adapter configuration Mvc:annotation-driven a lot of parameter binding methods are loaded by default,

For example, the JSON conversion parser is loaded by default, if MVC is used: Annotation-driven do not configure the upper requestmappinghandlermapping and Requestmappinghandleradapter

Actual development using Mvc:annotation-driven--

<!--<mvc:annotation-driven></mvc:annotation-driven>--

3.2 Development Notes Handler

Adapters that use annotations for mappers and annotations. (The annotated mapper and annotations must be paired with the adapter)

//use a controller to identify that it is a director@Controller Public classItemsController3 {//Product Enquiry List//@RequestMapping Implement mapping of Queryitems methods and URLs, one method corresponds to a URL//The general recommendation is to write URLs and methods as@RequestMapping ("/queryitems")     PublicModelandview Queryitems ()throwsexception{//Call Service Lookup database, query commodity list, use static data simulation hereList<items> itemslist =NewArraylist<items>(); //populating the list with static dataItems items_1=NewItems (); Items_1.setname ("Lenovo Notebook");        Items_1.setprice (6000F); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2=NewItems (); Items_2.setname ("Apple Phone");        Items_2.setprice (5000F); Items_2.setdetail ("Iphone6 Apple phone!" ");        Itemslist.add (Items_1);                Itemslist.add (items_2); //back to ModelandviewModelandview Modelandview =NewModelandview (); //equivalent to the request of Setattribut, in the JSP page through itemslist fetching dataModelandview.addobject ("Itemslist", itemslist); //Specify a ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); returnModelandview; }
View Code

3.3 Loading Handler in the Spring container

<!-- The handler for annotations can be individually configured    in real-world development using     component Scanning- --    class= " Cn.itcast.ssm.controller.ItemsController3 "/>-    <!-- can scan controller, service 、...    Here let's scan controller, specify controller's     package--    <context:component-scan base-packages = " Cn.itcast.ssm.controller "></context:component-scan>
View Code

4. Source Code Analysis (Learn)

Through the front-end controller source code Analysis SPRINGMVC implementation process.

First step: Front controller receives request

Call Dodiapatch

Step Two: The front controller calls the processor mapper to find the Handler

Step three: Call the processor adapter to execute handler and get the result Modelandview

Fourth step: View rendering, populating the model data into the request domain.

View resolution, get view:

Call the Render method of view to populate the request domain with the model data

Rendering method:

5. Summary of entry procedures

Understand the SPRINGMVC front-end controller, processor Mapper, processor adapter , and view resolver usage through the Getting Started program.

Front-End controller configuration:

The first type: *.action, accessed with. Action ends resolved by Dispatcherservlet

The second:/, so the address of the access is resolved by Dispatcherservlet, the resolution of the static file needs to be configured not to allow Dispatcherservlet to parse using this way can implement restful style URL

Processor Mapper:

Non-annotated processor mapper (learn)

Processor Mapper for annotations (master)

Maps A method that identifies a @requestmapping in the markup @controller class . define the URL of the map insidethe @requestmapping. The mapper using annotations does not have to configure The mapping of URLs and handler in XML .

Processor adapter:

Non-annotated processor adapter (learn)

Processor Adapter for annotations (master)

The processor mapper for the note processor adapter and annotations is paired with. Understand that you cannot use a non-annotation mapper for mapping.

<mvc:annotation-driven></mvc:annotation-driven> can replace the configuration below:

<!--annotation Mapper--

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/ >

<!--Note Adapter--

<bean class="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/ >

Actual development use: Mvc:annotation-driven

View parser configuration prefix and suffix:

No prefixes and suffixes are specified in the program:

6. Springmvc and MyBatis Integration

6.1 Requirements

Complete the Product List query using SPRINGMVC and MyBatis.

6.2 Integration Ideas

Springmvc+mybaits System Architecture:

First step: Integrate the DAO layer

MyBatis and spring are integrated to manage the Mapper interface through spring.

Use Mapper's scanner to automatically scan the Mapper interface for registration in spring.

Step Two: Integrate service tiers

Manage service interfaces through spring.

Configure the service interface in the spring configuration file by using the configuration method.

Implement transaction control.

Step Three: Integrate SPRINGMVC

Since SPRINGMVC is a spring module, no consolidation is required.

6.3 Preparation Environment

Database environment: mysql5.1

Java environment: jdk1.7.0_72 Eclipse Indigo

SPRINGMVC version: spring3.2

Required JAR Package: Database driver package: mysql5.1 MyBatis jar Package MyBatis and Spring Consolidation Package LOG4J package DBCP database connection Pool package Spring 3.2 All Jar pack JSTL packages

Engineering Structure:

6.4 Integrated DAO

MyBatis and spring are integrated.

6.4.1 Sqlmapconfig.xml

MyBatis your own configuration file.

6.4.2 Applicationcontext-dao.xml

Configuration:

Data Source: Sqlsessionfactory

Mapper Scanner

6.4.3 Reverse engineering to generate PO class and mapper (single table additions and deletions)

Copies the generated files to the project.

6.4.4 manually define commodity query mapper

For the comprehensive query Mapper, the general situation will have associated queries, the proposed custom mapper

6.4.4.1 Itemsmappercustom.xml

SQL statement: SELECT * from items WHERE items.name like '% notebook% '

6.4.4.2 Itemsmappercustom.java

6.5 Integrated Service

Let spring manage the service interface.

6.5.1 Defining the Service interface

6.5.2 Configuring Service (Applicationcontext-service.xml) in spring container

Create a Applicationcontext-service.xml, configure the service in the file.

6.5.3 transaction Control (Applicationcontext-transaction.xml)

Use the Spring declarative transaction control method in Applicationcontext-transaction.xml.

6.6 Integrated SPRINGMVC

6.6.1 Springmvc.xml

Create a Springmvc.xml file, configure the Processor mapper, adapter, and view resolver.

<!--Controller, service 、... can be scanned Here, let's scan the controller, specify the controller's package.-<context:component-scan base- Package= "Cn.itcast.ssm.controller" ></context:component-scan> <!--Note Mapper--<!--<beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>--<!--note adapter-- > <!--<beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-<!--using mvc:a nnotation-driven instead of the top annotation mapper and annotation adapter configuration mvc:annotation-driven The default load of many parameter binding methods, such as the JSON conversion parser is loaded by default, if you use Mvc:annotation-driven do not configure the upper requestmappinghandlermapping and Requestmappinghandleradapter are actually developed using Mvc:annotation-driven-<mvc:annotation-driven></mvc:annotation-driven> <!--The view resolver resolves the JSP, using the JSTL tag by default, and the Jstl package under Classpath-<Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--Configure prefix-to-<prope for JSP paths Rty name= "prefix" value= "/web-inf/jsp/"/> <!--Configure the suffix of the JSP path--<property name= "suffix" value= ". js P "/> </bean>
View Code6.6.2 Configuring the front-end controller

Refer to the Getting Started program.

6.6.3 Writing controller (that is, handler)

6.6.4 Writing JSPs

6.7 Loading the Spring container

Load the mapper, service, and controller into the spring container.

It is recommended to use a wildcard multibyte to load the configuration file on top.

In Web. XML, add the spring container listener to load the spring container.

Spring MVC Review Note 02

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.