2015-07-01 56 views
8

我正在学习从asp.net MVC来的Spring-MVC 4,我正在寻找一种方法将数据传递给View,而不必在每次调用中声明Model Atrribute。访问当前模型在spring-mvc

例如,现在我有这个。

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(); 
     } 
    } 
} 

然后,我加入一个拦截到的消息集合复制到型号:

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> 

这不是最佳的,但它的作品。

0

如果你想避免将模型作为方法参数,你可以在一个方法使用的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 
} 
+0

还有别的办法吗?正如你想象的那样,向模型中添加错误消息(例如)意味着需要从模型中检索一个列表,然后向其中添加东西。所以我真的需要访问模型实例 – tggm

+0

@TiagoMatias你在用AOP或类似的方式思考吗? –