0

My directory structureI have tried below answer but it didn't work..

How include an external JS file in a JSP page

I have been searching on google but couldn't find anything useful...my JS file is at same level as that of WEB-INF..any help would be appreciated...

Below is the code that I'm using to include my JS file in JSP :-

<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>

below code is in 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>

Below code is in dispatcher-servlet.xml file :-

<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>
Shandilya
  • 73
  • 9
  • 2
    what do you mean by `external` - In my thing an example of an external js file would be `` – Scary Wombat Nov 13 '18 at 00:15
  • If this is not what you are asking, then show an example of your code and your directory structure (as text) – Scary Wombat Nov 13 '18 at 00:18
  • Give a screenshot of your project structure and post code where you are trying to link js file ! – Avijit Barua Nov 13 '18 at 04:47
  • I have just now added ss of project structure and code that I have written to include my JS file. – Shandilya Nov 13 '18 at 08:30
  • @ScaryWombat...external file means I have written jquery code in a different file and I'm trying to reference that file in Jsp file...I have tried writing inline Jquery code in Jsp and its working fine... – Shandilya Nov 13 '18 at 08:34

2 Answers2

1

You might want to add ResourceHandler to resolve your static resources like js/css directory

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
        .addResourceHandler("/js/**")
        .addResourceLocations("/js/")
        .setCachePeriod(3600)
        .resourceChain(true)
        .addResolver(new PathResourceResolver());
}

This method apply for java configuration class extends WebMvcConfigurerAdapter

The xml version should look like this

<mvc:resources mapping="/js/**" location="/js/"/>

This will resolve any .js file under /webapp/js/ directory, using something like below in .jsp file

<script src="js/custom.js"></script>
nguyentaijs
  • 160
  • 8
  • can you please have a look at my directory structure once and my code and then let me know where to include this method? – Shandilya Nov 13 '18 at 08:35
  • You should create `resources` directory under `webapp` to store all static resources like `resources/js/` `resources/css/` then just simply set `` for all of them – nguyentaijs Nov 13 '18 at 08:59
0

for help you can see this github link click here I hope it will help you and this is the project structure

enter image description here

maddy
  • 129
  • 1
  • 4