-1

我正在覆盖属于超类的方法。方法不带参数。因此,我无法使用模型将对象传递给视图。有人有什么建议吗?

@Override
protected String connectView(){
    // I'd like to include an object in Model here
    // e.g. model.addAttribute(....) 
    // but unpossible because super does not take a Model as param
    return "connect/status";
}
4

2 回答 2

1

至少没有请求对象,我不相信你可以简单地做你所要求的。但是,有几个选项:

  1. 使用 servlet 过滤器...将所需的值添加到那里的会话中
  2. 使用装饰器模式,您将在另一个包装器类中创建您的类(在本例中为控制器)的实例。在您的包装器中,您将进行额外的处理,然后调用包装的内部对象(您的包装控制器),然后进行任何最终处理。
  3. HandlerInterceptor like @sp00m 建议(虽然我从未使用过它,所以我没有输入)

我敢肯定那里可能还有其他一些选择,但我想不出其他任何选择。

于 2013-01-23T15:48:08.130 回答
0

这应该适合您的需要,使用HandlerInterceptor自定义注释@Model和反射。

模型.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Model {

}

请求拦截器.java

@Service
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
        try {
            Class<?> clazz = request.getClass();
            if (clazz.isAnnotationPresent(Controller.class)) {
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(Model.class)) {
                        field.set(request, new ModelMap());
                        break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            // log("Cannot access model field of controller " + clazz.getSimpleName());
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        try {
            Class<?> clazz = request.getClass();
            if (clazz.isAnnotationPresent(Controller.class)) {
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(Model.class)) {
                        ModelMap model = (ModelMap) field.get(request);
                        if (model != null) {
                            modelAndView.addAllObjects(model);
                        }
                        break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            // log("Cannot access model field of controller " + clazz.getSimpleName());
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

}

应用程序上下文.xml

<!-- register the interceptor -->
<mvc:interceptors>
    <bean class="your.package.to.the.RequestInterceptor" />
</mvc:interceptors>

你的控制器.java

@Controller
public class YourController extends ConnectController {

    @Model
    private ModelMap model;

    @Override
    protected String connectView(){
        // model is here available
        model.addAttribute("attrName", "attrValue");
        return "connect/status";
    }

}
于 2013-01-23T16:23:58.267 回答