java - Spring MVC 注解总是需要 Response 对象

标签 java spring-mvc annotations

我正在尝试将 Controller 从旧的继承框架转换为新的注释。

这是一个现有的 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;
}

转换后我有这个:

@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 response) throws Exception {
        //String param = request.getParameter("selectedTab");
        if (selectedTab != null)
            tabSelectionHelper.setSelectedTabTo(selectedTab);
    }
}

这可行,但 selectTab 参数列表中有一个(冗余)HttpServletResponse 对象。如果我删除它,那么 JQuery 调用会说服务器返回 500 并且调用失败。

有什么帮助吗?

堆栈跟踪显示:

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

所以它试图找到一个 View 但失败了。但是,没有 View 可显示为 JQuery 的后端调用。

我想通过声明响应对象,Spring 认为我将编写响应。

如何阻止 Spring 尝试解析 View ?

最佳答案

当您使用 void 作为返回类型时,Spring 默认情况下会尝试从您的方法名称中确定 View 名称,除非它认为您直接编写响应(当您有 HttpServletResponse 作为参数时,它会这样做)。查看 Spring 3 文档的第 15.3.2.3 节。

您可能想尝试将返回类型更改为 ModelAndView 并返回 null 并看看会发生什么(我不确定您是否可以使用 @RequestMapping 摆脱 null View ,因为这不是我尝试过的东西)

关于java - Spring MVC 注解总是需要 Response 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6343727/

相关文章:

java - 如何在 mongoDB 中使用吗啡获取累加器结果?

java - Jar 无法加载包含类文件的文件夹

java - 排除 Spring MVC Controller 的请求映射中的某些路径

javascript - 打开注释器(注释存储在哪里?)

java - 如何分配多维输出缓冲区来提供 Android Tflite 的 interpreter.run()?

java - 使用 sourceforge spnego 针对 Activity 目录进行 SSO 身份验证

java - 在 Spring Controller 中加载静态文件

java - Spring 工件DescriptorException

java - JVM什么时候加载注解类

spring - 在 JUnit 测试中的 MockHttpServletRequest 中设置 @ModelAttribute