springboot如何通过controller层实现页面切换
作者:aiyongbo123456
在Spring Boot中,通过Controller层实现页面切换背景,Spring Boot的默认注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody会将返回值转换为字符串返回,因此无法实现页面切换,将@RestController换成@Controller
springboot通过controller层实现页面切换
背景
通过springboot完成一个web项目时,想像springmvc一样通过controller层实现页面切换。
实现方式
//return "dashboard";//这是由模板引擎解析,然后拼串 //return "forward:/dashboard.html";//这是转发 return "redirect:/main.html";//这是重定向
最终浏览器返回的只是字符串本身:redirect:/main.html。而无法实现页面切换。
各种百度,无法解决。最后才发现是因为springboot注解的原因。
springboot控制层默认注解是:@RestController ,而@RestController是@Controller 和 @ResponseBody的合体。
@ResponseBody会将返回值转换为字符串返回,所以得到的结果只能是字符串。
将@RestController 换成@Controller 后,就实现了页面的切换。
package com.zr.gktjweb.controller.login; import com.zr.gktjweb.common.HttpClientUtil; import com.zr.gktjweb.common.ResponseBean; import com.zr.gktjweb.constant.SysCode; import com.zr.gktjweb.model.SysUser; import com.zr.gktjweb.util.JSONUtils; import com.zr.gktjweb.util.UserUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @Api(tags = "登录") @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 = LoggerFactory.getLogger(LoginController.class); @ApiOperation(value = "登录", notes = "登录验证") @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "姓名", required = true, dataType = "String"), @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String") }) @RequestMapping(value = "/login.do", method = RequestMethod.POST) @ResponseBody public ResponseBean login(String username, String password) { String url = baseurl + loginUrl; Map<String, String> map = new HashMap<>(); map.put("username", username); map.put("password", password); String result = ""; //请求服务失败 try { result = HttpClientUtil.doPost(url, map); } catch (Exception e) { e.printStackTrace(); LOGGER.error("用户登录", e); return new ResponseBean(SysCode.errCode, "系统异常,请联系管理员", ""); } JSONObject json = JSONObject.fromObject(result); //登录验证未通过 if (!json.getString("code").equals(SysCode.successCode + "")) { ResponseBean responseBean = JSONUtils.jsonToObject(ResponseBean.class, result); return responseBean; } //验证通过,在session中设置token JSONObject dataJson = (JSONObject) json.get("data"); String userJson = dataJson.getString("user"); SysUser sysUser = JSONUtils.jsonToObject(SysUser.class, userJson); JSONObject tokenJson = (JSONObject) dataJson.get("token"); String token = tokenJson.getString("token"); UserUtil.setToken(token); UserUtil.setUserSession(sysUser); // session.setAttribute("token", token); // session.setAttribute("user", sysUser); String url1 = "/index.html"; Map<String, Object> resMap = new HashMap<>(); resMap.put("url", url1); return new ResponseBean(SysCode.successCode, "登录成功", resMap); } /** * 注销登录 * * @param request * @return */ @RequestMapping("/loginout.do") public String loginOut(HttpServletRequest request) { request.getSession().invalidate(); String url=baseurl+logoutUrl; HttpClientUtil.doGet(url); return "redirect:/"; } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。