微服务 第三章: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、运行项目即可。

猜你喜欢

转载自www.cnblogs.com/yaohuiqin/p/9366433.html
今日推荐