springboot实战笔记(十)----springboot整合jsp

一 创建一个springboot项目

pom.xml文件如下:


  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.10.RELEASE
  
  com.bjsxt
  08-spring-boot-view-jsp
  0.0.1-SNAPSHOT
  
  
  
  	1.7
  
  
  
  
    
        org.springframework.boot
        spring-boot-starter-web
    
	    
	
		javax.servlet
		jstl
	
	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	

二 创建Controller

package com.bjsxt.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.model.Users;

/**
 * SpringBoot整合jsp
 *
 *
 */
@Controller
public class UserController {
	/*
	 * 处理请求,产生数据
	 */
	@RequestMapping("/showUser")
	public String showUser(Model model){
		List list = new ArrayList<>();
		list.add(new Users(1,"张三",20));
		list.add(new Users(2,"李四",22));
		list.add(new Users(3,"王五",24));
		
		//需要一个Model对象
		model.addAttribute("list", list);
		//跳转视图
		return "userList";
	}
}

 

三 创建application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=8070

对应的创建文件:

springboot实战笔记(十)----springboot整合jsp_第1张图片

 

四 前端jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




Insert title here


	
			
ID Name Age
${user.userid } ${user.username } ${user.userage }

五 创建启动类启动

package com.bjsxt;

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

/**
 * SpringBoot启动类
 *
 *
 */
@SpringBootApplication
public class App {

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

启动测试:

springboot实战笔记(十)----springboot整合jsp_第2张图片

 

 

你可能感兴趣的:(spring,boot)