首页 > 编程 > Java > 正文

Spring boot 跳转到jsp页面的实现方法

2019-11-26 12:34:03
字体:
来源:转载
供稿:网友

本人正在学习Spring boot,搜索了很多关于Spring boot 跳转到jsp页面的实现方法介绍,下面我来记录一下,有需要了解的朋友可参考。希望此文章对各位有所帮助。

@Controller注解

1、application.properties文件中配置

# 配置jsp文件的位置,默认位置为:src/main/webappspring.mvc.view.prefix=/pages/# 配置jsp文件的后缀spring.mvc.view.suffix=.jsp

2、Controller文件中配置

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; @Controllerpublic class UserController {  @RequestMapping(value = "/index",method = RequestMethod.GET)  public String toIndex(){    return "index";  }}

3、启动项目App.Java中的【main】方法启动

4. 访问url,访问到jsp页面:http://localhost:8080/index

@RestController注解

1、application.properties文件中配置

# 配置jsp文件的位置,默认位置为:src/main/webappspring.mvc.view.prefix=/pages/# 配置jsp文件的后缀spring.mvc.view.suffix=.jsp

2、RestController文件中配置

import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.RestController;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod; @RestControllerpublic class UserController {  @RequestMapping(value = "/index",method = RequestMethod.GET)  public String toIndex(){    ModelAndView mv = new ModelAndView("index");      return mv;    }}

3、启动项目App.java中的【main】方法启动

4. 访问url,访问到jsp页面:http://localhost:8080/index

注意:application.properties中spring.mvc.view.prefix和spring.mvc.view.prefix属性,在spring-boot 老版本中和新版本中不一样:
老版本中用:

spring.view.prefix=/pages/ spring.view.suffix=.jsp

新版本中用:

spring.mvc.view.prefix=/pages/spring.mvc.view.suffix=.jsp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表