最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot 添加JSP支持

 更新時(shí)間:2017年05月05日 10:24:20   作者:愛笑的T_T  
本篇文章主要介紹了詳解Spring Boot 添加JSP支持,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

大體步驟:

(1)創(chuàng)建Maven web project;

(2)在pom.xml文件添加依賴;

(3)配置application.properties支持jsp

(4)編寫測試Controller

(5)編寫JSP頁面

(6)編寫啟動(dòng)類Application.Java

1,F(xiàn)reeMarker

2,Groovy

3,Thymeleaf (spring 官網(wǎng)使用這個(gè))

4,Velocity

5,JSP (貌似Spring Boot官方不推薦,STS創(chuàng)建的項(xiàng)目會(huì)在src/main/resources 下有個(gè)templates 目錄,這里就是讓我們放模版文件的,然后并沒有生成諸如 SpringMVC 中的webapp目錄)

不過本文還是選擇大家都熟悉的JSP來舉例,因?yàn)槭褂肑SP與默認(rèn)支持的模版需要特殊處理,所以拿來舉例更好。

(1)創(chuàng)建Maven web project

使用Eclipse新建一個(gè)Maven Web Project ,項(xiàng)目取名為:

spring-boot-jsp

(2)在pom.xml文件添加依賴

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.example</groupId> 
  <artifactId>spring-boot-jsp</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>war</packaging> 
 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  </properties> 
 
  <!-- Inherit defaults from Spring Boot --> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.0.RELEASE</version> 
  </parent> 
  <dependencies> 
    <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> 
    <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標(biāo)準(zhǔn)標(biāo)簽庫)是一個(gè)不斷完善的開放源代碼的JSP標(biāo)簽庫,是由apache的jakarta小組來維護(hù)的。JSTL只能運(yùn)行在支持JSP1.2和Servlet2.3規(guī)范的容器上,如tomcat  
      4.x。在JSP 2.0中也是作為標(biāo)準(zhǔn)支持的。 不然報(bào)異常信息: javax.servlet.ServletException: Circular  
      view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp]  
      again. Check your ViewResolver setup! (Hint: This may be the result of an  
      unspecified view, due to default view name generation.) --> 
    <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> 
    <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <scope>provided</scope> 
    </dependency> 
  </dependencies> 
  <build> 
    <finalName>spring-boot-jsp</finalName> 
    <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
         <source>1.8</source> 
         <target>1.8</target> 
       </configuration> 
      </plugin> 
    </plugins> 
  </build> 
</project> 

(3)application.properties配置

上面說了spring-boot 不推薦JSP,想使用JSP需要配置application.properties。

添加src/main/resources/application.properties內(nèi)容:

# 頁面默認(rèn)前綴目錄 
spring.mvc.view.prefix=/WEB-INF/jsp/ 
# 響應(yīng)頁面默認(rèn)后綴 
spring.mvc.view.suffix=.jsp 
# 自定義屬性,可以在Controller中讀取 
application.hello=Hello Angel From application 

(4)編寫測試Controller

package com.example.jsp.controller; 
 
import java.util.Map; 
 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
 
@Controller 
public class HelloController { 
  //從application中讀取配置,如取不到默認(rèn)值為hello jack 
  @Value("${application.hello:hello jack}") 
  private String hello; 
 
  @RequestMapping("/helloJsp") 
  public String helloJsp(Map<String, Object> map){ 
    System.out.println("HelloController.helloJsp().hello="+hello); 
    map.put("hello", hello); 
    return "helloJsp"; 
  } 
} 

(5)編寫JSP頁面

在 src/main 下面創(chuàng)建 webapp/WEB-INF/jsp 目錄用來存放我們的jsp頁面:helloJsp.jsp

<%@ 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> 
  helloJsp 
  <hr> 
  ${hello} 
   
</body> 
</html> 

6)編寫啟動(dòng)類

編寫Application.java啟動(dòng)類:

package com.example; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
//import org.springframework.boot.context.web.SpringBootServletInitializer; 
 
 
@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 
 
  public static void main(String[] args){ 
    SpringApplication.run(Application.class, args); 
  } 
}

 右鍵Run As  Java Application訪問:http://127.0.0.1:8080/helloJsp 可以訪問到:

