zoukankan      html  css  js  c++  java
  • SpringBoot使用Jsp开发Web项目

    1.简述

      以前都是使用JSP页面开发,但是因为JSP无法实现Spring Boot的多种特性,所以Spring Boot不推荐使用JSP进行页面开发。

    2.使用示例

      引入相关依赖

    <!--JavaServer Pages Standard Tag Library,JSP标准标签库-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    
    <!--内置tomcat对Jsp支持的依赖,用于编译Jsp-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    View Code

      application.yml文件中添加Jsp模板配置

    ##添加jsp模板配置
    spring:
      mvc:
        view:
          prefix: /WEB-INF/jsp/
          suffix: .jsp
    View Code

      在webapp/css文件夹下添加main.css样式文件,内容如下

    h1 {
        color: #0000FF;
    }
    
    h2 {
        color: #FF0000;
    }
    View Code

      在webapp/WEB-INF/JSP文件夹下添加index.jsp文件,内容如下

    <%@ page 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">
        <link rel="stylesheet" href="./css/main.css"/>
        <title>测试页面</title>
    </head>
    <body>
        <h1>Spring Boot + Thymeleaf demo</h1>
        <h2>Message: + ${message}</h2>
        <!-- ${xxx} 引用 SpringMVC 中 HttpServletRequest 属性 -->
    </body>
    </html>
    View Code

      创建一个HelloController.java,内容如下

    @Controller
    @RequestMapping("/hello")
    public class HelloController {
    
        @RequestMapping(method= RequestMethod.GET,produces="text/html;charset=UTF-8")
        public String hello(HttpServletRequest req) {
            req.setAttribute("message", "Hello World!");
            return "index"; // templatesindex.jsp
        }
    }
    View Code

      最后启动DemoApplication就可以访问,相关文件夹需要自己创建。

      项目结构图如下

  • 相关阅读:
    抽奖代码
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
    Cannot add or update a child row: a foreign key constraint fails
    SyntaxError:identifier starts immediately after numeric literal
    too much recursion
    微信处理红包
    minerd
    minerd
    kill常用
    阿里云centos 6安装Nginx+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/bl123/p/14357279.html
Copyright © 2011-2022 走看看