2012-05-29 26 views
3

我和春天實驗移動,但我似乎無法獲得基本的例子工作。我有一種感覺,我想的東西太簡單,但我無法弄清楚它是什麼。以下是我在的地方...Spring Mobile - Interceptor不適用?設備是空

在web.xml中

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/applicationContext.xml 
    </param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<filter> 
    <filter-name>deviceResolverRequestFilter</filter-name> 
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>deviceResolverRequestFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

在applicationContext.xml中

<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xmlns:device="http://www.springframework.org/schema/mobile/device" 
     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.1.xsd 
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/mobile/device 
       http://www.springframework.org/schema/mobile/device/spring-mobile-device-1.0.xsd"> 

    <!-- Interceptors that execute common control logic across multiple requests --> 
    <interceptors> 
     <!-- Detects the client's Device --> 
     <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" /> 
    </interceptors> 

</beans:beans> 

在我的Java類:

public class TestAction extends ActionSupport implements ServletRequestAware { 

    // So that we can lookup the current Device 
    private HttpServletRequest request; 

    public void setServletRequest(HttpServletRequest request) { 
     this.request = request; 
    } 

    @Override 
    public String execute() { 
     Device currentDevice = DeviceUtils.getCurrentDevice(request); 
     if (currentDevice.isMobile()) // <-- fails here with NPE 

爲什麼該設備未設置並導致爲空?

編輯: 日誌文件似乎表明與設置攔截器有問題,但我仍然不知道我哪裏錯了。

2012-05-29 09:36:36696 DEBUG [ConstructorResolver.java:201]: 忽略構造[公共 org.springframework.web.servlet.handler.MappedInterceptor(java.lang.String中[] ,org.springframework.web.context.request.WebRequestInterceptor)] bean'org.springframework.web.servlet.handler.MappedInterceptor#0': org.springframework.beans.factory.UnsatisfiedDependencyException: 創建名稱爲bean的錯誤 「org.springframework.web.servlet.handler.MappedInterceptor#0」: 不合格依賴通過構造器參數表示與類型的 索引[org.springframework.web.context.request.WebRequestInterceptor]:可能不 [org.springframework.mobile.device.DeviceResolverHandlerInterceptor] 類型的構造器參數值轉換爲所需的類型 [org.springframework.web.context。 request.WebRequestInterceptor]: 未能轉換類型 'org.springframework.mobile.device.DeviceResolverHandlerInterceptor' 所需類型 'org.springframework.web.context.request.WebRequestInterceptor' 的值; 嵌套異常是java.lang.IllegalStateException:不能 [org.springframework.mobile.device.DeviceResolverHandlerInterceptor] 類型的 值轉換爲所需的類型 [org.springframework.web.context.request.WebRequestInterceptor]:無 匹配編輯或轉換策略找到

回答

3

我已經看過這個,並設法讓它自己工作。所以我有幾點意見。

1A),我不認爲同時需要過濾器和攔截器。 I've just used the Filter and that was enough

1b)中的攔截器(如果使用的話)should be configured in a DispatcherServlet xml config file。你看起來像你使用Struts從使用ActionSupport,這是正確的?如果是這樣,你(可能)將不會有DispatcherServlet,因此我不認爲這個配置將按預期工作。我想這就是你得到堆棧跟蹤的原因。

2)我將斷點添加到org.springframework.mobile.device.DeviceResolverRequestFilter.doFilterInternal以確保它被執行。

3)我會檢查Struts是不是在與ServletRequest做一些有趣的事情,並以某種方式隱藏"currentDevice" request attribute。事實上,如果可能的話,我會將你的代碼移植到Spring。

4)也許你可以在你的execute方法使用ServletActionContext.getRequest,看看有沒有工作,和/或在setServletRequest比較返回的請求傳送到該組。


使用Spring MVC,這對我很有用。我的項目被稱爲彈簧移動測試,和「春天的移動測試」是它的上下文根:

的web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <filter> 
     <filter-name>deviceResolverRequestFilter</filter-name> 
     <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>deviceResolverRequestFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <servlet> 
     <servlet-name>mvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc</servlet-name> 
     <url-pattern>/mvc/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

的applicationContext.xml是空的。

MVC-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="temp" /> 

</beans> 

temp.TestController:

package temp; 

import javax.servlet.http.HttpServletRequest; 

import org.apache.log4j.Logger; 
import org.springframework.mobile.device.Device; 
import org.springframework.mobile.device.DeviceUtils; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class TestController { 
    private static final Logger logger = Logger.getLogger(TestController.class); 

    @RequestMapping("/") 
    public @ResponseBody 
    String home(HttpServletRequest req) { 
     Device device = DeviceUtils.getCurrentDevice(req); 
     String msg = ""; 
     if (device.isMobile()) { 
      msg = "Hello mobile user!"; 
     } else { 
      msg = "Hello desktop user!"; 
     } 
     logger.info(msg); 
     return msg; 
    } 
} 

瀏覽器顯示下面的文字,當我瀏覽到URL http://localhost/spring-mobile-test/mvc/

你好桌面用戶!

+0

正如我以爲......這是一個非常愚蠢的錯誤,沒有人能夠在這裏確定,因爲我沒有發佈整個'web.xml'內容。我有我的過濾器設置倒退!你的建議#2幫助我在正確的地方設置斷點來實現我的錯誤。我相信我需要過濾器和攔截器才能讓我的應用程序工作......至少在我的情況下。 – nmc

相關問題