2

I have created Spring boot application with html but getting error "No mapping found for HTTP request with URI [/app.js] in DispatcherServlet with name 'dispatcherServlet'

Project Structure:

TeamTrack
    --Src
       --Main
          --java
             --com.tm.controller
                  --application
                  --CertificateController
                  --WebConfig
          --resources
             ---static
                --app.js
       --Test

Application.java:

@SpringBootApplication
@EnableAutoConfiguration
@EntityScan("com.tm.vo")
@EnableJpaRepositories("com.tm.repository")
@EnableWebMvc
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

CertificateController.java

@EnableWebMvc
@Controller
public class CertificateController {

    @Autowired
    private certificateRepository certRep;

    @RequestMapping(value = "/cert",method = RequestMethod.GET)
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "certificate";
    }

    @RequestMapping(value = "/addCertificate", method = RequestMethod.POST)
    public String addNewCertificate(@RequestParam String certName,@RequestParam String certProvider)
    {
        certificate cert =new certificate();
        cert.setCertificateName(certName);
        cert.setCertificateProvider(certProvider);

        certRep.save(cert);
        return "/";
    }
}

WebConfig.java

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/static/", "classpath:/static/css/", "classpath:/static/js/" };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/static/**")) {
            registry.addResourceHandler("/static/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }
    }
}

certificate.html

in the certificate.html , just calling the java script and its angular.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="app.js"></script>

Error Details:

No mapping found for HTTP request with URI [/app.js] in DispatcherServlet with name 'dispatcherServlet'

In chrome, In the Developer tool, got this error:

Failed to load resource: the server responded with a status of 404 ()

can anyone help on this ?

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • I have added the below code in the Webconfig but still getting the error. @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } – BaskarNatarajan Sep 20 '16 at 15:16
  • I missed certificate.html code and just pasted below
    • {{phone.name}}

      {{phone.snippet}}

    – BaskarNatarajan Sep 20 '16 at 15:18
  • I think, you just need to add "/" before app.js. In this way – reos Sep 20 '16 at 16:58

2 Answers2

0

You're overwriting your own configuration multiple times.

Change your Application class to reflect this:

@SpringBootApplication
@EntityScan("com.tm.vo")
@EnableJpaRepositories("com.tm.repository")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Remove all @EnableWebMvc, since those disable MVC auto-configuration by Boot. Then completely delete your WebConfig class.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
0

Although, you've added the resourceHandlers in your WebConfig, I don't believe you're referencing the javascript correctly in your jsp.

try this:

<script src="/static/app.js"></script>

I've generally done it the following though:

<script src="${pageContext.request.contextPath}/static/app.js"></script>
Tony Card
  • 2,084
  • 3
  • 20
  • 27