》》》》idea中在项目多模块下springboot 整合jsp ,进行web浏览显示《《《《

157 篇文章 16 订阅

一.新建项目:03-spt-jsp-view

next:

next:

最后生成的项目结构:

我们可以发现,这样创建的项目和我们平时创建的Maven项目不太一样。它并没有在main文件中创建webapp文件夹及其相关的web文件。在文章开头,我就提到过了:这是因为Spring Boot 官方不推荐使用JSP,而且它的官方使用的是模块theamleaf。为此,我们需要在main文件夹下创建webapp/WEB-INF/jsp结构。

右键选中main包分别新建webapp/WEB-INF/jsp文件目录。 如下图:

二.编写代码

2.1 pom文件:

 <!-- springBoot 的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tomcat 服务器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- d单元测试  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- jsper-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- jstl servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

2.2 编写controller

package com.ljf.spt.demo.controller;

import com.ljf.spt.demo.model.Users;
import org.apache.catalina.LifecycleState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/08/04 17:42:56
 * @Version: V1.0
 **/
@Controller
public class UserController {
    @RequestMapping("/showUser")
    public String showUser(Model model){
        System.out.println("controller进来了");
        List<Users> userList=new ArrayList<Users>();
        userList.add(new Users(1,"beijing",23));
        userList.add(new Users(2,"shanghai",45));
        //需要一个model对象
        model.addAttribute("ulist",userList);
        //跳转视图
        return "userList";

    }
}

2.3 编写实体类:

package com.ljf.spt.demo.model;

/**
 * @ClassName: Users
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/08/04 17:44:46
 * @Version: V1.0
 **/
public class Users {
    private int id;
    private String name;
    private int age;

    public Users(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2.4  启动类:


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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
        System.out.println("srping boot 整合jsp程序启动成功!!!");
    }

}

2.5 编写jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/8/4 0004
  Time: 下午 5:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<!-- 引入c标签 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>显示用户信息页面</title>
</head>
<body>
  <a>用户列表显示页面</a>
<table width="500px" height="500" border="1px solid red">
    <tr>
    <td>编号</td>
    <td>姓名</td>
    <td>年龄</td>
    </tr>
    <c:forEach items="${ulist}" var="u">
    <tr>
    <td>${u.id}</td>
    <td>${u.name}</td>
    <td>${u.age}</td>
    </tr>
    </c:forEach>
</table>
</body>
</html>

2.6 编写配置文件:

spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#服务端端口号,名称
server.servlet.context-path= /spt-jsp-view

2.7 启动访问:http://localhost:8080/spt-jsp-view/showUser

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值