Spring Web Flow和Url重写过滤器 - 清理URL

时间:2011-05-23 13:33:19

标签: spring url-rewriting spring-webflow

我正在开发Spring MVC + Web Flow Web应用程序。我已将/home映射到MVC Controller,将/test映射到Flow Controller。要从我尝试使用Url Rewrite Filter的网址中删除/app。 MVC控制器(@Controller)中的映射效果很好: http://localhost:8080/spring/home - >渲染home视图。

但是当一个请求进入WebFlow控制器时出现错误导致Error 404http://localhost:8080/spring/test - >重定向到http://localhost:8080/spring/app/test?execution=e1s1 - > page not found

如何从网址中删除/app并使一切正常运作?

urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
    <!-- to remove /app -->
    <rule>
        <from>/**</from>
        <to>/app/$1</to>
    </rule>
    <outbound-rule>
        <from>/app/**</from>
        <to>/$1</to>
    </outbound-rule>
</urlrewrite>

Dispatcher servlet mapping

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

简单控制器

@Controller
public class MainController {

    @RequestMapping(value={"/home", "/"})
    public String index(Model model) {
        return "index";
    }

}

部分日志

DEBUG [FlowHandlerMapping:108] : Mapping request with URI '/spring/app/test' to flow with id 'test'
DEBUG [FlowExecutorImpl:135] : Launching new execution of flow 'test' with input null
DEBUG [FlowDefinitionRegistryImpl:59] : Getting FlowDefinition with id 'test'
DEBUG [FlowExecutionImplFactory:78] : Creating new execution of 'test'
...
DEBUG [FlowExecutionImpl:417] : Assigned key e2s1
DEBUG [FlowHandlerAdapter:367] : Sending flow execution redirect to '/spring/app/test?execution=e2s1'
DEBUG [DispatcherServlet:824] : Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
DEBUG [DispatcherServlet:674] : Successfully completed request
DEBUG [DispatcherServlet:693] : DispatcherServlet with name 'spring' processing GET request for [/spring/app/app/test]
DEBUG [FlowHandlerMapping:114] : No flow mapping found for request with URI '/spring/app/app/test'
WARN  [PageNotFound:947] : No mapping found for HTTP request with URI [/spring/app/app/test] in DispatcherServlet with name 'spring'

2 个答案:

答案 0 :(得分:1)

我暂时使用自定义的FlowHandler将其作为here。它有效,但我认为它必须是一个更简单的解决方案...

package utils;
public class PrettyFlowUrlHandler extends DefaultFlowUrlHandler {

    @Override
    public String createFlowDefinitionUrl(String flowId, AttributeMap input, HttpServletRequest request) {
       return cleanUrl(super.createFlowDefinitionUrl(flowId, input, request), request);
    }

    @Override
    public String createFlowExecutionUrl(String flowId, String flowExecutionKey, HttpServletRequest request) {
       return cleanUrl(super.createFlowExecutionUrl(flowId, flowExecutionKey, request), request);
    }

    protected String cleanUrl(String url, HttpServletRequest request) {
       String pattern = request.getServletPath().substring(1) + "/";
       return url.replaceFirst(pattern, "");
    }
}

配置:

<bean id="flowMappings" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry"/>
    <property name="flowUrlHandler">
        <bean class="utils.PrettyFlowUrlHandler"/>
    </property>
    <property name="order" value="0" />
</bean>

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor"/>
    <property name="flowUrlHandler">
        <bean class="utils.PrettyFlowUrlHandler"/>
    </property>
</bean>

修改 不使用如下所示的自定义流程处理程序:

@Component("test")
public class DataHandler extends AbstractFlowHandler {

    @Override
    public String handleExecutionOutcome(FlowExecutionOutcome outcome,
            HttpServletRequest request, HttpServletResponse response) {
        // ... checking outcome ...
        return "/home"; // redirect to '404 page not found', because of rewrite to `/app/app/home`
    }

}

答案 1 :(得分:-1)

您是否尝试从网址中移除应用,例如: http://www.mydomain.com/app/home.html并将其更改为 http://www.mydomain.com/home.html

如果是,那么您应该配置server.xml,并且您的应用程序应该部署为 ROOT 而不是public_html /或tomcat目录中的 app

这对你有用