"Spring real third edition of" Seven

"Chapter VII constructed using Spring MVC Web Application"

Spring MVC-based model - achieved Controller (Model-View-Contrlloer, MVC ) pattern - a view
that can help us build as flexible and loosely coupled applications like Spring Web

Spring MVC start

1. Spring tracking request

When users click on a link in a Web browser or when the form is submitted, the request began work
request is a very busy guy, from the beginning to leave the browser to get the response back
it will experience a lot of the station, the station will leave some information but also bring other information

Requests the DispatchServle, also known as front-end controller, the first map to the processor
The processor may map knows that the request should be assigned to which control
after completion of the processing in the controller, the request is a view (a view to a parser) to render the output results

2. set up Spring MVC

Spring MVC is the core DispatchServlet
it acts as a front controller of Spring MVC
DispatchServlet must web.xml Web configuration program
it is required to first <servlet> declaration into web.xml:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet = name> element is more important
default, DispatchServlet loaded when loading from a name based on the Sertlet Spring application context XML file
in this example, Servlet name for the dispatch
Therefore DispatchServlet will try from a file called dispatch-servlet.xml to load the application context
words, DispatchServlet using dispatch-servlet.xml created application context

Next, we must declare what DispatchServlet URL handling

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

Wherein <servlet-mapping> tag statement with corresponding matching rules servlet
each <url-pattern> tag represents a matching rule
where matched * .form is meant any form as a file name suffix

Write basic controller

Each resource in the preparation of this session, we should provide for the application of a separate controller
rather than writing a controller for each use case, we generally use annotations to configure the controller
DispatchServlet need to consult one or more processing mapping to distribute explicitly requests which controller
Spring comes with a map to achieve multiple processors for us to achieve

  1. BeanNameUrlHandlerMapping
    • According to Bean name of the controller to map the controller to the URL
  2. ControllerBeanNameHandlerMapping
    • According to Bean name of the controller to map the controller to the URL, Bean name does not follow URL conventions
  3. DefaultAnnotationHandlerMapping
    • Maps the request to a controller and method using annotated @RequestMapping
  4. SimpleUrlHandlerMapping
    • Spring definitions used in the application context attribute set to the URL to map the controller

Although many implementations provide a mapping for the processor, but still based on the main controller class notes actually used
therefore, DefaultAnnotationHandlerMapping DispatchServlet provided basically meet our needs
DefaultAnnotationHandlerMapping able to map the request to use annotations @RequestMapping the method
but the method implemented Spring MVC annotation driven not only maps the request to the controller
verifies information conversion and build on the controller, we also need to use binding request parameters annotation method parameters to the controller
so Spring also need MVC annotation-driven characteristics provided by
<mvc:annotation-driven/>
the definition of a simple controller

@Controller 
//声明该类为控制器类
public class HelloMVCController {
    //用于建立请求URL和处理方法之间的对应关系,即对hello请求执行sayHello方法
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String sayHello(){
        System.out.println("hello world");
        //返回视图的名称
        return "success";
    }
}

Analytic view

Handle most requests one thing have to do is render output for the user
in this task fell on the view to achieve - usually JSP
in order to determine which view specific request needs to use, DispatchServlet parser looks for a view
based on view resolution is the name of the view to the logical controller returns to the view of the actual conversion of rendering results
actually viewresolver does is to match the view name and JSP

Spring view resolver provides many, but introduces InternalResourceViewResolver view resolver
in Spring MVC, the extensive use of the development model convention over configuration
InternalResourceViewResolver is a convention for the elements
will resolve names to logical view View objects, which object rendering tasks entrusted to the Web application context of a template (JSP)
InternalResourceViewResolver to get a view of the template by adding a specific prefix and suffix logical view name path

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
    <value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
    <value>.jsp</value>
</property>
</bean>

When parsing DispatchServlet claim InternalResourceViewResolver view, it will get the name of a logical view
as the above-described "success", then adding the prefix "/ WEB-INF / pages / " and the suffix ".jsp"
Results obtained / WEB-INF / pages / sucess.jsp JSP path is rendered output

Complete the Spring Application Context

As previously mentioned, it DispatchServlet based on an XML file to load its Spring application context
and the name of the file based on its <servlet-name> property to determine
but if other classes of Bean how to do it? Also declared in the dispatch-servlet.xml file it?
But in fact we do not do more is to organize the Spring configuration file into multiple files
, such as a configuration file for the service layer, one for the persistence layer, a source of data for the class

Based on this concept, the configuration of the Web tier are placed in the dispatch-servlet.xml file is a reasonable
file will be loaded DispatchServlet, but we need another way to load other configuration

This is ContextLoaderListener can play a role in place
ContextLoaderListener is a Servlet listener, in addition to the application context DispatchServlet created, it can load other configuration files
to a Spring application context

To use ContextLoaderListener, you need to add the following <listener> declared in file web.xml

<listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

We must tell ContextLoaderListener which configuration file to load
If not specified, the context loader looks /WEB-INF/applicationContext.xml this Spring configuration file
, but the file itself does not do the application context into multiple fragments, so need to override the default implementation

In order to specify one or more Spring ContextLoaderListener profile to
configure the servlet context parameters contextConfigLocation

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    <!--默认实现-->
    /WEB-INF/applicationContext.xml
    <!--重写默认实现-->
    /WEB-INF/***.xml
    classpath:service-context.xml
    classpath:persistence-context.xml
    classpath:dataSource-context.xml
    </param-value>
</context-param>

contextConfigLocation parameter specifies a path list
, unless otherwise stated, the path is relative to the root directory of the application
here, Spring configuration is divided into a plurality of XML files

Guess you like

Origin www.cnblogs.com/ASE265/p/12499873.html