我用html创建了Spring启动应用程序,但收到错误 "在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);
}
}
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
在certificate.html中,只需调用java脚本及其角度。<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="app.js"></script>
错误详情:
找不到带有URI [/app.js]的HTTP请求的映射 DispatcherServlet,名称为&#39; dispatcherServlet&#39;
在chrome中,在Developer工具中,出现了这个错误:
无法加载资源:服务器响应状态为404()
任何人都可以帮忙吗?
答案 0 :(得分: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
,因为它们通过Boot禁用MVC自动配置。然后完全删除您的WebConfig
课程。
答案 1 :(得分:0)
虽然您已在WebConfig中添加了resourceHandlers,但我不相信您在jsp中正确引用了javascript。
试试这个:
<script src="/static/app.js"></script>
我一般都是这样做的:
<script src="${pageContext.request.contextPath}/static/app.js"></script>