本人正在學習Spring boot,搜索了很多關于Spring boot 跳轉到jsp頁面的實現方法介紹,下面我來記錄一下,有需要了解的朋友可參考。希望此文章對各位有所幫助。
@Controller注解
1、application.properties文件中配置
1
2
3
4
|
# 配置jsp文件的位置,默認位置為:src/main/webapp spring.mvc.view.prefix=/pages/ # 配置jsp文件的后綴 spring.mvc.view.suffix=.jsp |
2、Controller文件中配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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; @Controller public 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文件中配置
1
2
3
4
|
# 配置jsp文件的位置,默認位置為:src/main/webapp spring.mvc.view.prefix=/pages/ # 配置jsp文件的后綴 spring.mvc.view.suffix=.jsp |
2、RestController文件中配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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; @RestController public 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 老版本中和新版本中不一樣:
老版本中用:
1
2
|
spring.view.prefix=/pages/ spring.view.suffix=.jsp |
新版本中用:
1
2
|
spring.mvc.view.prefix=/pages/ spring.mvc.view.suffix=.jsp |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yanweihpu/article/details/53927892