完美解决:springboot报错would dispatch back to the current handler URL [/appversion] again
would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an。。。。
记录记录
整了大半天 最后终于解决 得到了首页的访问!
- 任务分析
spring boot整合项目,实现页面控制跳转。
/**
* 来登录页
* @return
*/
@GetMapping(value = {"/","/login"})
public String loginPage(){
return "login1";
}
@PostMapping("/login")
public String main(User user, HttpSession session, Model model){ //RedirectAttributes
if(StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())){
//把登陆成功的用户保存起来
session.setAttribute("loginUser",user);
//登录成功重定向到main.html; 重定向防止表单重复提交
return "redirect:/main.html";
}else {
model.addAttribute("msg","账号密码错误");
//回到登录页面
return "login1";
}
}
登陆成功后自动来到首页localhost:8080/即上图的login页面(之前就感觉访问的接口名和文件名同为login会无法识别,后改成了login1.html) 还是报错,奔溃了!学了一年的Java还是无可奈何404!!
3. 原因解释
avax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
主要原因:读取映射地址混乱----和自己的想象果然相似! 还是在代码里报错
4. 最终解决办法
1).方法体加@ResponseBody
@ResponseBody的作用其实是将java对象转为json格式的数据。
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。(返回字符串可以使用)
2).加入thymeleaf依赖*(资源路径使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
完美解决
更多推荐
所有评论(0)