Spring移动重定向404错误

时间:2015-01-24 09:35:00

标签: java spring spring-mvc thymeleaf

我正在使用不同的布局构建网站的移动版本。我使用的是Spring mobile + Thymeleaf。出于测试目的,我将从主站点重定向到移动版本。这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >


<display-name>Student Accounting site</display-name>

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

<servlet>
    <servlet-name>studentacc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>studentacc</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>/m/*</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

</webapp>

这是我的servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="org.something.webversion"/>

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
            <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <!--Thymeleaf beans init:START-->
    <bean id="templateResolver"
          class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
        <property name="additionalDialects">
            <set>
                <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
            </set>
        </property>
    </bean>

    <!--Thymeleaf beans init:END-->     

    <!--Other beans init:START-->
    <mvc:interceptors>
        <!-- Resolve the device which has generated the request -->
        <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />

        <!-- User's site preference -->
        <bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />

        <!-- Redirects users to the device specific site -->
        <bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
            <constructor-arg value="/m" />
        </bean>
    </mvc:interceptors>

    <bean id="liteDeviceDelegatingViewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
        <constructor-arg>
            <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
                <property name="templateEngine" ref="templateEngine"/>
                <property name="characterEncoding" value="UTF-8"/>
            </bean>
        </constructor-arg>
        <property name="mobilePrefix" value="mobile/" />
        <property name="enableFallback" value="true" />
    </bean>
    <!--Other beans init:END-->

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

</beans>

我的控制器:

public class MainController {
    private static final Logger logger = Logger.getLogger(MainController.class.getName());

@RequestMapping(value = "/")
public String HelloWorld(Model model,Device device){
    logger.info("Device is "+device);
    if(device.isNormal()){
        return "hello";
    }
    return "index";
}

}

Mine webapp文件夹如下所示: WEB-INF ---           |          页---                 |               移动 - -                        |                      的index.html

所以,重定向工作很好,我被重定向到移动版本。但是重定向到的页面i不存在(404)。我&#39;只是看不到错误。提前谢谢。

1 个答案:

答案 0 :(得分:1)

&#34;旋转&#34;重定向到网址:8080 / studentacc / m&#34;因为很可能&#34; studentacc&#34;是应用程序服务器中应用程序的名称,因此它必须是第一部分。

从简短的API看,我会尝试使用SiteSwitcherHandlerInterceptor.urlPath(String mobilePath, String rootPath)方法和rootPath的附加参数

<bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
     <constructor-arg value="/m" />
     <constructor-arg value="/studentacc" />
</bean>