3

使用 Spring MVC,有没有办法分解org.springframework.ui.Model,以便不必在任何控制器的方法参数中指定它?

换句话说,我目前正在这样做:

public abstract class AbstractController {

    @Autowired
    protected MultipartHttpServletRequest request;

}

@Controller
public class SigninController extends AbstractController {

    @RequestMapping(value = "/signin", method = RequestMethod.GET)
    public String signin(@ModelAttribute User user, Model model) {
        // do stuff with user (parameter)
        // do stuff with model (parameter) <--
        // do stuff with request (attribute)
        return "/signin/index";
    }

}

我想这样做:

public abstract class AbstractController {

    @Autowired
    protected MultipartHttpServletRequest request;

    @Autowired
    protected Model model;

}

@Controller
public class SigninController extends AbstractController {

    @RequestMapping(value = "/signin", method = RequestMethod.GET)
    public String signin(@ModelAttribute User user) {
        // do stuff with user (parameter)
        // do stuff with model (attribute) <--
        // do stuff with request (attribute)
        return "/signin/index";
    }

}

但是在调用 URL 时,会抛出异常:

...Could not autowire field: protected org.springframework.ui.Model...
...No matching bean of type [org.springframework.ui.Model] found for dependency...

我在使用时遇到了同样的错误org.springframework.ui.ModelMap

有什么天才的解决方法吗?

感谢您的帮助:)

4

1 回答 1

1

我终于找到了解决方案。我不确定这款游戏是否值得,但它有效:)

首先,将这些内容添加到您的AbstractController:

public abstract class AbstractController {

    @Autowired
    protected MultipartHttpServletRequest request;

    protected ModelMap model;

    public void setModel(ModelMap model) {
        this.model = model;
    }

    public ModelMap getModel() {
        return model;
    }

}

然后,创建一个org.springframework.web.servlet.HandlerInterceptor像这样实现的拦截器:

public class UserContextInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
        if (handler instanceof AbstractController) {
            AbstractController controller = (AbstractController) handler;
            controller.setModel(new ModelMap());
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        if (handler instanceof AbstractController && modelAndView != null) {
            AbstractController controller = (AbstractController) handler;
            modelAndView.addAllObjects(controller.getModel());
        }
    }

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

}

最后,将这些行添加到您的applicationContext.xml

<mvc:interceptors>
    <bean class="path.to.my.just.created.UserContextInterceptor" />
</mvc:interceptors>

当然,让你的控制器实现你的AbstractController.

这里是!您不再需要在控制器方法参数中指定 yourrequest或 yourmodel了:) 虽然我并不真正相信这个技巧的有用性,但是是的。如果它能让疯狂的开发者更快乐:)

不过,仍然对更简单的解决方案持开放态度。

于 2012-04-25T21:55:46.340 回答