3

Spring Mobileを試していますが、基本的な例が機能していないようです。ばかげてシンプルなものが欠けているような気がしますが、それが何なのかわかりません。これが私が用意しているものです...

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

デバイスが設定されず、結果がnullになるのはなぜですか?

編集:ログファイルはインターセプターの設定に問題があることを示しているようですが、どこが間違っているのかまだわかりません。

タイプ'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]に変換できません:一致するエディターまたは変換戦略が見つかりません

4

2 に答える 2

4

私はこれを見て、なんとか自分で動作させることができました。だから私はいくつかのコメントがあります。

1a)フィルターとインターセプターの両方が必要だとは思いません。フィルタを使用したばかりで、それで十分でした。

1b)インターセプター(使用する場合)は、 xml構成ファイルで構成する必要がありDispatcherServletます。の使用からStrutsを使用しているように見えますがActionSupport、これは正しいですか?もしそうなら、あなたは(おそらく)持っていないでしょう、DispatcherServletそしてそれ故に私はこの設定が期待通りに機能するとは思わない。これが、スタックトレースを取得している理由だと思います。

2)ブレークポイントを追加して、org.springframework.mobile.device.DeviceResolverRequestFilter.doFilterInternal実行されていることを確認します。

3)Strutsがで「おかしな」ことをしていないことを確認しServletRequest「currentDevice」リクエスト属性をなんらかの方法で非表示にします。実際、可能であれば、コードをバニラスプリングに移植します。

4)メソッドで使用ServletActionContext.getRequestして、executeそれが機能するかどうかを確認したり、返されたリクエストをで設定されたリクエストと比較したりできますsetServletRequest


Spring MVCを使用すると、これが私にとってうまくいきます。私のプロジェクトはspring-mobile-testと呼ばれ、「spring-mobile-test」はそのコンテキストルートです。

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/

デスクトップユーザーの皆さん、こんにちは。

于 2012-06-12T22:52:12.517 に答える
0

web.xmlにフィルターを追加するだけで、モバイルデバイスのエテクションを処理できます。

1-web.xmlファイルで以下の構成を使用します。

<!--Device Resolver filter start here  -->
<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>  

2-以下のjarファイルを追加します

        spring-mobile-device-1.x.x.RELEASE.jar

3-コードデバイスをチェックするには

        Device device=DeviceUtils.getCurrentDevice(request);
        boolean isMobile = device.isMobile();
        boolean isTablet=device.isTablet();
于 2018-04-26T05:32:38.690 に答える