0

我使用 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 资源设置视图解析器吗?

4

1 回答 1

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 文件:localhost:8080/yourapp/resources/js/somefile.js

于 2012-10-26T10:10:32.397 回答