有没有办法在 spring 控制器中将对象添加到没有 @ModelAttribute 的 Freemarker 模型
Is there a way, in a spring controller, to add objects to a Freemarker Model without @ModelAttribute
我将我的 MVC 视图解析器设置为正常的 Freemarker。但是我想在我的模型中添加一堆对象。
现在我知道我可以做这样的事情了:
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("user") User user) {
这将映射参数并创建一个添加到模板标记的用户对象。我知道我可以做到:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) {
我可以在其中添加几乎所有我想要的内容。但我的问题是我必须那样做吗?
我想知道是否有办法做这样的事情:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(HttpServletRequest req) {
MyContext myContext = new MyContext();
myContext.addStuff(stuff);
.... add more stuff
MagicViewObject.addModel(myContext);
return "freemarkerTemplate"
}
然后访问freemarker模板中的myContext对象。现在我知道我可能可以使用 @ModelAttribute("model") ModelMap 模型来做到这一点,但我的问题是:是否有另一种方法可以做到这一点。我不喜欢方法签名中的注释。我就是这么奇怪。
Return 一个 org.springframework.web.servlet.ModelAndView
对象。
我将我的 MVC 视图解析器设置为正常的 Freemarker。但是我想在我的模型中添加一堆对象。
现在我知道我可以做这样的事情了:
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("user") User user) {
这将映射参数并创建一个添加到模板标记的用户对象。我知道我可以做到:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) {
我可以在其中添加几乎所有我想要的内容。但我的问题是我必须那样做吗?
我想知道是否有办法做这样的事情:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(HttpServletRequest req) {
MyContext myContext = new MyContext();
myContext.addStuff(stuff);
.... add more stuff
MagicViewObject.addModel(myContext);
return "freemarkerTemplate"
}
然后访问freemarker模板中的myContext对象。现在我知道我可能可以使用 @ModelAttribute("model") ModelMap 模型来做到这一点,但我的问题是:是否有另一种方法可以做到这一点。我不喜欢方法签名中的注释。我就是这么奇怪。
Return 一个 org.springframework.web.servlet.ModelAndView
对象。