本文介紹了springboot 中使用jsp的方法示例,分享給大家,具體如下:
依賴:
1
2
3
4
5
6
7
8
9
10
11
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 1 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> |
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@requestmapping (value = "/register" , method = requestmethod.get) @responsebody public string register(){ return "user register" ; } /** @getmapping 是spring 4.3 的新特性 */ @getmapping ( "getuser" ) @responsebody public string getuser(){ return "user get" ; } /** @postmapping 也是spring 4.3 的新特性 */ @postmapping ( "createuser" ) @responsebody public string createuser(){ return "user create" ; } /** * @requestparam 接收提交的參數(shù),參數(shù)默認(rèn)是必填的 * @requestparam(value = "password", required = false) required = false,可以不是必填的參數(shù) * */ @postmapping ( "builduser" ) @responsebody public string builduser( @requestparam ( "username" ) string username, @requestparam (value = "password" , required = false ) string password){ return "提交的參數(shù):username" + username + " password:" + password; } |
在springboot中使用jsp
springboot默認(rèn)不支持jsp,需要在項(xiàng)目中添加相關(guān)的依賴
1
2
3
4
5
6
7
8
9
10
11
|
<dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> </dependency> <dependency> <groupid>org.eclipse.jdt.core.compiler</groupid> <artifactid>ecj</artifactid> <version> 4.6 . 1 </version> <scope>provided</scope> </dependency> |
配置文件增加配置項(xiàng):
1
2
|
spring.mvc.view.prefix=/web-inf/views/ spring.mvc.view.suffix=.jsp |
login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@controller public class logincontroller { @postmapping ( "login" ) public string login(string username, string password){ if (username.equals(password)){ return "list" ; } return "login" ; } @getmapping ( "form" ) public string from(model model){ model.addattribute( "username" , "tomcat" ); return "form" ; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/w_x_z_/article/details/54933843