简体   繁体   English

Spring无法解析servlet中的视图

[英]Spring cannot resolve view within servlet

I've added a new method/mapping to one of my servlets: 我已经为我的一个servlet添加了一个新的方法/映射:

@RequestMapping(value = "/user/prefs/order", method = RequestMethod.POST)
public void updateUsersPrefs(@RequestBody Map<String, ArrayList> body, HttpServletRequest request) {
    ...
}

but when I send a request to this url I get a 500 Internal Server Error, with the following error message: 但是当我向此URL发送请求时,我收到500内部服务器错误,并显示以下错误消息:

javax.servlet.ServletException: Could not resolve view with name 'user/prefs/order' in servlet with name 'appfinder'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

I cannot for the life of me see why this is being reported. 我不能为我的生活看到为什么这是报道。 Can anyone help with this? 有人能帮忙吗? I there is anymore information that I could provide, please let me know. 我已经提供了更多信息,请告诉我。

Thanks! 谢谢!

Spring treats @RequestMapping methods with void return type in the following manner : Spring 以下列方式处理带有void返回类型的@RequestMapping方法:

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). void - 如果方法处理响应本身(通过直接编写响应内容,为此目的声明类型为ServletResponse / HttpServletResponse的参数)或者是否应该通过RequestToViewNameTranslator隐式确定视图名称(不声明响应参数)处理程序方法签名)。

Therefore since there is no HttpServletResponse parameter to this method, Spring assumes that you would like the view name to be determined through a RequestToViewNameTranslator . 因此,由于此方法没有HttpServletResponse参数,因此Spring假定您希望通过RequestToViewNameTranslator确定视图名称。

If you do not specify a particular RequestToViewNameTranslator to use in your context, then the default implementation kicks in which will : 如果您没有指定要在您的上下文中使用的特定RequestToViewNameTranslator ,那么默认实现将启动

simply transforms the URI of the incoming request into a view name. 只需将传入请求的URI转换为视图名称即可。

If you don't want the URI of the incoming request to be used as the view name, you have a few options: 如果您不希望将传入请求的URI用作视图名称,则可以使用以下几个选项:

  1. Configure a custom RequestToViewNameTranslator with the behavior you would like 使用您想要的行为配置自定义RequestToViewNameTranslator
  2. Add a HttpServletResponse parameter to this method if you would like to write to the response directly rather than View resolution take place. 如果要直接写入响应而不是View查询解析,请向此方法添加HttpServletResponse参数。
  3. Change the return type of this method to String , View, or ModelAndView` to be able to specify the view or view name within the method. 将此方法的返回类型更改为StringView, or ModelAndView`,以便能够在方法中指定视图或视图名称。

I had this problem, and the reason was I was using tiles framework and did not mention the view name in tiles-def.xml. 我有这个问题,原因是我使用的是tile框架,并没有在tiles-def.xml中提到视图名称。 After configuring tiles-def.xml had the problem solved. 配置完tile-def.xml之后问题就解决了。

I also had this problem. 我也有这个问题。 I solved it by using the @ResponseBody annotation. 我使用@ResponseBody注释解决了它。

Like this: 像这样:

@RequestMapping(value = "/user/prefs/order", method = RequestMethod.POST)
@ResponseBody
public void updateUsersPrefs(@RequestBody Map<String, ArrayList> body, HttpServletRequest request) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 无法解析 JSP 查看 - Spring cannot resolve JSP view 无法解析名为&#39;spring&#39;的servlet中名为&#39;streamrecords&#39;的视图 - Could not resolve view with name 'streamrecords' in servlet with name 'spring' 使用FreeMarker的Spring:无法解析名称为“ MyServlet”的servlet中名称为“ Home”的视图 - Spring with FreeMarker: Could not resolve view with name 'Home' in servlet with name 'MyServlet' 无法解析符号'servlet' - cannot resolve symbol 'servlet' 无法解析MVC View Spring MVC 3 - Cannot resolve MVC View Spring MVC 3 spring boot 无法解析视图页面 - Spring boot cannot resolve view page Servlet 应该有映射,不能解析 Servlet - Servlet should have a mapping and cannot resolve Servlet Spring Security抛出javax.servlet.ServletException:无法解析名称为&#39;j_spring_security_check&#39;的视图 - Spring security throws javax.servlet.ServletException: Could not resolve view with name 'j_spring_security_check' Java、Spring、Apache 平铺错误:无法解析名称为“spring”的servlet中名称为“contact”的视图 - Java, Spring, Apache Tiles error : Could not resolve view with name 'contact' in servlet with name 'spring' Spring 引导无法解析 Eclipse 内的属性占位符 - Spring boot cannot resolve property placeholder within Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM