0

我正在开发一个在移动设备和网络上运行的 Spring 应用程序。在 Web 上一切运行良好,但在移动设备上,当表单发布时,它会命中控制器并且控制器会重定向到其他应用程序。

@RequestMapping(value = "/common", method = RequestMethod.GET)
public String showLandingPage(HttpServletRequest req, HttpServletResponse response, Model model) {
logger.debug("Received request to set partner info");
Device currentDevice                = DeviceUtils.getCurrentDevice(req);
setCookies(response);
Properties props = new Properties();
try {
    props.load(getClass().getClassLoader().getResourceAsStream( "sampleApp.properties"));
} catch (IOException e) {
    logger.fatal(new StringBuilder("MainController : setCookies() : Error while reading sampleApp.properties "+e));
}catch (Exception e) {
    logger.fatal(new StringBuilder("MainController : setCookies() : Error while reading sampleApp.properties "+e));
}
if(currentDevice.isMobile() || currentDevice.isTablet()){
    return "redirect:"+props.getProperty("popcorn-mobile-url");
} else {               
    return "redirect:"+props.getProperty("popcorn-web-url");
}
}

当控件转到重定向位置时,我在屏幕上看到“错误加载页面”。

在 JSP 中,我使用以下 jQuery 库。

    <script src="${pageContext.request.contextPath}/js/mobile/mobile-config.js"></script> 
    <script src="${pageContext.request.contextPath}/js/mobile/jquery.mobile-1.2.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/js/mobile/plugins.js"></script>
4

1 回答 1

0

我会使用一个设备拦截器,如下所示:

public class DeviceInterceptor extends HandlerInterceptorAdapter {    
private final DeviceResolver deviceResolver;

    private Device device;

    /**
     * Create a device resolving {@link HandlerInterceptor} that defaults to a
     * {@link LiteDeviceResolver} implementation.
     */
    public DeviceInterceptor() {
        this(new LiteDeviceResolver());
    }

    public DeviceInterceptor(DeviceResolver deviceResolver) {
        this.deviceResolver = deviceResolver;
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception    {
        device = deviceResolver.resolveDevice(request);
        request.setAttribute(DeviceUtils.CURRENT_DEVICE_ATTRIBUTE, device);
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) throws Exception {
    if (device.isMobile()) {
            modelAndView.setViewName("/mobile/" + modelAndView.getViewName());
        }else {
            modelAndView.setViewName("/jsp/" + modelAndView.getViewName());
        }           
    }
于 2013-02-06T10:31:49.250 回答