Spring Integration Spring MVC

Keywords: Spring xml encoding JSP

Spring Integration Spring MVC

POM

Add the org.spring framework: spring-webmvc dependency to the pom.xml configuration file

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.17.RELEASE</version>
</dependency>

Configure web.xml

CharacterEncodingFilter

Configure Character Set Filter to Solve Chinese Coding Problem

<filter>
    <filter-name>encodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

DispatcherServlet

Configure Spring's Servlet Distributor to process all HTTP requests and responses

<servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring-mvc*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Configure Spring MVC

Create a file named spring-mvc.xml to configure MVC

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <description>Spring MVC Configuration</description>

    <!-- Load the configuration properties file -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>

    <!-- Use Annotation automatic logon Bean,Scanning only @Controller -->
    <context:component-scan base-package="com.lusifer.myshop" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- Default annotation mapping support -->
    <mvc:annotation-driven />

    <!-- Define View File Resolution -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="${web.view.prefix}"/>
        <property name="suffix" value="${web.view.suffix}"/>
    </bean>

    <!-- Static resource mapping -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>

Relevant configuration instructions:

  • context:property-placeholder: dynamically loaded property configuration files refer to the required values in a variable manner

  • context:component-scan: The current configuration file is MVC-related, so you only need to scan the annotations containing @Controller. Because package scanning is also configured in the spring-context.xml configuration file, you need to exclude @Controller's annotation scanning.

  • Internal ResourceViewResolver: A view file parser that configures the path of view resources and the type of view resource files to be interpreted. There are two properties prefix and suffix to be configured.

  • prefix: Configure the view resource path, such as: / WEB-INF/views/

  • suffix: Configure view resource types, such as:. jsp

  • mvc:resources: static resource mapping, mainly used to configure static resource file storage path, such as: JS, CSS, Image, etc.

System Related Configuration

In spring-mvc.xml, we configure < context: property-placeholder ignore-unresolvable= "true" location= "classpath: myshop. properties"/> to dynamically load the attribute configuration file. In actual development, we will encapsulate some configuration information needed by the system into the. properties configuration file for unified management.

Create a configuration file named myshop.properties, which reads as follows:

#============================#
#==== Framework settings ====#
#============================#

# \u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp

Remove duplicate scanning from Spring configuration

Since the scanning of @Controller annotation has been configured in spring-mvc.xml and the scanning of all annotations in spring-context.xml, it is necessary to exclude the scanning configuration of @Controller annotation here.

Modify the spring-context.xml configuration:

<! - Use Annotation to automatically register beans, do not scan @Controller annotations in the main container, and only scan @Controller annotations in Spring MVC. -->
<context:component-scan base-package="com.funtl.my.shop">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

Posted by fxb9500 on Mon, 22 Jul 2019 01:32:57 -0700