3

I'm working on Spring MVC + Web Flow web application. I have mapped /home to MVC Controller and /test to Flow Controller. To remove /app from URLs i'm trying to use Url Rewrite Filter. Mappings in MVC Controllers (@Controller) works good with that: http://localhost:8080/spring/home -> render home view.

But when a request goes to WebFlow Controller something is wrong resulting in Error 404: http://localhost:8080/spring/test -> redirect to http://localhost:8080/spring/app/test?execution=e1s1 -> page not found.

How to remove /app from URLs and got everything working ?

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>

Simple controller:

@Controller
public class MainController {

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

}

Some logs:

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'
marioosh
  • 27,328
  • 49
  • 143
  • 192

2 Answers2

1

Temporarily i did it as here using customized FlowHandler. It works, but i think it must be a simpler solution...

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, "");
    }
}

config:

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

Edit Not working with custom flow handler like below:

@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`
    }

}
marioosh
  • 27,328
  • 49
  • 143
  • 192
-1

Are you you trying to remove app from your url for eg http://www.mydomain.com/app/home.html and change it to http://www.mydomain.com/home.html

If yes then you should be configuring server.xml and your application should be deployed as ROOT instead of app in public_html/ or your tomcat directory.

This will work for you

vsingh
  • 6,365
  • 3
  • 53
  • 57