helloJsp

Hello Angel From application

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

相關(guān)文章

  • Java實(shí)現(xiàn)郵件發(fā)送功能

    Java實(shí)現(xiàn)郵件發(fā)送功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 全局記錄Feign的請(qǐng)求和響應(yīng)日志方式

    全局記錄Feign的請(qǐng)求和響應(yīng)日志方式

    這篇文章主要介紹了全局記錄Feign的請(qǐng)求和響應(yīng)日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Spring Boot2.x集成JPA快速開發(fā)的示例代碼

    Spring Boot2.x集成JPA快速開發(fā)的示例代碼

    這篇文章主要介紹了Spring Boot2.x集成JPA快速開發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 盤點(diǎn)MQ中的異常測試

    盤點(diǎn)MQ中的異常測試

    這篇文章主要為大家介紹了盤點(diǎn)MQ中的異常測試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java使用Sa-Token框架完成踢人下線功能

    Java使用Sa-Token框架完成踢人下線功能

    踢人下線是一個(gè)很常見的需求,本文主要介紹了Java使用Sa-Token框架完成踢人下線功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Sentinel熱點(diǎn)key限流的實(shí)現(xiàn)詳解

    Sentinel熱點(diǎn)key限流的實(shí)現(xiàn)詳解

    這篇文章主要介紹了Sentinel熱點(diǎn)key限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java AOP動(dòng)態(tài)代理詳細(xì)介紹

    Java AOP動(dòng)態(tài)代理詳細(xì)介紹

    AOP是一種設(shè)計(jì)思想,是軟件設(shè)計(jì)領(lǐng)域中的面向切面編程,它是面向?qū)ο缶幊痰囊环N補(bǔ)充和完善。本文將用Java實(shí)現(xiàn)AOP代理的三種方式,需要的可以參考一下
    2022-08-08
  • Lombok基本注解之@SneakyThrows的作用

    Lombok基本注解之@SneakyThrows的作用

    @SneakyThrows注解是由lombok為咱們封裝的,它能夠?yàn)樵蹅兊拇a生成一個(gè)try...catch塊,并把異常向上拋出來,下面這篇文章主要給大家介紹了關(guān)于Lombok基本注解之@SneakyThrows作用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java UrlRewriter偽靜態(tài)技術(shù)運(yùn)用深入分析

    Java UrlRewriter偽靜態(tài)技術(shù)運(yùn)用深入分析

    通常我們?yōu)榱烁玫木徑夥?wù)器壓力,和增強(qiáng)搜索引擎的友好面,都將文章內(nèi)容生成靜態(tài)頁面,這就產(chǎn)生了偽靜態(tài)技術(shù),也就是我們常說的Url Rewriter重寫技術(shù)
    2012-12-12
  • SpringCloud整合OpenFeign實(shí)現(xiàn)微服務(wù)間的通信

    SpringCloud整合OpenFeign實(shí)現(xiàn)微服務(wù)間的通信

    微服務(wù)之間的通信?式,通常有兩種: RPC 和 HTTP,在SpringCloud中, 默認(rèn)是使?HTTP來進(jìn)?微服務(wù)的通信, 最常?的實(shí)現(xiàn)形式有兩種:RestTemplate和OpenFeign,本文給大家介紹了SpringCloud整合OpenFeign實(shí)現(xiàn)微服務(wù)間的通信,需要的朋友可以參考下
    2024-06-06

最新評(píng)論

黎平县| 沿河| 天镇县| 永兴县| 招远市| 会泽县| 麟游县| 新安县| 门头沟区| 双流县| 淅川县| 武安市| 昌黎县| 玛纳斯县| 朝阳县| 郸城县| 嘉禾县| 湛江市| 浦江县| 遵化市| 合阳县| 宁强县| 裕民县| 稷山县| 华坪县| 鄄城县| 买车| 加查县| 青岛市| 科技| 义马市| 襄垣县| 丹寨县| 青铜峡市| 茶陵县| 秦皇岛市| 繁昌县| 米易县| 筠连县| 庆阳市| 长春市|