external java scrpt files and css files are essential when developing a cool webapp. So how would you manage your files? As far as I know the best way is to put it under webapp folder. That way, all the js and css files will be included in the war file that you will generate. First create a folder named resources in your webapp folder. then put your js files in a folder called js. and css files in a folder called css. Now link your file in the jsp file like below.
<script src="<%=request.getContextPath()%>/resources/js/youfile.js" type="text/javascript">/script>
But still the dispatcher-servlet does not know how to link your file. It will try to get the file in the default manner (Through controller mapping if you use controllers). You need to put the following 2 lines to handle this problem.
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /><mvc:resources mapping="/resources/**" location="/resources/" />
This will easily link you js files. You can link css files in the same manner as well.
<script src="<%=request.getContextPath()%>/resources/js/youfile.js" type="text/javascript">/script>
But still the dispatcher-servlet does not know how to link your file. It will try to get the file in the default manner (Through controller mapping if you use controllers). You need to put the following 2 lines to handle this problem.
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /><mvc:resources mapping="/resources/**" location="/resources/" />
This will easily link you js files. You can link css files in the same manner as well.
No comments:
Post a Comment