欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot實戰(zhàn)之使用JSP的示例

 更新時間:2017年10月10日 08:24:01   作者:思與學  
本篇文章主要介紹了spring boot實戰(zhàn)之使用JSP的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前后端分離的架構(gòu)有其優(yōu)勢,但具體情況具體分析,并不是任何時候使用前后端分離架構(gòu)都是合適的。我最近就體會到其中的坑,因為部門屬性的問題,前端項目占比較低,所以公司前端基本上都是新手,結(jié)果就是后端接口完成了一個多月,前端還在加班加點的趕。前后端人員的能力和人數(shù)與工作量是匹配的,前后端都能hold住時建議使用前后端分離架構(gòu),如果前端能力有限或人員較少,那就最好不要采用,這樣才能保證項目進度可控。

Spring Boot并不建議使用JSP,但是可能有習慣和人員知識面的限制,還是希望使用jsp,則可以根據(jù)下面的教程來了解如何在spring boot項目內(nèi)使用jsp。

1、添加maven依賴

<!-- 添加對jsp視圖解析的支持 -->
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
</dependency>

2、添加配置

在application.properties內(nèi)添加以下配置:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

3、創(chuàng)建jsp

創(chuàng)建src/main/webapp/WEB-INF/jsp目錄,目錄結(jié)構(gòu)不要改動

在src/main/resources目錄下創(chuàng)建static目錄用于存放靜態(tài)資源,如image目錄用于存放圖片,js目錄用于存放js文件

創(chuàng)建jsp文件,如test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!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>test</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>

</head>
<body>
  hello,welcome to you 123!test=[${test }] test2=[${test2 }]
  <br>
  ![](${pageContext.request.contextPath }/image/1.jpg)
  <c:if test="${1 == 1 }"><br>this is ShangHai,china!</c:if>
</body>
</html>

${pageContext.request.contextPath }用于獲取項目路徑,即server.context-path設置的值

訪問圖片${pageContext.request.contextPath }/image/1.jpg,也就是src/main/resources/static/image/1.jpg文件,注意直接訪問/image/1.jpg即可

加載js路徑為${pageContext.request.contextPath }/js/jquery.min.js,同圖片,加載靜態(tài)資源的方式類似

4、訪問jsp

創(chuàng)建controller

@Controller
public class TestController {

  @RequestMapping("/test")
  public String myJsp(HttpServletRequest request,ModelMap model){
    System.out.println("myjsp");
    model.put("test", "test");
    request.setAttribute("test2", "test2");
    return "test";
  }
  
}

啟動項目后,訪問localhost:port/test就可以看到上面的示例頁面了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論