Spring MVC how to create controller without return (String) view?

非 Y 不嫁゛ 提交于 2021-01-29 18:22:50

问题


The below is my sample controller.

@RequestMapping(value = "/validate", method = RequestMethod.POST)
public String validatePage1 (@ModelAttribute("page1")
                        Page1 pg1, BindingResult result) {

    System.out.println("Value1:" + pg1.getVal1() + 
                "Value2:" + pg1.getVal2());

    return "page2"; // I don't want to take any action (page navigation) here
}

@RequestMapping("/page1")
public ModelAndView pageShow() {

    return new ModelAndView("page1", "command", new Page1());
}

Now the question is, I don't want to take any action in the client side when the method (validatePage1) is called by Spring framework, how to do?

Actually I have loaded all required pages in my client side at loading time (to avoid repeated page load), so I dont want to take any page navigation action in the client side, I just want to do the 'data binding' to complete my business logic in server side.

When I return "" empty string in "validatePage1()", Spring framework throws exception " Request processing failed; nested exception is org.apache.tiles.definition.NoSuchDefinitionException:" since I am using tiles, I have to remove tiles configuration later since I am loading all files at first loading itself.


回答1:


Straight from the documentation:

Supported method return types

The following are the supported return types:

[...]

void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).




回答2:


You can set the method to return void and annotate it with @ResponseBody, as suggested here. No need to deal with HttpServletResponse etc.



来源:https://stackoverflow.com/questions/13781046/spring-mvc-how-to-create-controller-without-return-string-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!