Spring MVC - 重定向后无法加载视图



我有以下一个简单的控制器类,有两种方法:

@Controller
public class RegistrationController {
private RegistrationService registrationService;
@Autowired
public RegistrationController(RegistrationService registrationService){
this.registrationService = registrationService;
}
@RequestMapping(value = "/register", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
public String register(@RequestBody Customer customer) {
registrationService.registerCustomer(customer);
return "redirect:login";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
System.out.println("Login page");
return "login";
}
}

调度程序 servlet 视图解析器配置:

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

当我使用 json body 提交请求时/register它正确注册了新用户,然后它正确地重定向到第二种方法(在控制台中我可以看到"登录页面"(,但随后浏览器中的视图不会更改为login.jsp.当我简单地输入 localhost:8080/login 时,它会正确显示login.jsp页面。我也尝试过返回new ModelAndView("login"),但它也没有用。

选项 1: 尝试返回"重定向:/登录"而不是"重定向:登录">

选项 2:尝试为重定向提供完整的完整路径,理想情况下可以返回 redirect:/login,这将重定向到相对于您的 Web 应用程序根目录的路径 - http://example.com/appName/login

最新更新