SpringBoot 中使用JSP的方法示例

2025-05-29 0 93

本文介绍了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";

}

}

SpringBoot 中使用JSP的方法示例

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

原文链接:https://blog.csdn.net/w_x_z_/article/details/54933843

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot 中使用JSP的方法示例 https://www.kuaiidc.com/111563.html

相关文章

发表评论
暂无评论