我正在学习从asp.net MVC传入的Spring-MVC 4,我正在寻找一种方法将数据传递给View,而不必在每次调用中声明Model Atrribute。
例如,现在我有了这个。
public class BaseController {
public void AddMessage(Model model, String m) {
Model.addAttribute("msg", m);
}
}
public class PersonController extends BaseController{
@RequestMapping("details/{Id}")
public String details(@PathVariable int Id, Model model) {
Person p = service.LoadById(Id);
if(p == null) {
AddMessage(model, "Record not found...");
} else {
model.addAttribute("bean", q);
}
return "person/details";
}
}
但我真正想要的是有一种方法来访问我的基本控制器方法中的Model实例,而不必将其作为参数传递。类似于在asp.net MVC中使用ViewData或TempData。
是否可以以这种方式将数据传递给视图?
谢谢
答案 0 :(得分:1)
我设法使用请求拦截器解决此问题。基本上:
在我的基本控制器类上:
public abstract class BaseController {
protected List<UserViewMessage> viewMessages;
public List<UserViewMessage> getViewMessages() {
if (viewMessages == null) {
viewMessages = new ArrayList<UserViewMessage>();
}
return viewMessages;
}
public void addMessage(String message, UserViewMessageType type) {
getViewMessages().add(new UserViewMessage(message, type));
}
public void clearMessages() {
if (viewMessages != null) {
viewMessages.clear();
}
}
}
然后,我添加了一个拦截器来将消息集合复制到Model:
public class RequestInterceptor extends HandlerInterceptorAdapter {
private static String MODEL_MESSAGES_KEY = "ModelMessageList_";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (handler instanceof org.springframework.web.method.HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod != null) {
Object bean = handlerMethod.getBean();
if (bean != null && bean instanceof BaseController) {
BaseController bc = (BaseController) bean;
bc.clearMessages();
}
}
}
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if (handler instanceof org.springframework.web.method.HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod != null && modelAndView != null) {
Object bean = handlerMethod.getBean();
if (bean != null && bean instanceof BaseController) {
BaseController bc = (BaseController) bean;
if (bc.getViewMessages() != null) {
modelAndView.addObject(MODEL_MESSAGES_KEY, bc.getViewMessages());
}
}
}
}
super.postHandle(request, response, handler, modelAndView);
}
}
在PreHandle上,清除基本控制器集合上的所有消息。在请求(PostHandle)之后,由于模型可用,我将消息集合复制到模型中,从而使其在我的视图中可用,如下所示:
<div class="row">
<div class="col-lg-12">
<c:forEach var="messageItem" items="${_ModelMessageList_}">
<div class="alert alert-info"><c:out value="${messageItem.message}" /></div>
</c:forEach>
</div>
</div>
这不是最佳选择,但可行。
答案 1 :(得分:0)
如果要避免将Model作为方法参数传递,可以在方法中使用ModelAttribute注释: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ModelAttribute.html
只需注释该方法,Spring就会自动添加该方法返回给模型的内容。
@ModelAttribute
public Stuff addStuffToModel() {
Stuff stuff = new Stuff("dummy data");
return stuff; // stuff is added to the model
}