2012-10-26 74 views
0

我使用Spring MVC的控制器如下產生的javascript:如何爲JavaScript資源設置Spring MVC視圖解析器?

@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資源的視圖解析器?

回答

0

可以放置在

src/main/webapp/resources/js/somefile.js 

您的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/resources/js/somefile.js

+0

您還可以映射favicon.ico和其他資源(如圖像等),如上所示... –

+0

您的意思是說我不需要視圖解析器當我從Spring生成一個JS資源? – balteo