Front and Back End Separation of Java Projects - Spring MVC Configuration of html View Parser

Keywords: xml Spring JSP JavaEE

Links to the original text: https://blog.csdn.net/qq_38355456/article/details/73603602

Based on the separation of the front and rear parts of the project, it does not use the backend template engine, so what is the end of.jsp,.vm template engine is also useless, the front end development of good projects packaged with webpack, placed in the back-end project, directly configure the html view parser, and then give all static resources to the heart, so that you can directly pack the static files after adding war packets directly. Put it on the server and don't talk much nonsense. The way is as follows:


Step 1: Configure web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">

     
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
     
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
     
        <filter>
            <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
     
        <servlet>
            <servlet-name>e3-manager</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- contextConfigLocation Not required, if not configured contextConfigLocation, springmvc The configuration file defaults to: WEB-INF/servlet Of name+"-servlet.xml" -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>e3-manager</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

It's better not to configure resource filtering release in servlet-mapping because Tomcat is not very good at handling static resources. He's a good at handling dynamic resources. That's it.


Step 2: Configure the html parser in spring MVC

    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".html"/>
        <property name="contentType" value="text/html"/>
    </bean>


Step 3: Set up static resource release rules in spring mvc:

    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/fonts/**" location="/fonts/"/>
    <mvc:resources mapping="/style/**" location="/style/"/>

Or direct release of all static resources:

    <mvc:default-servlet-handler />


Restart the service after configuration, and you can access your static page. When packaging, enter the war package directly and deploy it on the server.
 

 

Posted by fantic on Tue, 08 Oct 2019 23:54:27 -0700