2011-08-21 7 views
0

기존의 Spring 3.0.5 프로젝트에 테마를 추가하고 있습니다. 나는 CookieThemeResolver를 사용하고있다. 로드 할 테마를 얻을 수는 있지만 지정된 기본값 (theResolver bean에 지정된대로)이 아니며 themeChangeInterceptor가 작동하지 않는 것 같습니다.스프링 테마 및 애플리케이션 컨텍스트

스프링 MVC- 기본 프로젝트에 추가 한 (CSS 리소스) 때문에 작업을 추가 한 세 가지 테마 구성을 알고 있습니다. 그들은 잘 작동했다. 또한 Spring mvc-basic 프로젝트는 web.xml에서 ContextLoaderListener를 필요로하지 않았습니다.

원래 프로젝트 구성에 테마 구성을 추가하면 spring : theme 태그가 포함 된 페이지가 해당 페이지의 오류를 일으키고 java.lang.IllegalStateException을 발생시킵니다. WebApplicationContext가 없습니다. spring : theme 태그에 등록 된 ContextLoaderListener가 없습니다. 그래서 ContextLoaderListener를 추가하고 spring mvc-basic 앱에도 application-context.xml에없는 내용을 넣으십시오.

이 오류는 Spring mvc-basic 프로젝트의 동일한 스프링 태그와 테마 구성에서 발생하지 않으므로 (mvc 프로젝트에는 리스너 또는 컨텍스트 매개 변수 속성이 없음), 다음 중 하나의 요소 지금은 내 애플리케이션 컨텍스트가 문제입니다.

내 구성 요소가 아래 구성에서 실행되면 theme.properties 파일 (theme-day.properties 및 theme-night.properties와 함께 클래스 경로에 있음)가로드됩니다. ? theme = day 또는? theme = night 요청이있을 때 쿠키가 설정되지 않습니다. 그러나 로그 설정이 추적 되더라도 오류가 발생하지 않습니다.

누군가가 WTF가 발생했다는 것을 지적 할 수 있기를 바랍니다. 필자는 configs를 최소한으로 페어링했습니다. 나머지 요소 중 하나 이상이 문제의 원인입니다. 궁금한 점이 있으면 질문하십시오.

문제를 해결하지 못했던 응용 프로그램 컨텍스트로 모든 것을 옮겨 보았습니다. 요소를 제거하려고 시도했지만 여기에 게시 된 항목은 전체 앱으로 이동하지 않고 최소한입니다. 재검색 된 항아리에는 동일한 버전의 올바른 버전과 POM 파일이 있습니다.

추가 정보 : 로캘 분석자가 태그 없이도 동일한 패턴을 따르므로 JSP에서 모든 테마 구성 및 태그를 제거하고 모든 선언을 servlet.xml로 다시 이동하고 ContextLoaderListener를 그것의 설정. 결과는 테마와 유사하여 스프링 기본 (브라우저 기본 설정으로 일컬어 짐)이 작동하지만 쿠키를 설정할 수 없으며 로케일 기능과 관련하여 아무 것도 작동하지 않는 것 같습니다. 다시 모든 것을 application.xml로 돌리고 ContextLoaderListener를 다시 삽입하면 동일한 결과가 나타납니다. 적어도 일관성이 유지되는 한, 내 구성의 일부로 인해 로케일과 테마가 모두 손상됩니다. 또한 ContextLoaderListener의 필요성은 테마 태그의 직접적인 기능이 아니라 다른 요소 중 하나의 부작용입니다.

업데이트 2 3.0.6으로 업그레이드하고 추적 레벨에서 실행하는 동안 다음 항목을 찾았습니다.

DEBUG ResourceBundleThemeSource,"http-bio-8080"-exec-5: 109 - Theme created: name 'theme',basename [theme] 

이 줄은 각보기가 렌더링되기 직전에 나타납니다. 그것은 (나에게) ResourceBundleThemeSource가 디폴트 값으로 만 동작한다는 것을 제안한다.

TRACE DefaultListableBeanFactory,Thread-7:201 Ignoring Constructor [public.org.springframework.web.servlet.handler.MappedInterceptor(java.lang.String[],org.springframework.web.context.request.WebRequestInterceptor)] of bean 'org.springframework.web.servlet.handler.MappedInterceptor#1' :org.springframework.beans.factoryUnsatisfiedDependancyException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1':Unsatisfied dependnacy expressed through contructor argument with index 1 of type [org.springframework.web.context.request.WebRequestInterceptor]: Could not convert constructor argument value of type [org.springframework.web.servlet.theme.ThemeChangeInterceptor] to required type [org.springframeworkweb.context.request.WebRequestInterceptor]: Failed to convert value of type 'org.springframework.web.servlet.theme.ThemeChangeInterceptor' to required type 'org.springframeworkweb.context.request.WebRequestInterceptor';nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.servlet.theme.ThemeChangeInterceptor] to required type [org.springframeworkweb.context.request.WebRequestInterceptor]: no matching editors or conversion strategy found 

이 오류는 응용 프로그램을 초기화하는 동안 나타납니다. MVC-Basic 프로젝트에서 똑같은 구성이 작동하기 때문에 왜 이런 일이 발생하는지 확신 할 수 없습니다. 나는 그것이 무엇이 잘못되었는지 보지 못한다는 것을 이해하고 있습니다.다음과 같이

