@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.excbooks.controller")
public class ServletConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css");
registry.addResourceHandler("/js/**").addResourceLocations("/js");
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix("");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
和我的控制器
@Controller
public class MainController {
{
BasicConfigurator.configure();
}
@RequestMapping(value = "/d/login", method = RequestMethod.GET)
public String login(Model model){
return "log-in.html";
}
@RequestMapping(value = "/d/user", method = RequestMethod.GET)
public String userProfile(Model model){
User user = new User();
user.setId(new BigInteger("1"));
user.setUsername("Sashko");
model.addAttribute("user",user);
return "index.jsp";
}
}
链接到我的JSP索引 https://drive.google.com/file/d/0B42ezhAKqwZlcUEyVkR5amNIaDg/view?usp=sharing
答案 0 :(得分:1)
在你的JSP中,我怀疑javacsript / css文件大致类似于......
<link rel="stylesheet" href="css/my.css">
<script src="js/my.js"></script>
问题是href
和src
属性指定了相对URL。当网址为http://localhost:8080/user时,他们会正确指向您的文件,但当网址为http://localhost:8080/some-intermediate-node/user时,浏览器会查找...
分别
更改href
和src
属性,使其以正斜杠(/
)字符开头。
答案 1 :(得分:0)
将CSS和javascript href更改为/ css / **和/ js / **
例如:
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script src="/js/jquery-1.12.3.min.js"></script>