SpringBoot使用jsp作為視圖模板&常規部署


springboot其實並不推薦使用jsp作為視圖模板,其默認采用 Thymeleaf作為模板,出於對其沒有研究,故考慮目前階段仍然使用jsp作為視圖模板。下面就展開實踐案例過程:

1、首先創建一個jsp頁面:
<!DOCTYPE html>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html lang="en">

<body>
<c:url value="/resources/text.txt" var="url"/>
<spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />
Spring URL: ${springUrl} at ${time}
<br>
JSTL URL: ${url}
<br>
Message: ${message}
</body>

</html>

2、在springmvc中我們也需要定義InternalResourceViewResolver來描述相關頁面的存放地址等屬性,springboot中同樣需要進行描述,在application.properties中配置如下:
# viewpage path
spring.mvc.view.prefix=/WEB-INF/jsp/
# suffix of view
spring.mvc.view.suffix=.jsp
# message
application.message=Hello Angel From application

3、如此我們1中新建的頁面存放路徑為:


4、此時我們需要新增一個控制器類,通過控制控制類轉發至我們的請求頁面:
package com.shf.springboot.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

@Value("${application.message:Hello World}")
private String message = "Hello World";
@GetMapping("/welcome")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}

5、啟動服務通過設定的請求地址訪問:

注:可以發現打印的message消息為application.properties中的配置的message內容。
@Value("${application.message:Hello World}"):如果在當前類讀取的資源文件中存在對應的key屬性,則通過@Value能夠獲取其值。

6、下面刪除application.properties中的message配置,查驗結果:

打印信息:

驗證發現,沒有配置的情況下,則直接讀取@Value注解中定義的值。

7、此時發現,開發環境下我們能夠正常的訪問我們的請求並轉發至對應的jsp請求頁面,但是我們部署環境如何呢,首先嘗試通過打包成jar驗證:
直接通過java -jar 方式啟動jar服務

通過瀏覽器訪問,

無法正常打開jsp頁面請求。非jsp頁面轉發請求正常


8、通過maven直接打包成war包,然后部署至常規tomcat下:

驗證發現404錯誤,沒有找到對應的請求處理,通過繼續了解發現啟動類可以繼承SpringBootServletInitializer類。

9、修改啟動類繼承自SpringBootServletInitializer,重新打包驗證:
package com.shf.SpringBoot1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.shf.springboot.controller.ServerConfig2;

@SpringBootApplication
@EnableConfigurationProperties({ServerConfig.class,ServerConfig2.class})
@ComponentScan(basePackages={"com.shf.SpringBoot1","com.shf.springboot.*"})
public class App
extends SpringBootServletInitializer //這個類的作用與在web.xml中配置負責初始化Spring應用上下文的監聽器作用類似,如果需要打成war部署在tomcat下則需要
{
@Autowired
ServerConfig serverConfig;

public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
}
再次啟動服務正常訪問:


注:說明SpringBootServletInitializer對比傳統的web項目構建,可以理解為web.xml的作用,集成后tomcat容器能夠將其作為web項目進行加載。
通過跟蹤源碼可以發現,其實SpringBootServletInitializer實現了WebApplicationInitializer接口。

10、以上我們采用的是application.properties中配置相關jsp視圖解析對應的參數值,那么我們是否可以通過一個普通的Java配置來實現呢,答案是肯定的,首先注釋掉application.properties中的配置:
# viewpage path
#spring.mvc.view.prefix=/WEB-INF/jsp/
# suffix of view
#spring.mvc.view.suffix=.jsp

然后新增一個java配置類:
package com.shf.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
public class ViewResolverConfiguration {

@Bean
public InternalResourceViewResolver getJspViewResolver(){
InternalResourceViewResolver jspViewResolver=new InternalResourceViewResolver();
jspViewResolver.setPrefix("/WEB-INF/jsp/");
jspViewResolver.setSuffix(".jsp");
jspViewResolver.setViewClass(JstlView.class);
return jspViewResolver;
}
}
驗證請求響應


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM