首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >严格的条形URLBinding

严格的条形URLBinding
EN

Stack Overflow用户
提问于 2016-04-12 14:24:29
回答 1查看 148关注 0票数 1

如何在条纹中实现严格的UrlBinding?对于我来说,严格的意思是,除了空事件之外,不应该有未定义事件的默认处理程序。

代码语言:javascript
复制
/hello -> DefaultHandler -> currentDate()
/hello/currentDate -> currentDate()
/hello/randomDate -> randomDate()
/hello/* -> DefaultHandler (I want a 404)

该问题也只存在于一个事件,因为它自动是一个默认处理程序。下面的代码摘自条纹书籍。

代码语言:javascript
复制
@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);
    }

}
EN

回答 1

Stack Overflow用户

发布于 2016-04-15 08:55:43

不确定这是否可以以一种更通用的‘Stripe’方式来完成(也许可以编写您自己的ActionResolver),但是这里有一个解决方案,它添加了一个defHandler集为@DefaultHandler (而不是currentDate)的方法。当没有给出currentDate的值时,这个添加的方法转发给$event,否则返回一个404 ErrorResolution

currentDaterandomDate事件的请求不会通过defHandler传递,因为它们有自己的处理程序方法。

代码语言:javascript
复制
@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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36576187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档