如何在Webflow中使用Spring-Mobile?

时间:2013-10-01 05:54:15

标签: spring-mvc spring-webflow spring-mobile

我需要使用Spring-mobile来检测设备。我看过很多关于spring-mobile和spring mvc的例子,但没有使用webflow的例子。下面是一个示例Web流,我需要使用设备检测,以便我可以根据设备将页面重定向到移动设备或桌面或桌面。

的Webflow-config.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:webflow="http://www.springframework.org/schema/webflow-config"
    xmlns:faces="http://www.springframework.org/schema/faces"
    xsi:schemaLocation="
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/webflow-config
                        http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd
                        http://www.springframework.org/schema/faces
                        http://www.springframework.org/schema/faces/spring-faces.xsd">



    <!-- JSF Specific -->
    <!-- A listener maintain one FacesContext instance per Web Flow request. -->
    <bean id="facesContextListener"
        class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
    <!--- Executes flows: the central entry point into the Spring Web Flow system 
        - -->
    <webflow:flow-executor id="flowExecutor">
        <webflow:flow-execution-listeners>
            <webflow:listener ref="facesContextListener" />
        </webflow:flow-execution-listeners>
    </webflow:flow-executor>
    <!-- The registry of executable flow definitions -->
    <webflow:flow-registry id="flowRegistry"
        flow-builder-services="flowBuilderServices" base-path="/WEB-INF/flows">
        <webflow:flow-location-pattern value="/**/*-flow.xml" />
    </webflow:flow-registry>
    <!-- Configures the Spring Web Flow JSF Integration -->
    <faces:flow-builder-services id="flowBuilderServices"
        development="true" />
    <faces:resources />


    <bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>
    <bean id="faceletsViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.faces.mvc.JsfView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".xhtml" />
    </bean>

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="order" value="1" />
        <property name="flowRegistry" ref="flowRegistry" />
        <property name="defaultHandler">
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
        </property>
    </bean>
    <!-- Dispatches requests mapped to org.springframework.web.servlet.mvc.Controller 
        implementations -->
    <bean
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
</beans>

flow main

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <view-state id="welcome">
    </view-state>

</flow>

Web.xml中

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>WebFlowSample</display-name>

    <!-- - Location of the XML file that defines the root application context. 
        - Applied by ContextLoaderListener. -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/application-config.xml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>

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

    <!-- Resource mapping -->
    <servlet>
        <servlet-name>Resources Servlet</servlet-name>
        <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resources Servlet</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>


    <!-- - Servlet that dispatches request to registered handlers (Controller 
        implementations). -->


    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

    <!--   In order for JSF to bootstrap correctly -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

</web-app>

控制器

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/wb")
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */

        @RequestMapping("/create/")
        public String home(Device device, Model model) {
            if (device == null) {
                logger.info("no device detected");
            } else if (device.isNormal()) {
                logger.info("Device is normal");
            } else if (device.isMobile()) {
                logger.info("Device is mobile");
            } else if (device.isTablet()) {
                logger.info("Device is tablet");
            }
                          // where main is the flow id for welcome page
            return "main"; 
        }
}

更新

我需要homecontroller类来调用流,这不是正确的方法。但是,任何人都可以告诉如何打电话吗?

1 个答案:

答案 0 :(得分:0)

(这是对你的评论的回复,但是太大而不能作为评论本身。)

为了向Webflow添加拦截器,请将它们添加到FlowHandlerMapping bean定义中。像这样:

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    ...
    <property name="interceptors">
        <list>
            <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor"/>
        </list>
    </property>
</bean>