2016-04-12 22 views
1

厳密にストライプでUrlBindingを実現できますか? 厳密は、空のイベント以外の未定義イベントのデフォルトのハンドラがないことを意味します。ストライプの厳密なURLバインド

/hello -> DefaultHandler -> currentDate() 
/hello/currentDate -> currentDate() 
/hello/randomDate -> randomDate() 
/hello/* -> DefaultHandler (I want a 404) 

問題は、自動的にデフォルトのハンドラであるため、1つのイベントにのみ存在します。 以下のコードは、Stripes本から取られています。

@UrlBinding("/hello/{$event}") 
public class HelloActionBean implements ActionBean { 

    private static final String VIEW = "/WEB-INF/jsp/hello.jsp"; 
    private ActionBeanContext ctx; 

    public ActionBeanContext getContext() { 
     return ctx; 
    } 

    public void setContext(ActionBeanContext ctx) { 
     this.ctx = ctx; 
    } 

    private Date date; 
    public Date getDate() { 
     return date; 
    } 

    @DefaultHandler 
    public Resolution currentDate() { 
     date = new Date(); 
     return new ForwardResolution(VIEW); 
    } 

    public Resolution randomDate() { 
     long max = System.currentTimeMillis(); 
     long random = new Random().nextLong() % max; 
     date = new Date(random); 
     return new ForwardResolution(VIEW); 
    } 

} 

答えて

2

ない、これはより一般的な「スジ」の方法(おそらくあなた自身のActionResolverを書く)で行うことができますが、ここで(代わりにcurrentDateの)@DefaultHandlerような方法defHandlerセットを追加し、回避策 だか確認します。この追加されたメソッドは、$eventの値が指定されていない場合はcurrentDateに、そうでない場合は404 ErrorResolutionを返します。

イベントはを通過しません。これは、独自のハンドラメソッドがあるためです。currentDateおよびrandomDateイベントは、defHandlerを通過しません。

@DefaultHandler 
public Resolution defHandler() { 

    // The request uri 
    Path path = Paths.get(getContext().getRequest().getRequestURI()); 

    // Get the action name from the @UrlBinding value 
    Path action = Paths.get(this.getClass().getAnnotation(UrlBinding.class).value().split("/\\{")[0]); 

    // no event specified -> currentDate 
    if (path.getFileName().equals(action.getFileName())) return currentDate(); 

    // unknown event specified -> 404 
    else return new ErrorResolution(404); 
}