Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Stripes application to use Friendly URLs

Tags:

java

stripes

I am working through Fred Daoud's Stripes book and trying to convert the Hello World application to use friendly URLs, as I'm not a big fan of suffix-based mappings like http://localhost:8080/getting_started/Hello.action.

Here is the before ...

index.jsp:

<jsp:forward page="/Hello.action"/>

web.xml:

<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

and I have no UrlBinding on my HelloActionBean. I have the book example working.

I'm wondering if the book examples may suit an earlier version of Stripes, as I've downloaded 1.5.1 and my web.xml defines the StripesFilter and StripesDispatcher whereas I've seen a DynamicMappingFilter used elsewhere, e.g. in this article by Fred on TheServerSide.

Anyway, I made the following changes:

index.jsp:

<jsp:forward page="/hello"/>

web.xml:

<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
 </servlet-mapping>

HelloActionBean.java:

**@UrlBinding("/hello")**
public class HelloActionBean implements ActionBean 
{

However, when I try to load the app through http://localhost:8080/getting_started I see this:

net.sourceforge.stripes.exception.ActionBeanNotFoundException: Could not locate an ActionBean that is bound to the URL [/]. Commons reasons for this include mis-matched URLs and forgetting to implement ActionBean in your class. Registered ActionBeans are: {/hello=class stripesbook.action.HelloActionBean, /controller/DefaultView.action=class net.sourceforge.stripes.controller.DefaultViewActionBean, /hello/=class stripesbook.action.HelloActionBean, /controller/DefaultView.action/=class net.sourceforge.stripes.controller.DefaultViewActionBean}
    at net.sourceforge.stripes.controller.AnnotatedClassActionResolver.getActionBean(AnnotatedClassActionResolver.java:341)

and if I access it through http://localhost:8080/getting_started/hello the server seems to go into a loop throwing one exception after another.

Any suggestions appreciated - thanks.

like image 362
Andrew Whitehouse Avatar asked Apr 07 '09 12:04

Andrew Whitehouse


2 Answers

I've been trying out a few other things and got it working ...

I removed the existing DispatcherServlet servlet and servlet-mapping definitions in web.xml and replaced with the DynamicMappingFilter.

As a bonus, to change the way link events are passed, so that e.g.

http://localhost:8080/getting_started/hello?randomDate=

becomes

http://localhost:8080/getting_started/hello/randomDate

change the UrlBinding on the ActionBean to:

@UrlBinding("/hello/{$event}")
like image 98
Andrew Whitehouse Avatar answered Sep 21 '22 04:09

Andrew Whitehouse


It didn't work for me to just replace the Dispatcher servlet with the DynamicMappingFilter (I got an error message about the DynamicMappingFilter only works in conjunction with the StripesFilter). So I have two filters and one filter-mapping configured in my web.xml now:

<filter>
    <display-name>Stripes Filter</display-name>
    <filter-name>StripesFilter</filter-name>
    <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
    <init-param>
        <param-name>ActionResolver.Packages</param-name>
        <param-value>com.package.myactions.package</param-value>
    </init-param>
</filter>

<filter>
    <description>Dynamically maps URLs to ActionBeans.</description>
    <display-name>Stripes Dynamic Mapping Filter</display-name>
    <filter-name>DynamicMappingFilter</filter-name>
    <filter-class>
        net.sourceforge.stripes.controller.DynamicMappingFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>DynamicMappingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>
like image 26
stian Avatar answered Sep 23 '22 04:09

stian