The path of Spring MVC development (II.)

Source: Internet
Author: User

Ii. Introduction to the simple configuration of Web. XML 11. When you start a Web project, the container goes back to read the two nodes in the XML config file <context-param> and <listener>2, then the container will create a servletcontext (context) within the scope of the application can use this context3, then the container will read to <context-param> converted to a key value pair, and to ServletContext. 4. Create monitoring through <listener>5, in the listening class there will be a contextinitialized (Servletcontextevent event) initialization method, in this method can be Event.getservletcontext (). Getinitparameter ("Contextconfiglocation") to get the value Context-param set. There must also be a contextdestroyed (Servletcontextevent event) Destruction method in this class. To release resources before closing the app, such as closing the database connection. 6. After you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started yet. This action will be earlier than all servlets. One
<span style= "FONT-SIZE:18PX;" ><context-param>        <param-name>contextConfigLocation</param-name>        <param-value >            classpath:/spring/application-datasource.xml        </param-value></context-param></span ></span>

This element is used to declare application-scoped context initialization parameters (throughout the Web project) such as configuration of the database connection pool, etc.

Param-name Setting the parameter name of the context, the name must be unique

Param-value sets the value of the parameter name to specify the response of the XML file name, if there are multiple files that can be written together with "," delimited

You can use the wildcard form applicationcontext-*.xml to load all the files at the beginning of this class

1, you can directly put it under the/web-inf

2, put under the Classpath Classpath:/spring/application-datasource.xml

Such as:

<span style= "FONT-SIZE:18PX;" ><!--Context Configuration locations for Spring XML files----  <context-param>      <param-name >contextConfigLocation</param-name>      <param-value>/web-inf/applicationcontext-*.xml, classpath*:applicationcontext-*.xml</param-value>  </context-param> </span><span style= " font-size:14px; " ></span></span>


This is used by the Web container to load


Two
<span style= "FONT-SIZE:18PX;" ><listener>        <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class></listener></span></span>

listener tags are used for monitoring, Contextloaderlistener implements the Servletcontextlistener interface, you can listen to all the servlet, session, request and other interfaces. Contextloaderlistener's role is to start the Web container, automatically assemble the ApplicationContext configuration information, it implements the Servletcontextlistener interface, The listener is configured in Web. XML, and when the container is started, it is implemented by default.


servletcontext: Each Web application has a servletcontext associated with it. The ServletContext object is created when the app is launched and destroyed when the app is closed.

ervletcontextlistener: using the Listener interface, developers can add arbitrary objects to ServletContext before serving client requests. This object is initialized when the ServletContext is started, and is visible throughout the ServletContext operation. The interface has two methods as shown below:

Three
<filter>    <filter-name></filter-name>    <filter-class></filter-class></ Filter>
filter execution is performed according to the filter-mapping sequence of the Web. XML Configuration
<span style= "FONT-SIZE:18PX;" >    <filter>        <filter-name>character encoding</filter-name>        <filter-class> org.springframework.web.filter.characterencodingfilter</filter-class>        <init-param>            < param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param >    </filter>    <filter-mapping>        <filter-name>character encoding</filter-name >        <url-pattern>/*</url-pattern>    </filter-mapping></span><span style= " font-size:14px; " ></span>

the above is to prevent garbled
<span style= "FONT-SIZE:18PX;" ><filter>        <filter-name>spring openentitymanagerinviewfilter</filter-name>        < Filter-class>org.springframework.orm.jpa.support.openentitymanagerinviewfilter</filter-class>        <init-param>            <param-name>entityManagerFactoryBeanName</param-name>            <param-value >entityManagerFactory</param-value>        </init-param>    </filter>    < filter-mapping>        <filter-name>spring openentitymanagerinviewfilter</filter-name>        < url-pattern>/*</url-pattern>    </filter-mapping></span><span style= "FONT-SIZE:14PX;" ></span>

Using hibernate in a Java Web project often encounters Lazyinitializationexception. This is because the controller and the model layer (Java code) are going through some of the JPA-enabled domains (such as the Getrefrence () method or the use of fetch= in an association relationship) Fetchtype.lazy) is returned to the view layer (JSP code), due to the loading of the domain object's JPA session has been closed, resulting in these lazy loading data access exceptions.

You can then use Openentitymanagerinviewfilter to bind a jpasession to the line threads the complete request process.

If the openentitymanagerinviewfilter,session is not used, it will be closed after the Service.find () method, after which the session is closed after the entire view layer has ended.

Four
<span style= "FONT-SIZE:18PX;" ><servlet>        <servlet-name>mvc-dispatcher</servlet-name>        <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class>        <load-on-startup>1</ Load-on-startup>    </servlet><servlet-mapping><servlet-name>mvc-dispatcher</ Servlet-name><url-pattern>/</url-pattern></servlet-mapping></span><span style= " font-size:14px; " ></span>

Dispatcherservlet is the implementation of the front-end controller design pattern, providing a centralized access point for spring WEB MVC, responsible for assigning responsibilities, and seamlessly integrating with the spring IOC container to get all the benefits of spring. Please refer to Figure 2-1 in chapter two for details.

Dispatcherservlet is mainly used for responsibility scheduling, itself mainly for the control process, the main responsibilities are as follows:

1, File upload parsing, if the request type is multipart will be through the Multipartresolver file upload parsing;

2, through the handlermapping, map the request to the processor (return a handlerexecutionchain, which includes a processor, multiple handlerinterceptor interceptors);

3. Support multiple types of processors (processors in handlerexecutionchain) through Handleradapter;

4, through the Viewresolver analytic logical view name to the concrete view realization;

5, localization analysis;

6, rendering the specific views, etc.;

7, if the execution process encountered an exception will be given to Handlerexceptionresolver to resolve.

Load-on-startup: Indicates that the servlet was initialized when the container was started;

Url-pattern: Indicates which requests are given to spring WEBMVC processing, and "/" is used to define the default servlet mappings. You can also block all requests that have HTML extensions as "*.html".

Dispatcherservlet uses Webapplicationcontext as the context by default, and the spring default profile is "/web-inf/[servlet name]-servlet.xml".


These are the easy-to-understand sections of the Web. Xml article.

The path of Spring MVC development (II.)

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.