英文:
Page display using Spring mvc controller appends endpoint to page resources
问题
我有一个Spring MVC控制器,返回一个页面,像这样:
@Controller
@RequestMapping("/subscriber")
public class SubscriberWebController {
@GetMapping("/edit/{id}")
public String getSubscriber(@PathVariable("id") Long id, Model model) {
model.addAttribute("id", id);
return "subscriber/manage";
}
@GetMapping("/add")
public String addSubscriber() {
return "subscriber/manage";
}
}
问题是,当manage.html
加载时,资源是通过http://localhost:8081/subscriber/vendor/nucleo/css/nucleo.css
来加载的,而不是http://localhost:8081/vendor/nucleo/css/nucleo.css
。这导致加载所有页面资源时出现404错误。我似乎找不到我做错了什么。
编辑
我在我的ApplicationConfiguration
中有这个配置,以帮助提供所有静态CSS和JS文件。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(0);
}
然而,问题仍然存在。
英文:
I have a spring MVC controller that returns a page like this
@Controller
@RequestMapping("/subscriber")
public class SubscriberWebController {
@GetMapping("/edit/{id}")
public String getSubscriber(@PathVariable("id") Long id, Model model) {
model.addAttribute("id", id);
return "subscriber/manage";
}
@GetMapping("/add")
public String addSubscriber() {
return "subscriber/manage";
}
}
The problem is that when manage.html loads, assets are loaded using http://localhost:8081/subscriber/vendor/nucleo/css/nucleo.css rather than http://localhost:8081/vendor/nucleo/css/nucleo.css. This results in a 404 while loading all page resources. I cannot seem to find what I'm doing wrong.
EDIT
I have this configuration in my ApplicationConfiguration so as to help serve all static CSS and JS files.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(0);
}
Yet, the issue still persists
答案1
得分: 1
你没有说明在manage.html
内部如何引用你的CSS。
如果你想让你的CSS从/vendor/nucleo/css/
加载,那么请像这样设置你的CSS引用:
<link rel="stylesheet" href="vendor/nucleo/css/nucleo.css">
并且在你的厂商(vendor)包中添加映射:
<mvc:resources mapping="/vendor/**" location="/实际位置/" />
希望对你有所帮助。
英文:
You did not say how you reference your CSS inside your manage.html
In case you want your CSS to load from /vendor/nucleo/css/, then put your CSS reference like so:
<link rel="stylesheet" href="vendor/nucleo/css/nucleo.css">
and put a mapping to your vendor package:
<mvc:resources mapping="/vendor/**" location="/ACTUAL LOCATION/"/>
Hope this helps
答案2
得分: 1
参考了@eugen的上面的答案,我修改了我的web配置类,如下:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(0);
}
在我的HTML页面中,我这样引用静态资源:
<link rel="stylesheet" href="/vendor/nucleo/css/nucleo.css">
注意上面href属性中"vendor"之前的"/"。这解决了我的问题。
英文:
Taking a cue from @eugen's answer above, I modified my web configuration class thus:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(0);
}
and in my HTML pages, I reference static resources thus:
<link rel="stylesheet" href="/vendor/nucleo/css/nucleo.css">
Notice the "/" before vendor in the href attribute above. This resolves my issue
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论