SpringBoot 整合jsp
一:用idea 创建 springboot 项目:
详情请参考:《使用IDEA创建一个springboot项目》
二:具体代码内容:
1:代码结构
2:JspApplication
package com.alancode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /*** * * *@author Alan_Liu *@date 2021/2/16 23:17 * **/ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class JspApplication { public static void main(String[] args) { SpringApplication.run(JspApplication.class, args); } } /** * @description: JspApplication 项目启动器 * <p> * 注意: * 关于编写启动器需要注意的问题。 * 启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。 * 但是不能把该文件放到controller平行级的包或者其子包下。 * @author: Alan_Liu * @create: 2021/2/16 23:17 */
3:HelloWorld
package com.alancode.springboot.controller;/** * @author Alan_liu * @Create 2021/2/16 23:33 */ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** *@program: SpringBootAddJsp *@description: HelloWorld *@author: Alan_Liu *@create: 2021-02-16 23:33 */ public class HelloWorld { /** * @return * @throws * @Param: [] * @author Alan_Liu * @date 2021/2/14 21:52 **/ @RequestMapping("/hello") @ResponseBody public Map<String, Object> showHelloWolrd() { Map<String, Object> map = new HashMap<String, Object>(); map.put("msg", "HelloWorld"); return map; } }
4:UserController
package com.alancode.springboot.controller; /** * @author Alan_liu * @Create 2021/2/16 21:40 */ import com.alancode.springboot.pojo.Users; 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; /** *@description: SpringBoot 整合 jsp *@author: Alan_Liu *@create: 2021-02-16 21:40 */ @Controller /**做view视图 跳转的 所有这里不能使用@RestController ***/ public class UserController { @RequestMapping("/showUserInfo") public String showUser(Model model){ List<Users> list=new ArrayList<>(); list.add(new Users(1,"张三",20)) ; list.add(new Users(2,"李四",20)) ; list.add(new Users(3,"王五",20)) ; list.add(new Users(4,"赵六",20)) ; //需要一个 model 对象 model.addAttribute("list",list); //视图跳转 return "userList"; } }
5:Users
package com.alancode.springboot.pojo; /** * @author Alan_liu * @Create 2021/2/16 21:44 */ /** *@program: SpringBootAddJsp *@description: 用户 *@author: Alan_Liu *@create: 2021-02-16 21:44 */ public class Users { private Integer userid; private String username; private Integer 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; } @Override public String toString() { return "Users{" + "userid=" + userid + ", username='" + username + '\'' + ", userage=" + userage + '}'; } public Users() { } public Users(Integer userid, String username, Integer userage) { this.userid = userid; this.username = username; this.userage = userage; } public Users(Integer userid) { this.userid = userid; } public Users(Integer userid, String username) { this.userid = userid; this.username = username; } public Users(String username) { this.username = username; } public Users(String username, Integer userage) { this.username = username; this.userage = userage; } }
6:application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
7:userList.jsp
<%-- Created by IntelliJ IDEA. User: Alan_Liu Date: 2021/2/16 Time: 23:11 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% System.out.println("----这里有没有进来-----"); %> <html> <head> <title>Title</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <td>ID</td> <td>Name</td> <td>Age</td> </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>
8:pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alanCode.springBoot</groupId> <artifactId>SpringBootAddViewJsp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootAddViewJsp</name> <description>springboot 整合 jsp </description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--jsp页面使用jstl标签 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--用于编译jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
9:启动项目
启动正常
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/