Spring MVC 注解总是需要 Response 对象
我正在尝试将控制器从旧的继承框架转换为新的注释。
这是一个现有的控制器:
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'
因此它正在尝试查找视图但失败了。但是,没有视图可显示为 JQuery 的后端调用。
我想通过声明响应对象,Spring 认为我将编写响应。
如何阻止 Spring 尝试解析视图?
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 response) 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用 void 作为返回类型时,Spring 默认情况下会尝试从您的方法名称中确定视图名称,除非它认为您直接编写响应(当您有 HttpServletResponse 作为参数时,它会这样做)。查看 Spring 3 文档的第 15.3.2.3 节。
您可能想尝试将返回类型更改为 ModelAndView 并返回 null 并看看会发生什么(我不确定您是否可以使用 @RequestMapping 摆脱 null 视图,因为这不是我曾经尝试过的)
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)