SpringBoot入門之集成JSP的示例代碼
原本打算這篇繼續(xù)寫thymeleaf方面的內(nèi)容,一看內(nèi)容還挺多的,可能一周也寫不完,而且從Controller獲取值等內(nèi)容也都能從網(wǎng)上百度,所以就寫了springboot集成jsp。不管thymeleaf還是jsp其實都是分層思想的體現(xiàn)。
一、引入依賴
還是用上一博客的demo,在它基礎(chǔ)上進行修改,這次是集成jsp,所以要先引入jsp的依賴。這里需要把上一博客加的thymeleaf去掉。
<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>
二、創(chuàng)建jsp頁面
既然是集成jsp,肯定少不了jsp頁面,這里我把jsp頁面login.jsp放在了/demo/src/main/webapp/view下。在jsp中獲取Controller中的一個變量值。
<%@ 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的前綴后綴。
spring.mvc.view.prefix=/view/ spring.mvc.view.suffix=.jsp
四、創(chuàng)建Controller
在
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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 通過AQS實現(xiàn)數(shù)據(jù)組織
這篇文章主要介紹了通過AQS實現(xiàn)數(shù)據(jù)組織,想了解AQS的同學(xué)可以參考下2021-04-04
Java中兩個大數(shù)之間的相關(guān)運算及BigInteger代碼示例
這篇文章主要介紹了Java中兩個大數(shù)之間的相關(guān)運算及BigInteger代碼示例,通過biginteger類實現(xiàn)大數(shù)的運算代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11
JAVA多線程實現(xiàn)生產(chǎn)者消費者的實例詳解
這篇文章主要介紹了JAVA多線程實現(xiàn)生產(chǎn)者消費者的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java基礎(chǔ)知識之StringReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識之StringReader流的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

