原本打算這篇繼續寫thymeleaf方面的內容,一看內容還挺多的,可能一周也寫不完,而且從controller獲取值等內容也都能從網上百度,所以就寫了springboot集成jsp。不管thymeleaf還是jsp其實都是分層思想的體現。
一、引入依賴
還是用上一博客的demo,在它基礎上進行修改,這次是集成jsp,所以要先引入jsp的依賴。這里需要把上一博客加的thymeleaf去掉。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <scope>provided</scope> </dependency> |
二、創建jsp頁面
既然是集成jsp,肯定少不了jsp頁面,這里我把jsp頁面login.jsp放在了/demo/src/main/webapp/view下。在jsp中獲取controller中的一個變量值。
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>insert title here</title> </head> <body> 姓名:${name}<br> </body> </html> |
三、配置
在application.properties中配置view的前綴后綴。
1
2
3
|
spring.mvc.view.prefix=/view/ spring.mvc.view.suffix=.jsp |
四、創建controller
在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.example.demo; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller @requestmapping ( "/login" ) public class login { @requestmapping (value = "/login.do" ,method = requestmethod.get) public string hello(model model) { model.addattribute( "name" , "cuiyw" ); return "login" ; } } |
五、測試
輸入http://localhost:8080/login/login.do
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/5ishare/p/9251412.html