关于springboot 从controller指定跳转到html页面

本文介绍了在SpringBoot环境下,使用ibeetl模板引擎和Mybatis Plus时,从Controller如何正确跳转到HTML页面的问题。通过分析@RestController与@Controller的区别,解决了返回字符串而非跳转页面的错误。通过修改注解,成功实现了页面跳转。
摘要由CSDN通过智能技术生成

web后台管理系统
环境:springboot+idea+ibeetl(模版引擎)+mybatis plus(包含在kernel-core包中)+mysql。。。
开始访问跳转到登录页面:
链接:http://127.0.0.1:8084/login
控制层接口代码:

@RestController
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 跳转到登录页面
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:41 PM
     */
    //@GetMapping(value = "/login")
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login() {
        if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) {
            return REDIRECT + "/";
        } else {
            return "/login.html";
        }
    }
 }

出现的问题是它没有跳到login.html页面,而是返回字符串“login.html”

要在SpringBoot中实现跳转到静态HTML页面,可以使用控制器(Controller)的方式来实现。具体的实现步骤如下: 1. 在SpringBoot项目的resources文件夹下创建一个static文件夹,用来存放静态HTML文件。 2. 在控制器中添加一个请求映射(RequestMapping)注解,并指定需要跳转的静态HTML页面路径。 3. 在方法体中使用ModelAndView返回静态HTML页面的名称。 示例代码如下: ```java @Controller public class HtmlController { @RequestMapping("/index") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index.html"); return modelAndView; } } ``` 在上面的代码中,我们定义了一个控制器类HtmlController,并在其中添加了一个请求映射注解@RequestMapping("/index"),表示当访问http://localhost:8080/index时,将会执行index()方法。 在index()方法中,我们使用ModelAndView类来指定需要跳转的静态HTML页面名称,即modelAndView.setViewName("index.html")。 最后返回ModelAndView对象即可。 需要注意的是,SpringBoot默认情况下会将静态文件放置在classpath下的static目录中,如果需要更改静态文件的存放位置,可以在application.properties中添加以下配置: ```properties # 指定静态文件存放位置 spring.resources.static-locations=classpath:/static/,file:/usr/local/static/ ``` 上面的配置表示将静态文件放置在classpath下的static目录和本地磁盘的/usr/local/static/目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值