'Spring controller returns double slashes url
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[] {
"/"
};
}
}
Solution 1:[1]
remove the slash from the context path in application.property (myapp).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nawress Rafrafi |
