本文介绍了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 接收提交的参数,参数默认是必填的 * @requestparam(value = "password", required = false) required = false,可以不是必填的参数 * */ @postmapping ( "builduser" ) @responsebody public string builduser( @requestparam ( "username" ) string username, @requestparam (value = "password" , required = false ) string password){ return "提交的参数:username" + username + " password:" + password; } |
在springboot中使用jsp
springboot默认不支持jsp,需要在项目中添加相关的依赖
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> |
配置文件增加配置项:
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" ; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/w_x_z_/article/details/54933843