Springboot realizes page switching through controller layer
background
When completing a web project through springboot, imagine using springmvc to achieve page switching through the controller layer.
Implementation method
//return "dashboard";//This is parsed by the template engine and then spelled the string//return "forward:/";//This is forwardingreturn "redirect:/";//This is a redirect
The final browser returns only the string itself: redirect:/. Page switching cannot be achieved.
Various Baidu cannot be solved. Finally, I found out that it was because of the springboot annotation.
The default annotation of springboot control layer is:@RestController , and @RestController is a combination of @Controller and @ResponseBody.
@ResponseBody will convert the return value to a string, so the result can only be a string.
After changing @RestController to @Controller, the page switching is implemented.
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import org.; import org.; import ; import ; import ; import ; import ; import ; import ; import ; @Api(tags = "Log in") @Controller public class LoginController { @Value("${baseurl}") private String baseurl; @Value("${login_url}") private String loginUrl; @Value("${logout_url}") private String logoutUrl; private static final Logger LOGGER = (); @ApiOperation(value = "Log in", notes = "Login Verification") @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "Name", required = true, dataType = "String"), @ApiImplicitParam(name = "password", value = "password", required = true, dataType = "String") }) @RequestMapping(value = "/", method = ) @ResponseBody public ResponseBean login(String username, String password) { String url = baseurl + loginUrl; Map<String, String> map = new HashMap<>(); ("username", username); ("password", password); String result = ""; //Request service failed try { result = (url, map); } catch (Exception e) { (); ("User Login", e); return new ResponseBean(, "System exception, please contact the administrator", ""); } JSONObject json = (result); //Log in verification failed if (!("code").equals( + "")) { ResponseBean responseBean = (, result); return responseBean; } //Verify and set token in session JSONObject dataJson = (JSONObject) ("data"); String userJson = ("user"); SysUser sysUser = (, userJson); JSONObject tokenJson = (JSONObject) ("token"); String token = ("token"); (token); (sysUser); // ("token", token); // ("user", sysUser); String url1 = "/"; Map<String, Object> resMap = new HashMap<>(); ("url", url1); return new ResponseBean(, "Login successfully", resMap); } /** * Log out * * @param request * @return */ @RequestMapping("/") public String loginOut(HttpServletRequest request) { ().invalidate(); String url=baseurl+logoutUrl; (url); return "redirect:/"; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.