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

时间:2012-10-26 09:34:21

标签: 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 ModelAndView("/path/to/js/", "contextPath", contextPath);
}

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

1 个答案:

答案 0 :(得分:0)

您可以将您的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文件: 本地主机:8080 / yourapp /资源/ JS / somefile.js