Spring mvc 如何为javascript资源设置SpringMVC视图解析器?

Spring mvc 如何为javascript资源设置SpringMVC视图解析器?,spring-mvc,Spring Mvc,我使用Spring MVC控制器生成javascript,如下所示: @RequestMapping(value="path/to/handler", method = RequestMethod.GET, produces ="text/javascript") public ModelAndView getJs(HttpServletRequest request) { String contextPath = request.getContextPath(); return new

我使用Spring MVC控制器生成javascript,如下所示:

@RequestMapping(value="path/to/handler", method = RequestMethod.GET, produces ="text/javascript")
public ModelAndView getJs(HttpServletRequest request) {
  String contextPath = request.getContextPath();
  return new ModelAndView("/path/to/js/", "contextPath", contextPath);
}

不过,我不知道如何为javascript资源设置视图解析器。有人可以建议如何为js资源设置视图解析器吗?

您可以将js文件放在

src/main/webapp/resources/js/somefile.js
将其添加到servlet-context.xml中

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources location="/resources/favicon.ico" mapping="/favicon.ico"/>

现在,您可以通过以下方式访问.js文件:
localhost:8080/yourapp/resources/js/somefile.js

您还可以映射favicon.ico和其他资源(如图像等),如上图所示……您的意思是说我从Spring生成js资源时不需要视图解析器吗?