Spring MVC(3.0)将带有和不带尾部斜杠的URL视为相同的URL.
例如:
http://www.example.org/data/something = http://www.example.org/data/something/
我需要使用尾部斜杠重定向URL
到没有它的URL:
我需要在应用程序内部执行此操作(因此不要通过Apache重写规则等).
一种方法是:
@ResponseStatus(value=HttpStatus.MOVED_PERMANENTLY)
@RequestMapping(value = "/data/something/")
public String dataSomethingRedirect(...) {
return "redirect:/data/something";
}
Run Code Online (Sandbox Code Playgroud)
但这通常有两个问题:
有没有办法拦截所有的URL,如果它们有一个尾部斜杠,将它们重定向到没有斜杠的相对URL?
我在春天有以下内容
@RequestMapping("/hello")
Run Code Online (Sandbox Code Playgroud)
但是Spring会自动为/ hello /和/hello.*添加映射.如何进行精确的URL匹配?
只有/你好应该工作,其他任何东西都应该404
我有一个像这样的spring MVC配置类:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题,使用尾部斜杠来管理URL,类似于此.所以我想添加RequestMappingHandlerMapping类,但基于我到达那里的指令,我需要扩展 WebMvcConfigurationSupport 类和实现requestMappingHandlerMapping()方法,但不幸的是我已经扩展了WebMvcConfigurationSupport类的资源映射.有什么办法可以为我的班级添加requiest映射处理程序吗?
注意:我使用的是Spring 3.1.1.RELEASE版