2011-05-23 50 views
3

我正在處理Spring MVC + Web Flow Web應用程序。我已將/home映射到MVC控制器,並將/test映射到流量控制器。要從我嘗試使用的網址中刪除/appUrl Rewrite Filter。 MVC控制器(@Controller)中的映射適用於: http://localhost:8080/spring/home - >呈現home視圖。Spring Web Flow和Url重寫篩選器 - 清除URL

但是,當一個請求到達一個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> 

分派器servlet映射

<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' 

回答

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

你是否試圖從你的網址中移除應用程序,例如: http://www.mydomain.com/app/home.html並將其更改爲 http://www.mydomain.com/home.html

如果是的話,那麼你應該配置的server.xml和您的應用程序應該被部署爲ROOT,而不是在的public_html 應用 /或你的tomcat目錄。

這將適用於你

+0

完全不相關的答案。他沒有讀過這個問題。 – 2014-10-19 01:48:32