【发布时间】:2018-05-13 13:27:12
【问题描述】:
我有以下简单的 Controller 类,它有两种方法:
@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";
}
}
Dispatcher 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>
当我在/register 上提交带有 json 正文的请求时,它会正确注册新用户,然后它会正确重定向到第二种方法(在控制台中我可以看到“登录页面”),但是浏览器中的视图没有更改为login.jsp。当我简单地输入 localhost:8080/login 时,它会正确显示 login.jsp 页面。我也试过返回new ModelAndView("login"),但也没有用。
【问题讨论】:
-
property name="prefix">
/ 这就是为什么你的页面没有重定向.. 根据你的 viewResolver 您要求它在 / 目录中搜索以 .jsp 为后缀的 jsp 文件。但我猜。您的 jsp 文件位于 webapps/WEB-INF/jsp 之类的文件夹中。很快.. 更改前缀目录。 -
不,jsp 在 / 文件夹中,因此 localhost:8080/login 正确显示 login.jsp。
-
你测试过"redirect:/login"; ??
-
是的,我做到了。奇怪的是,它进入了登录方法,却没有改变浏览器中的视图。
标签: java spring spring-mvc controller