我想在web.xml中为两个不同的url模式加载两个欢迎文件

时间:2016-02-24 10:19:42

标签: java spring-mvc web.xml

下面是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>apiServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/api-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>webServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/web-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>adminServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/admin-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>apiServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>adminServlet</servlet-name>
        <url-pattern>/admin/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>webServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/resources/web/views/layout/layout.html</welcome-file> 
    </welcome-file-list>

</web-app>

现在我想加载像这样的欢迎文件

  1. Welcome file- <welcome-file>/resources/web/views/layout/layout.html</welcome-file>
     Url Pattern- <url-pattern>/</url-pattern>

  2. Welcome file- <welcome-file>resources/admin/views/layout/layout.html</welcome-file> 
     Url Pattern- <url-pattern>admin/*</url-pattern>

我想从web.xml加载欢迎文件,而不是从控制器加载。我尝试了几种方法从web.xml加载不同的url模式的不同欢迎文件,但它不起作用:(

由于

1 个答案:

答案 0 :(得分:0)

您可以使用urlrewrite传递到欢迎文件,而无需用户注意。它简单实用。

<rule>
  <from>/</from>
  <to>/layout.html</to>
</rule>

<rule>
  <from>/admin/</from>
  <to>/admin_layout.html</to>
</rule>