설정은

서블릿-context.xml에

<?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:http-conf="http://cxf.apache.org/transports/http/configuration" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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 
      http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> 

<!-- Scans the classpath of this application for @Components to deploy as beans --> 
<context:component-scan base-package="org.myproject.test" /> 

<!-- Configures the @Controller programming model --> 
<mvc:annotation-driven /> 

<!-- Configures Handler Interceptors -->  
<mvc:interceptors> 
    <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> 
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 

    <bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"> 
     <property name="paramName" value="theme" /> 
    </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/" /> 

<!-- Saves a locale change using a cookie --> 
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> 

<!-- Application Message Bundle --> 
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="/WEB-INF/messages/messages" /> 
    <property name="cacheSeconds" value="0" /> 
</bean> 

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/views/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 
    <!-- Theme source and Resolver definition --> 

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

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

    </beans> 

응용 프로그램의 context.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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"> 

    <!--  Read the config.properties file--> 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:config.properties</value> 
     </list> 
    </property> 
</bean> 

<bean id="myService" class="com.stuff.generated.service1"> 
    <constructor-arg value="${service1UrlPrefix}?wsdl"/> 
</bean> 

<bean id="myServiceFactory" factory-bean="myService1" factory-method="getServicePort"/> 

<bean id="myOtherService" class="com.stuff.generated.service2"> 
    <constructor-arg value="${service2UrlPrefix}?wsdl"/> 
</bean> 
<bean id="myOtherServiceFactory" factory-bean="myService2" factory-method="getServicePort2"/> 

    <!-- assembles some variables used widely which are autowired into other classes 
    contains getter and setters and some code running in afterPropertiesSet() method --> 
    <bean id="myStartVars" class="com.myClass.myStartVars"/> 

    <bean id="autowiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

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"> 
<!-- Reads request input using UTF-8 encoding --> 
<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> 
<!-- Handles all requests into the application --> 
<servlet> 
    <servlet-name>myTest Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> /WEB-INF/spring/servlet-context.xml </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>myTest Servlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/application-context.xml</param-value> 
</context-param> 

+0

모든 Spring 응용 프로그램에 응용 프로그램 컨텍스트가 있으므로 "응용 프로그램 컨텍스트가 없습니다 (필자는 필요하지 않았습니다.)"라는 것이 무슨 뜻인지 확실하지 않습니다. 참고로, 요소는 전체 웹 애플리케이션을위한 반면, contextConfigLocation은 특히 하나의 서블릿을위한 것이다. 나는 이상한 상호 작용이 없는지 확인하기 위해이 설정 파일을 하나의 파일로 병합하는 것으로 시작한다. 또한 오류가 발생했을 때 사용하고 있던 실제 최소 구성 파일을 실제로 게시 했습니까? XML이 유효하지 않은 것 같습니다. – jtoberon

+0

스프링 예제에서 볼 수 있듯이 서블릿 컨텍스트 만있는 앱을 가질 수 있습니다 .Spring이이를 밀어냅니다. 그것은 문서에 있습니다. 원래 그들은 하나의 파일 servlet-conext.xml에 있었고, 나는 알 수없는 이유로 애플리케이션 컨텍스트를 만들어야 만했다. 테마 태그와 설정을 spring mvc basic에 추가 할 수 있었고 서블릿 컨텍스트에서만 잘 작동했기 때문에 알 수 없다고합니다. 네, 잘라내어 붙여 넣은 실제 파일입니다. 스택 오버플로 인터프리터는 주석의 모양을 엉망으로 만들었지 만 모든 부분이있는 것처럼 보입니다. – stimpy

+1

실수로 XML이 유효합니다. 다음으로해야 할 일은 ResourceBundleThemeSource 및 CookieThemeResolver의 생성자, 등의 동작을 이해하려는 클래스의 메서드 등 Spring 코드의 다양한 지점에 중단 점을 넣는 것입니다. 그런 다음 응용 프로그램을 시작합니다. 디버거를 사용하여 실제로 어떤 일이 일어나고 있는지, 이러한 클래스가 어떻게 인스턴스화되는지 등을 확인하십시오. 또는 다른 방법은 테스트 테마 나 로케일을 제외하고는 아무 것도하지 않도록 응용 프로그램을 더 많이 제거하는 것입니다. 이 작업을 완료하면 기능을 하나씩 추가하십시오. – jtoberon

답변

0

이 솔루션을 사용하십시오.

로케일과 테마 시스템은 WEB-INF 외부 앱의 요소에는 적용 할 수 없다는 것이 판명되었습니다.

그래서 응용 프로그램에서 느슨한 JSP에서 로캘과 테마를 사용하려고하는 스프링 태그를 넣는 것은 실패합니다.

위의 코드의 경우 나는 모든 것을 servlet-context.xml로 다시 옮겼습니다. web-app의 루트에있는 index.jsp 파일에서 태그를 제거하고 보기로 다시 돌아갑니다. 이제 모든 것이 작동합니다.

이 정보가 도움이되기를 바랍니다. 다르게 찾으면 여기에 게시하십시오.

관련 문제