[英]How to include external JS file in JSP file in Spring WebMVC framework?
我一直在Google上搜索,但找不到任何有用的...我的JS文件與WEB-INF處於同一級別..任何幫助將不勝感激...
以下是我用來在JSP中包含JS文件的代碼:-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/formValidation.js"></script>
下面的代碼在web.xml中:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
下面的代碼在dispatcher-servlet.xml文件中:
<context:component-scan base-package="com.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
您可能想要添加ResourceHandler
來解析您的靜態資源,例如js/css
目錄
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
此方法適用於java配置類extends WebMvcConfigurerAdapter
xml
版本應如下所示
<mvc:resources mapping="/js/**" location="/js/"/>
這將解析/webapp/js/
目錄下的任何.js
文件,使用.jsp
文件中的以下內容
<script src="js/custom.js"></script>
要獲得幫助,您可以查看此github鏈接,請單擊此處 ,希望對您有所幫助,這是項目結構
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.