javascript
SpringBoot添加JSP支持
①創建一個新的Maven Web 項目,命名為SpringBoot_jsptest
建成之后會如上圖所示,報錯是因為沒有加入jsp的支持。
② 按照Maven規范,在src/main/下新建一個名為resource的文件夾,并在下面新建static以及templates文件夾
修改pom.xml文件:
????? ? 1、在url標簽后面加入parent元素:
??????????????<!-- ??
spring-boot-starter-parent是Spring Boot的核心啟動器,
包含了自動配置、日志和YAML等大量默認的配置,大大簡化了我們的開發。
引入之后相關的starter引入就不需要添加version配置,
? ?spring boot會自動選擇最合適的版本進行添加。
? ? ? ? ? ? ? ?-->
? ? ? ? ? ? <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>?
? ??</parent>
????? ? 2、添加一些依賴
????????????<!-- 添加spring-boot-starter-web模塊依賴 -->
? <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加 servlet 依賴. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
? ?
<!-- 添加 JSTL(JSP Standard Tag Library,JSP標準標簽庫) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加 tomcat 的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Jasper是tomcat中使用的JSP引擎,運用tomcat-embed-jasper可以將項目與tomcat分開 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
③ 在resources下創建application.properties配置文件
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
④創建控制器
package com.ysh.jsptest.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ysh.jsptest.domain.Dog;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(Model model){
// 保存一個username到model
model.addAttribute("username", "badao");
// 模擬數據庫數據保存到List集合
List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog(1, "巴扎黑1", "1.jpg",109.00,"ysh"));
dogs.add(new Dog(2, "巴扎黑2", "2.jpg",108.00,"badao"));
dogs.add(new Dog(3, "巴扎黑3", "3.jpg",58.00,"liumang"));
dogs.add(new Dog(4, "巴扎黑4", "4.jpg",108.00,"qi"));
dogs.add(new Dog(5, "巴扎黑5", "5.jpg",79.00,"zhi"));
// 保存數據到model
model.addAttribute("dogs", dogs);
return "index";
}
}
⑤ 創建JSP頁面
application.properties文件中指定的文件路徑是/WEB/jsp/,這是web開發中最常用的方式
在webapp/WEB-INF/下新建一個jsp目錄,并重新建一個index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
? ? pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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"></meta>
<title>Insert title here</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap-theme.min.css"/>?
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
</head>
<body>
<div class="panel panel-primary">
<!-- .panel-heading 面板頭信息。 -->
<div class="panel-heading">
<!-- .panel-title 面板標題。 -->
<h3 class="panel-title">Spring Boot添加JSP示例</h3>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4">
<h3>歡迎[<font color="red">${requestScope.username }</font>]</h3>
</div>
</div>
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
? ?<h3 class="panel-title">狗狗信息列表</h3>
?</div>
?<div class="panel-body">
<!-- table-responsive:響應式表格,在一個表展示所有的數據,當不夠顯示的時候可以左右滑動瀏覽-->
<div class="table table-responsive">
<!--
? ? ? ? ? ? ? ? .table-bordered 類為表格和其中的每個單元格增加邊框。
? ? ? ? ? ? ? ? .table-hover 類可以讓 <tbody> 中的每一行對鼠標懸停狀態作出響應。
? ? ? ? ? ? ? ?-->
<table class="table table-bordered table-hover">
<thead>
<th class="text-center">狗狗照片</th ><th class="text-center">狗狗名字</th>
<th class="text-center">狗狗價格</th ><th class="text-center">狗狗主人</th>
</thead>
<tbody class="text-center">
<c:forEach items="${requestScope.dogs}" var="dog">
<tr>
<td> <img src="images/${dog.image}" height="60"/></td>
<td>${dog.name}</td>
<td>${dog.price}</td>
<td>${dog.owner}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
⑥創建APP類
package com.ysh.jsptest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@SpringBootApplication指定這是一個 spring boot的應用程序.
@SpringBootApplication
public class App?
{
? ? public static void main( String[] args )
? ? {
? ? // SpringApplication 用于從main方法啟動Spring應用的類。
? ? ? ? SpringApplication.run(App.class, args);
? ? }
}
⑦運行應用,打開瀏覽器輸入:http://localhost:8080/
代碼下載:
https://download.csdn.net/download/badao_liumang_qizhi/10532637
總結
以上是生活随笔為你收集整理的SpringBoot添加JSP支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小白教你一步一步安装Scrapy(西瓜皮
- 下一篇: jQuery中选择器加尖括号的区别