2016-09-20 55 views
1

我已经使用html创建了Spring引导应用程序,但出现错误 “未找到具有URI的HTTP请求的映射[/app.js在名为 '的DispatcherServlet'找不到具有名称为'dispatcherServlet'的DispatcherServlet中具有URI [/app.js]的HTTP请求的映射

项目结构的DispatcherServlet:

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); 
    } 
} 

Certif icateController.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

在certificate.html,只需调用Java脚本和它的角。

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

错误详细信息:

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

在Chrome中,在开发工具,得到这个错误:

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

任何人都可以在此帮助?

+0

我已经添加了下面的代码在Webconfig,但仍然得到错误。 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)configurer.enable(); } – BaskarNatarajan

+0

我错过了certificate.html代码,只是粘贴如下<!DOCTYPE HTML> <体NG-控制器=” PhoneListController “>

    {{phone.name}}

    {{电话。段}}

BaskarNatarajan

+0

我想,你只需要 “/” app.js.之前添加这样 reos

回答

0

您将多次覆盖自己的配置。

更改Application类来反映这一点:

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

删除所有@EnableWebMvc,因为通过引导这些禁用MVC自动配置。然后彻底删除你的WebConfig类。

0

虽然,您已经在您的WebConfig中添加了resourceHandlers,但我不相信您在jsp中正确地引用了javascript。

试试这个:

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

我通常做它虽然以下几点:

<script src="${pageContext.request.contextPath}/static/app.js"></script> 
相关问题