Java 如果spring映射与真实的html文件冲突,如何设置它?

Java 如果spring映射与真实的html文件冲突,如何设置它?,java,spring,Java,Spring,我配置spring并将其映射到*.htm,但它也应用于real.htm文件。因此,我无法在web应用程序中访问真正的静态*.htm。我应该如何配置spring来解决这个问题?谢谢 <servlet-mapping> <servlet-name>systemdispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> 系

我配置spring并将其映射到*.htm,但它也应用于real.htm文件。因此,我无法在web应用程序中访问真正的静态*.htm。我应该如何配置spring来解决这个问题?谢谢

<servlet-mapping>
  <servlet-name>systemdispatcher</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping> 

系统调度器
*.htm

如果我尝试访问像index.htm这样的静态htm文件,它将显示404错误。如何配置spring以解决此问题?谢谢

您不能直接解决它,因为您有冲突的映射。但是您可以让spring读取html文件并将其输出给用户

不过,建议对静态文件使用
.html
,这样就不会有任何冲突

除此之外,还需要放置资源处理程序,以便从DispatcherServlet跳过静态内容

<mvc:resources location="/resources/" mapping="/resources/**" />

您不能直接解决它,因为您有冲突的映射。但是您可以让spring读取html文件并将其输出给用户

不过,建议对静态文件使用
.html
,这样就不会有任何冲突

除此之外,还需要放置资源处理程序,以便从DispatcherServlet跳过静态内容

<mvc:resources location="/resources/" mapping="/resources/**" />

您是否在spring配置文件中这样设置了视图解析器

<!-- View Resolver -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

/WEB-INF/pages/
.jsp
在上面的示例中,将.jsp更改为.htm。之后,您可以使用web.xml中的任何url映射,即您已经使用的url映射

<servlet-mapping>
  <servlet-name>systemdispatcher</servlet-name>
  <url-pattern>*.htm</url-pattern>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

系统调度器
*.htm
*.做

它将起作用,因为您将只在控制器中提供视图名称,而Spring mvc将完成其余工作。

您是否在Spring配置文件中设置了视图解析器,如下所示

<!-- View Resolver -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

/WEB-INF/pages/
.jsp
在上面的示例中,将.jsp更改为.htm。之后,您可以使用web.xml中的任何url映射,即您已经使用的url映射

<servlet-mapping>
  <servlet-name>systemdispatcher</servlet-name>
  <url-pattern>*.htm</url-pattern>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

系统调度器
*.htm
*.做

它将起作用,因为您将只在controller中提供视图名称,而Spring mvc将完成其余工作。

我同意@Bozho给出的解决方案

但还有另一种方法可以做到这一点。您可以指定ResourceHandler,它可以从应用程序的特定路径提供静态内容,而无需将请求传输到DispatcherServlet

<mvc:resources location="/resources/" mapping="/resources/**" />
尝试在servlet xml文件中执行以下配置

<mvc:resources location="/resources/" mapping="/resources/**" />
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

在这里,您可以替换static.htm文件所在的路径

希望这对你有帮助。
干杯。

我同意@Bozho给出的解决方案

但还有另一种方法可以做到这一点。您可以指定ResourceHandler,它可以从应用程序的特定路径提供静态内容,而无需将请求传输到DispatcherServlet

<mvc:resources location="/resources/" mapping="/resources/**" />
尝试在servlet xml文件中执行以下配置

<mvc:resources location="/resources/" mapping="/resources/**" />
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

在这里,您可以替换static.htm文件所在的路径

希望这对你有帮助。
干杯。

您可以在xml文件中尝试以下模式定义

<mvc:resources location="/resources/" mapping="/resources/**" />
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


可能会出现spring版本冲突的情况,然后用您的版本替换该版本。

您可以在xml文件中尝试以下模式定义

<mvc:resources location="/resources/" mapping="/resources/**" />
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


您的spring版本可能会发生冲突,然后将其替换为您的版本。

还有其他选择吗?我也无法将其更改为.html。我们可以添加以解决静态资源映射问题。还有其他选择吗?我也无法将其更改为.html。我们可以添加以解决静态资源映射问题。请您解释一下上面的配置放在哪里?在dispatcher-servlet.xml文件中。但上面的行应该放在哪一部分?在bean标记中?我将其添加到dispatch-servlet.xml中,如下所示:但我得到错误:javax.servlet.ServletException:javax.servlet.ServletException:handler[com.sas.pdg.mrr.controller]没有适配器。UserController@5e34d46a]:您的处理程序是否实现了一个受支持的接口,如控制器?请您在dispatcher-servlet.xml文件中解释将配置放在上面的什么位置。但是将行放在上面的哪个部分?在bean标记中?我将其添加到dispatch-servlet.xml中,如下所示:但我得到错误:javax.servlet.ServletException:javax.servlet.ServletException:handler[com.sas.pdg.mrr.controller]没有适配器。UserController@5e34d46a]:您的处理程序是否实现了类似控制器的受支持接口?