如何在条纹中实现严格的UrlBinding?对于我来说,严格的意思是,除了空事件之外,不应该有未定义事件的默认处理程序。
/hello -> DefaultHandler -> currentDate()
/hello/currentDate -> currentDate()
/hello/randomDate -> randomDate()
/hello/* -> DefaultHandler (I want a 404)该问题也只存在于一个事件,因为它自动是一个默认处理程序。下面的代码摘自条纹书籍。
@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);
}
}发布于 2016-04-15 08:55:43
不确定这是否可以以一种更通用的‘Stripe’方式来完成(也许可以编写您自己的ActionResolver),但是这里有一个解决方案,它添加了一个defHandler集为@DefaultHandler (而不是currentDate)的方法。当没有给出currentDate的值时,这个添加的方法转发给$event,否则返回一个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);
}https://stackoverflow.com/questions/36576187
复制相似问题