Spring Boot---(5)SpringBoot整合JSP

SpringBoot整合JSP

Springboot官方推荐的前端框架却是thymeleaf,并且默认不支持jsp,但是很多java开发人员最熟悉的前端开发工具还是jsp,现在jsp技术已经被淘汰了,还是不推荐使用jsp,我也只在一开始使用springboot时,没习惯使用thymeleaf,所以整合使用了jsp。

关于jsp不做细讲,仅仅只是用springboot整合一下jsp而已。

 

首先,因为springboot不支持jsp,需要引入jsp的支持,而且springboot默认的tomcat也是不支持使用jsp的,所以要在pom添加一下几个依赖包

<dependencies>
        <!-- springboot的mvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 内置的tomcat不支持jsp,如果需要使用jsp需要添加对tomcat的支持-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
        </dependency>

        <!-- 添加对jsp的支持,<scope>provided</scope>在打包时会将该包排除掉-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- 配置对jsp jstl的支持-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

在application.yml文件中需要设置jsp的前辍和后辍等,默认页面会是使用thymeleaf,所以将thymelead模板关闭。

application.yml

spring:
  mvc:
    view:
      suffix: .jsp
      prefix: /WEB-INF/jsp/
  thymeleaf:
    cache: false
    enable-spring-el-compiler: false
server:
  port: 8080

Controller

package com.kevin.controller;

import com.kevin.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kevin
 * @version 1.0
 * @description     springboot整合jsp
 * @createDate 2019/3/13
 */
@Controller
public class UserController {

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

    /**
     * 处理请求,产生数据
     * @return
     */
    @RequestMapping(value = "/showUser",method = RequestMethod.POST)
    public String showUser(Model model) {
        List<User> list = new ArrayList<>();
        list.add(new User(1,"张三",20));
        list.add(new User(2,"李四",21));
        list.add(new User(3,"王五",22));
        // 需要一个model对象
        model.addAttribute("list",list);
        // 跳转视图
        return "userList";
    }


}

User

package com.kevin.entity;

/**
 * @author kevin
 * @version 1.0
 * @description
 * @createDate 2019/3/13
 */
public class User {

    private Integer userId;
    private String userName;
    private Integer userage;

    public User() {
    }

    public User(Integer userId, String userName, Integer userage) {
        this.userId = userId;
        this.userName = userName;
        this.userage = userage;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userage=" + userage +
                '}';
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getUserage() {
        return userage;
    }

    public void setUserage(Integer userage) {
        this.userage = userage;
    }
}

启动类

package com.kevin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author kevin
 * @version 1.0
 * @description
 * @createDate 2019/3/13
 */
@SpringBootApplication
public class JSPApplication {

    public static void main(String[] args) {
        SpringApplication.run(JSPApplication.class,args);
    }
}

userList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center" width="50%">
		<tr>
			<th>ID</th>
			<th>Name</th>
			<th>Age</th>
		</tr>
		<c:forEach items="${list }" var="user">
			<tr>
				<td>${user.userId }</td>
				<td>${user.userName }</td>
				<td>${user.userage }</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

比较重要的一点是,需要到project Structure中的Mudules里找到项目,然后点击web,添加web..xml和设置webapp为web目录

整体的目录

访问地址localhost:8080/showUser

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值