0
    WEB-INF
     |
     +----static
           |
           +---- angular
                 |
                 +----angular.min.js
           +---- css       
           |
           +---- html
                 |
                 +----index.html

dispacher-servlet.xml contents are as below

<mvc:annotation-driven/>

<mvc:default-servlet-handler/>

<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
<mvc:resources mapping="/static/html/**" location="/WEB-INF/static/html/" />
<mvc:resources mapping="/static/angular/**" location="/WEB-INF/static/angular/" />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="order" value="1" />
      <property name="mediaTypes">
        <map>
           <entry key="json" value="application/json" />
           <entry key="xml" value="application/xml" />
           <entry key="rss" value="application/rss+xml" />
            <entry key="html" value="text/html"/>                
            <entry key="js" value="text/javascript"/>
        </map>
      </property>

    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
        </list>
    </property>
</bean>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="2" />
 </bean>

content of web-inf/static/html/index.html is as below

<!DOCTYPE html>
<html ng-App>
<head>
    <script src="angular.min.js"></script>
</head>
<body>
<h2>Hello World!</h2>
<input type="text" ng-model="name"/>
Welcome {{name}}

MyController class is as below

public class MyController {
    @RequestMapping(value = "/service/greeting/{name}", method = RequestMethod.GET)
     public String getGreeting(@PathVariable String name) {
      String result="Hello "+name; 
          return result;
     }

@RequestMapping(value="/")  
public ModelAndView showHomePage(ModelAndView mv ) {
    mv.setViewName("/static/html/index.html");
    return mv;
 }
}

index page is up but what could be the issue with angular js which is showing GET 404 error in web console of browser?

Akhil
  • 217
  • 2
  • 3
  • 7

1 Answers1

1

Try this:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script src="<c:url value="/static/angular/angular.min.js"/>"></script>

Update:

Check this post if you are using HTML instead of JSP --> org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI

As per this post you need to add additional tag to escape the HTML files also like this:

<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />

<mvc:resources mapping="/WEB-INF/pages/**" location="/WEB-INF/pages/"/>

assuming your HTML files are in 'WEB-INF/pages' directory. Since they are HTML files you can directly import the JS files like this:

<script src="static/angular/angular.min.js"></script>
Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • I am not on JSP but HTML. Also my whole page is not showing up. It is not just this tag, i am getting 404 for welcome file. – Akhil Jul 24 '15 at 13:23
  • @Akhil, Can you add complete error details to your question, as per your post you just say you have issue with JS file. – Chaitanya Jul 24 '15 at 13:51
  • @Akhil, also look at my update, see if that fixes your issue. – Chaitanya Jul 24 '15 at 13:55
  • my request is going to dispatcher and from there it is going to a controller.... I am able get a response with view other than home page. But Sad thing is I am only able to push a string... how do I have a controller/dispatcher servlet to render my index.html. If you are asking about error... then my default home-page or index.html page is showing 404. My index.html is in webapps... which is at equal level to WEB-INF. that is outside WEB-INF – Akhil Jul 24 '15 at 14:21
  • I made some change like returning ModelAndView from my method and able to view the page now. However for angular js part in debug it is showing url as - http://localhost:8080/angular.min.js 404 error.... why it is not appending my project name or context root? – Akhil Jul 24 '15 at 15:17
  • @Akhil, the question in your post is misleading, it says the issue is with JS not index.html. Any way, have you tried my updated answer? It is about fixing your index.html file only, I have tried on my machine by placing index.html & angular.js and it is working fine. If you still having issues then please share your complete setup, you are not showing what is your spring configuration file and what your controller is returning. – Chaitanya Jul 24 '15 at 15:17
  • Contorller @RequestMapping(value="/") public ModelAndView showHomePage(ModelAndView mv ) { mv.setViewName("/static/html/index.html"); return mv; } – Akhil Jul 24 '15 at 15:20
  • Servlet - – Akhil Jul 24 '15 at 15:21
  • @Akhil, Add more details so I can reproduce your issue. The information given is not enough. Please edit your question don't comment here. – Chaitanya Jul 24 '15 at 15:21