Spring控制器返回双斜杠url
Spring 控制器在 URL 中创建双斜杠,为什么?
例如,下面的控制器将创建一个如下所示的 URL: “http://localhost/myapp//customer_home”,或者,如果登录失败, “http://localhost/myapp//login”
我正在使用 spring mvc 5.3.16 配置类标记如下,hello world level 最小。
@Controller
public class LoginController {
@ModelAttribute("login")
public Login setLogin() {
return new Login();
}
@GetMapping("/login")
public String login(Login login) {
return "login";
}
@PostMapping(value="/login")
public String login(@ModelAttribute("login") final Login login, final Model model) {
// check login
if (loginOK) {
return "redirect:customer_home";
} else {
model.addAttribute("error", "Login failed");
return "login";
}
}
public class SpringDispatcherConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
public class SpringDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
static {
HibernateUtil.buildSessionFactory("hibernate.cfg.xml");
new SocketServer().start();
}
@Override
protected Class <?> [] getServletConfigClasses() {
return new Class[] {
SpringDispatcherConfig.class
};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
Spring controller is creating double slashes in URL, why?
For example the controller below will create a URL that looks like:
"http://localhost/myapp//customer_home" or, if login failed,
"http://localhost/myapp//login"
I am using spring mvc 5.3.16
Configuration classes is tagged below, hello world level minimal.
@Controller
public class LoginController {
@ModelAttribute("login")
public Login setLogin() {
return new Login();
}
@GetMapping("/login")
public String login(Login login) {
return "login";
}
@PostMapping(value="/login")
public String login(@ModelAttribute("login") final Login login, final Model model) {
// check login
if (loginOK) {
return "redirect:customer_home";
} else {
model.addAttribute("error", "Login failed");
return "login";
}
}
public class SpringDispatcherConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
public class SpringDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
static {
HibernateUtil.buildSessionFactory("hibernate.cfg.xml");
new SocketServer().start();
}
@Override
protected Class <?> [] getServletConfigClasses() {
return new Class[] {
SpringDispatcherConfig.class
};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 application.property (myapp) 的上下文路径中删除斜杠。
remove the slash from the context path in application.property (myapp).