SpringBoot 中使用JSP的方法示例
更新時間:2018年06月05日 13:39:27 作者:王學政
本篇文章主要介紹了SpringBoot 中使用JSP的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹了SpringBoot 中使用JSP的方法示例,分享給大家,具體如下:
依賴:
<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>
示例代碼:
@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ù)默認是必填的
* @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默認不支持JSP,需要在項目中添加相關(guān)的依賴
<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>
配置文件增加配置項:
spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp
Login.java
@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";
}
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 中整合 MyBatis-Plus詳細步驟(最新推薦)
本文詳細介紹了如何在SpringBoot項目中整合MyBatis-Plus,包括整合步驟、基本CRUD操作、分頁查詢、批量操作、自定義SQL操作等,通過這些步驟,開發(fā)者可以快速實現(xiàn)數(shù)據(jù)庫操作,提高開發(fā)效率,感興趣的朋友一起看看吧2025-01-01
Java動態(tài)數(shù)組添加數(shù)據(jù)的方法與應用示例
這篇文章主要介紹了Java動態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實例形式詳細分析了Java動態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
Spring加載屬性文件方式(自動加載優(yōu)先級問題)
這篇文章主要介紹了Spring加載屬性文件方式(自動加載優(yōu)先級問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

