我对java spring mvc有一个奇怪的问题。当用户想要“localhost:8080 / admin”时,一切正常,但当用户想要“localhost:8080 / admin / create”时,所有CSS和JS文件都将丢失。
@Controller
@RequestMapping(value="/admin")
public class AdminController {
@Autowired
private JobService jobService;
@RequestMapping(path="")
public String index(){
return "admin";
}
@RequestMapping(path = "/create", method = RequestMethod.GET)
public String create(){
return "create";
}
@RequestMapping(path = "/create", method = RequestMethod.POST)
public @ResponseBody String create(@RequestParam String title, @RequestParam String description,@RequestParam int personQuantity, @RequestParam String lastApp) {
Job newJob = new Job();
newJob.setJobTitle(title);
newJob.setJobDescription(description);
newJob.setNumberOfPersonToHire(personQuantity);
newJob.setLastApplicationDate(lastApp);
jobService.create(newJob);
return "admin";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Kodgemisi-HR-Application-master</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
我找到了解决方案
<link href="/css/index.css" rel="stylesheet"></link>
<link href="/css/bootstrap.min.css" rel="stylesheet"></link>
<script src="/js/jquery-3.2.1.js"></script>
<script src="/js/bootstrap.min.js"></script>
添加/写入html
内的js和css路径Spring mvc: css does not work when adding slash at the end of URL