开发者

Spring MVC annotations always requires Response object

开发者 https://www.devze.com 2023-03-12 15:03 出处:网络
I am trying to convert controllers from the old inheritance framework to the new annotations. Here\'s an existing controller:

I am trying to convert controllers from the old inheritance framework to the new annotations.

Here's an existing controller:

public class SelectedTabController extends AbstractController {

private TabSelectionHelper tabSelectionHelper;

public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String param = request.getParameter("selectedTab");
    if (param != null)
        tabSelectionHelper.setSelectedTabTo(param);
    return null;
}

public void setTabSelectionHelper(TabSelectionHelper tabSelectionHelper) {
    this.tabSelectionHelper = tabSelectionHelper;
}

And after conversion I have this:

@Controller
public class SelectedTabController {

    private TabSelectionHelper tabSelectionHelper;

    @Autowired
    public SelectedTabController(@Qualifier(value = "tabSelectionHelper") TabSelectionHelper tabSelectionHelper) {
        this.tabSelectionHelper = tabSelectionHelper;
    }

    @RequestMapping("/selectedTab")
    public void selectTab(String selectedTab, HttpServletResponse respo开发者_StackOverflow中文版nse) throws Exception {
        //String param = request.getParameter("selectedTab");
        if (selectedTab != null)
            tabSelectionHelper.setSelectedTabTo(selectedTab);
    }
}

This works but there is a (redundant) HttpServletResponse object in the selectTab paramter list. If I remove it, then the JQuery call says the server returns 500 and the call fails.

Any help?

The stacktrace shows:

javax.servlet.ServletException: Could not resolve view with name 'selectedTab' in servlet with name 'prodman'

So it is trying to find a view and failing. However, there is NO view to display as its a backend callby JQuery.

I guess by declaring the response object, Spring thinks I will write the response.

How can I prevent Spring from trying to resolve a view?


When you use void as your return type Spring will by default try to determine the view name from your method name, unless it thinks you're directly writing the response (which it does when you have a HttpServletResponse as a parameter). Have a look at section 15.3.2.3 of the Spring 3 docs.

You might want to try changing the return type to ModelAndView and return null and see what happens (I'm not certain you can get away with a null view with @RequestMapping as it's not something that I have ever tried)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号