当前位置: 首页 > 知识库问答 >
问题:

Spring主题与REST不兼容

鲁斯伯
2023-03-14

我已经试着解决这个问题好几天了,我在论坛上也找不到任何有类似问题的帖子。当我将servlet映射url模式设置为*时,Spring主题运行良好。web中的html。xml文件。但是,如果我更改web中的url模式。将xml文件改为正斜杠,以便默认servlet处理所有请求,并且URL可以看起来像REST URL,然后我使用它来获得无映射错误,这导致无法显示网页的css设置。我用来获取的无映射警告如下:

WARN http-apr-8080-exec-6 (DispatcherServlet.java:947) - No mapping found for HTTP request with URI [/MyApp/themes/default.css] in DispatcherServlet with name 'spring'.

但在通过添加到我的Spring servlet配置文件来解决映射问题时:

<mvc:default-servlet-handler />  

出现了另一个问题:现在,在应用程序启动后和任何操作之前,在任何REST操作之前单击主题选项的使用效果很好。这一切都很好地展示了正确的主题。事实上,如果我通过正常的控制器(没有REST)进行非REST操作,一切都会继续正常工作,我可以在主题之间切换。然而,在通过REST访问时,在单击主题时,即使它返回的视图名称与正常控制器返回的视图名称相同,我在浏览器中也会出现以下错误,奇怪的是,没有报告控制台(调试)消息:

    HTTP Status 405 - Request method 'GET' not supported

    type Status report

    message Request method 'GET' not supported

    description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).


    Apache Tomcat/7.0.29

这是我更新的网站。xml文件是:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      <display-name>LiveAppSpring</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <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>
      <servlet>
        <servlet-name>liveAppSpring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>liveAppSpring</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

我更新的Spring servlet配置是:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:p="http://www.springframework.org/schema/p"
                 xmlns:beans="http://www.springframework.org/schema/beans"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

        <!-- Enables the Spring MVC @Controller programming model -->

        <mvc:annotation-driven />

        <mvc:default-servlet-handler />

        <mvc:interceptors>
            <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <beans:property name="paramName" value="lang" />
            </beans:bean>
            <beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
                <beans:property name="paramName" value="theme" />
            </beans:bean>                      
        </mvc:interceptors>


        <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
        <mvc:resources mapping="/resources/**" location="/resources/" />

        <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <!--  
        <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>

        View Classes:
        <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
        -->
        <!--  
        <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />       
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>

        -->

        <!-- This attribute could also be added to viewResolver bean below... 
        <beans:property name="requestContextAttribute" value="requestContext"/>
         -->
        <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">        
            <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />       
        </beans:bean>   


        <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <beans:property name="definitions">
                <beans:list>
                    <beans:value>/WEB-INF/tiles.xml</beans:value>
                </beans:list>
            </beans:property>
        </beans:bean>

        <!-- Application Message Bundle -->
        <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <beans:property name="basename" value="classpath:messages" />
            <beans:property name="defaultEncoding" value="UTF-8"/>
        </beans:bean>

        <beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
            <beans:property name="defaultLocale" value="en"/>
        </beans:bean>

        <!-- Theme setup -->
        <beans:bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
                <beans:property name="basenamePrefix" value="theme-" />
        </beans:bean>

        <beans:bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
            <beans:property name="defaultThemeName" value="default" />
        </beans:bean>

        <!-- 
           The 2 bean declarations below are NOT compatible with 3.0.5 as it conflicts with the mvc tag library (as defined in header).
           Work around is to have the mvc:interceptors tag declared as done near the top of this configuration file.
        -->

        <!--  
        <beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <beans:property name="paramName" value="lang" />
        </beans:bean>
        -->

        <!--   
        <beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">     

            <beans:property name="interceptors">
                <beans:ref bean="localeChangeInterceptor" />
            </beans:property>

        </beans:bean>
        -->

        <context:component-scan base-package="com.liveapp.spring" />

    </beans:beans>

最后,我的主题属性文件位于

LiveAppSpring\src\main\resources 

所有主题属性文件的内容具有以下模式:

css=themes/default.css 

为了安全起见,我在所有3个目录中复制了css文件:

LiveAppSpring\src\main\webapp\resources

LiveAppSpring\src\main\webapp\resources\themes 

LiveAppSpring\src\main\webapp\themes 

任何指示将不胜感激。

共有1个答案

孔鸿云
2023-03-14

我希望/MyApp/主题/default.css应该提供default.css。但这是一个资源文件,您通过

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

因此,正确的请求url应该是MyApp/resources/default。css(假设默认.css位于webapp/resources/default.css中)

 类似资料:
  • 我想看看Spring Boot2和GraphQL。我的pom.xml看起来是: 使用graphql进行Spring Boot的graphql演示项目 当我开始这个项目时,它运行正常。然后我想查看graphiql站点,并放一些查询以供测试。但是在我的IDE的控制台中有一个异常,模式没有加载到GraphiQL-Page上。

  • 我们在堆栈中使用Kafka、Kafka连接和模式注册表。版本为2.8.1(融合6.2.1)。我们使用Kafka connect的配置(key.converter和value.converter),其值为:io.confluent.connect.avro.AvroConverter。 它自动注册主题的新模式。但有一个问题,AvroConverter没有为新模式指定主题级兼容性,当我们试图通过RES

  • 我在使用SpringWebService和SpringFramework4.3时收到以下错误。15个版本罐。 spring-ws-2.1。2.全部释放。jarspring-ws-core-2.2。2.释放。罐子 您能告诉我如何解决这个问题吗?我需要升级SpringWS-JAR还是降低SpringFramework版本。 原因:java。lang.NoSuchMethodError:org。spri

  • 我在运行API-GATEWAY时出现了以下错误,我尝试了很多方法,但我无法解决这个问题。 说明: pom.xml 完整错误信息

  • 问题内容: 最新的Hibernate ORM版本(5.2.0)使得Spring ORM(最新的4.2.6版)不兼容,因为Hibernate必须将其Session.getFlushMode重命名为Session.getHibernateFlushMode,从而导致此NoSuchMethodError: 引起原因:java.lang.NoSuchMethodError:org.hibernate.Se

  • 最新的HiberNate ORM版本(5.2.0)使Spring ORM(4.2.6,最新)不兼容,因为HiberNate不得不将其Session.getFlush模式重命名为Session.getHibernateFlush模式,从而导致NoSuch方法错误: 引起原因:Java . lang . nosuchmethod error:org . hibernate . session . ge