zoukankan      html  css  js  c++  java
  • 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    http://www.cnblogs.com/chry/p/5912870.html

    ***********************************************

    做WEB项目,一定都用过JSP这个大牌。Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的。当你从一个传统的Spring MVC项目转入一个Spring Boot项目后,却发现JSP和view关联有些麻烦,因为官方不推荐JSP在Spring Boot中使用。在我看来,继续用这种繁杂的手续支持JSP仅仅只是为了简单兼容而已。

    我们先来看看如何在SpringBoot中使用JSP ?

    1. 在pom.xm中加入支持JSP的依赖

    复制代码
            <dependency>
               <groupId>org.apache.tomcat.embed</groupId>
               <artifactId>tomcat-embed-jasper</artifactId>
               <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
    复制代码

    2. 在src/main/resources/application.properties文件中配置JSP和传统Spring MVC中和view的关联

    # MVC
    spring.view.prefix=/WEB-INF/views/
    spring.view.suffix=.jsp

    3. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里

    复制代码
    <!DOCTYPE html>
    
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Hello</title>
    </head>
    <body>
        Hello ${name}
    </body>
    </html>
    复制代码

    4. 编写Controller

    复制代码

      package com.chry.study;

      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.ModelMap;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;

    @Controller
    @EnableAutoConfiguration
    public class SampleController {
        @RequestMapping("/hello")
        public ModelAndView getListaUtentiView(){
            ModelMap model = new ModelMap();
            model.addAttribute("name", "Spring Boot");
            return new ModelAndView("hello", model);
        }
    }
    复制代码

     5. 编写Application类

    复制代码
    package com.chry.study;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class WebApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(WebApplication.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(WebApplication.class, args);
        }
    }
    复制代码

    6. 以java application方式运行后,就可以访问http://locahost:8080/hello

    ==================  分割线 ==================================

    以上代码pom.xml中的javax.servlet.jsp.jstl是用于支持JSP标签库的,在Web2.5的容器中没有问题,单当你的容器是Web3.0或以上版本时,就会出问题。 这是个非常坑爹的问题。

    javax.servlet.jsp.jstl会自动加载依赖servlet-api-2.5.jar, 而且会在实际运行时把支持Web3.0的3.1版本的javax.servlet-api覆盖掉。即使你在pom.xml显示的在加入3.1版本的javax.servlet-api也没用。导致SpringBoot应用抛出Runtime exception运行错误。

    这是一个不可调和的矛盾,要吗不用javax.servlet.jsp.jstl,要吗不用Web3.0。

    但绝大多数情况下,jstl标签库不是必须的,而Web3.0是必须的。替代方式就是不用JSP,改用Themeleaf吧

  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/zhao1949/p/6552241.html
Copyright © 2011-2022 走看看