微服務 第三章:SpringBoot 創建web項目(調用jsp)


springboot內部對jsp的支持並不是特別理想,而springboot推薦的視圖是Thymeleaf,對於java開發人員來說還是大多數人員喜歡使用jsp

碼雲地址:https://gitee.com/yaohuiqin/SpringBootDemo

在第二章的基礎上進行修改:

1、maven添加

     <!--配置springboot支持jsp  添加tomcat支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!--配置springboot支持jsp  添加jsp支持 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--配置springboot支持jsp  jsp對servlet容器的支持 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!--在jsp頁面使用jstl標簽來處理界面邏輯,那么需要引入jstl maven-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

 maven刪除

     <!--配置springboot支持web 加載html文件(thymeleaf模板引擎)-->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        -->

 

2、新建webapp文件夾  > WEB-INF 文件夾 > jsp 文件夾  > indexjsp.jsp

建完你會發現 webapp文件夾下缺少藍點標識,修改方法如下圖

 

3、在application.properties 文件中添加屬性:

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

4、在根目錄下新建類ServletInitializer.java :

public class ServletInitializer extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(LessonOneApplication.class);
	}
}

它繼承了SpringBootServletInitializer這個父類,而SpringBootServletInitializer這個類是springboot提供的web程序初始化的入口,當我們使用外部容器(后期文章講解使用外部tomcat如何運行項目)運行項目時會自動加載並且裝配。 實現了SpringBootServletInitializer的子類需要重寫一個configure方法,方法內自動根據LessonOneApplication.class的類型創建一個SpringApplicationBuilder交付給springboot框架來完成初始化運行配置。 

5、新建 controller對象:

@Controller
public class IndexJspController {
    @RequestMapping(value = "/indexJsp",method = RequestMethod.GET)
    public String index(){
        return "indexjsp";
    }
}

6、運行項目即可。

 


免責聲明!

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



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