使用IDEA社区版搭建Springboot、jsp开发环境

embedded/2025/2/5 4:20:06/

1,感觉传统的JSP可以放弃,直接前端JS+后端就可以了

2,建议不要用低版本的springboot,版本兼容搭配太麻烦

如果仅仅是搭建springboot开发环境,没什么难度,本文主要记录以下几个问题的解决:

1,支持jsp

2,打包war后在外部tomcat环境下运行

使用软件清单

安装IDEA社区版的最新版本(时下是2024.3.2.2)

下载Maven

下载java21

配置maven

以下内容为D:\Develop\apache-maven-3.9.9\settings.xml,从网上搬来的,感谢该网友的分享。

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><!--本地仓库。该值表示构建系统本地仓库的路径。其默认值为${user.home}/.m2/repository。  --><localRepository>D:\Develop\apache-maven-3.9.9\local</localRepository><!--配置服务端的一些设置。如果局域网内部有nexus,需要管理项目jar包可配置 -->  <servers></servers><!--为仓库列表配置的下载镜像列表-->  <mirrors><!--给定仓库的下载镜像-->  <mirror><id>aliyun</id><mirrorOf>central</mirrorOf><url>http://maven.aliyun.com/nexus/content/groups/public/</url></mirror></mirrors><!-- 仓库配置 --><profiles><!--根据环境参数来调整的构件的配置 -->  <profile><!--该配置的唯一标识符 -->  <id>lovecto_profile</id><!-- 远程仓库列表 -->  <repositories><repository><id>aliyun</id><name>aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories><!-- 配置插件下载的仓库列表--><pluginRepositories><pluginRepository><id>thirdparty_repository</id><name>thirdparty_repository</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></pluginRepository></pluginRepositories></profile></profiles><!-- 激活所使用的配置--><activeProfiles><activeProfile>lovecto_profile</activeProfile></activeProfiles>
</settings>

打开Idea,配置Maven:

 

创建一个web项目

打开pom.xml,修改配置后如下(注意其中的注释,需要保留,后面打war包需要):

<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 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>TestSpringBoot3</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>TestSpringBoot3 Maven Webapp</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.2</version></parent><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
<!--<exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions>
--></dependency><!-- for jsp --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version>
<!--<scope>provided</scope>
--></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version>
<!--<scope>provided</scope>
--></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><version>10.1.34</version>
<!--<scope>provided</scope>
--></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jasper</artifactId><version>10.1.19</version>
<!--<scope>provided</scope>
--></dependency><!-- end for jsp --></dependencies><!--for spring boot init application--><build><finalName>TestSpringBoot3</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.sample.Application</mainClass></configuration></plugin></plugins></build></project>

最后的build-plugin的配置就是让外部tomcat环境启动时能够正常加载Springboot框架二设置的。其中所指的Application.java代码如下:

java">package com.sample;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;import org.springframework.boot.builder.SpringApplicationBuilder;@SpringBootApplication
@ServletComponentScan("com.sample")
public class Application extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {System.out.println("--SpringApplicationBuilder  ------------------x-");return builder.sources(Application.class);}
}

这个文件可以分成两个,也可以像我一样放在一个文件里,它包含了连个部分:springbootapplication和Springbootservletinitializer,前者只有一个main方法,用来在idea环境下启动springboot项目用,后者是打包war后用的。

添加Springboot的配置文件application.properties(在src-main-resources目录下):

#server.port=8080
#server.servlet.context-path=/test3
spring.mvc.view.suffix=.jsp
#spring.mvc.view.prefix=/WEB-INF/jsp/
server.servlet.jsp.init-parameters.development=true

注意最后一行,那个也是支持jsp的关键。 

写一个Controller测试以下:

java">package com.sample.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class Hello {@RequestMapping("/hello")public String helloHtml() {System.out.println("--hello1-index.html-------");return "index";}//@GetMapping("/helloJsp")@RequestMapping("/helloJsp")public String hello() {System.out.println("--hello1-jsp-------");return "/WEB-INF/jsp/hello1";}
}

请看我代码里特意保留了一个注释@GetMapping,不要用它,那个是SpringMvc的注解,在springboot项目里不起作用。

在src-main-webapp目录下编写一个index.jsp、WEB-INF/jsp/目录下编写hello1.jsp,内容只有一行即可,用来区分输出的页面,例如(两个文件里自己修改输出的内容,能够区分开两个文件即可):

java"><%out.println("This is index.jsp file.");%>

在Idea开发环境运行springboot项目

直接在Application.java代码页面右键 Run Application.main

注意观察下方的运行日志,其中就标明了默认的服务器端口8080,看到正常启动后,就可以打开浏览器输入以下地址:

http://localhost:8080/hello

http://localhost:8080/helloJsp

就可以分别看到两个页面的输出了。

打包war

去掉pom.xml里的所有注释,然后在maven栏目-plugin-war:war双击。


http://www.ppmy.cn/embedded/159653.html

相关文章

DeepSeek-R1 论文. Reinforcement Learning 通过强化学习激励大型语言模型的推理能力

论文链接&#xff1a; [2501.12948] DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning 实在太长&#xff0c;自行扔到 Model 里&#xff0c;去翻译去提问吧。 工作原理&#xff1a; 主要技术&#xff0c;就是训练出一些专有用途小模型&…

c++ list的front和pop_front的概念和使用案例—第2版

在 C 标准库中&#xff0c;std::list 的 front() 和 pop_front() 是与链表头部元素密切相关的两个成员函数。以下是它们的核心概念和具体使用案例&#xff1a; 1. front() 方法 概念&#xff1a; 功能&#xff1a;返回链表中第一个元素的引用&#xff08;直接访问头部元素&am…

257. 二叉树的所有路径

二叉树的所有路径 已解答 简单 给你一个二叉树的根节点 root &#xff0c;按 任意顺序 &#xff0c;返回所有从根节点到叶子节点的路径。 叶子节点 是指没有子节点的节点。 示例 1&#xff1a; 输入&#xff1a;root [1,2,3,null,5] 输出&#xff1a;[“1->2->5”,“…

HTB:UnderPass[WriteUP]

目录 连接至HTB服务器并启动靶机 信息收集 使用rustscan对靶机TCP端口进行开放扫描 使用nmap对靶机TCP开放端口进行脚本、服务扫描 使用nmap对靶机TCP开放端口进行漏洞、系统扫描 使用nmap对靶机常用UDP端口进行开放扫描 使用nmap对靶机UDP开放端口进行脚本、服务扫描 …

Python玄学

过年期间无聊的看了看DY直播&#xff0c;也是迷上玄学了。突然想着为啥要自己掐指算&#xff0c;我这&#x1f437;脑哪记得到那么多东西啊。然后&#xff0c;就捣鼓捣鼓了一些玩意儿。留个纪念。 注&#xff1a;就是一个玄学推动学习&#xff0c;部分内容不必当真&#xff0c;…

三傻排序的比较(选择,冒泡,插入)

在学习排序算法时&#xff0c;选择排序、冒泡排序和插入排序是最常见的基础排序算法。但是&#xff0c;尽管这些算法看起来非常相似&#xff0c;它们在实际应用中的效率和性能却有所不同。本文将详细比较这三种排序算法的时间复杂度、空间复杂度。 比较总结 排序算法时间复杂…

如何在5步内使用 Spring AI 和 OpenAI 的 DALL-E 3 生成图像

将 Spring AI 与 OpenAI 的 DALL-E 3 集成&#xff0c;以生成图像。轻松设置 Spring Boot、配置 API 集成并自定义设置。 大家好&#xff01;这是关于 Spring AI 系列介绍文章的第一篇。今天&#xff0c;我们将了解如何通过文本提示轻松生成图片。为此&#xff0c;我们将利用 …

Go优雅实现redis分布式锁

前言 系统为了保证高可用&#xff0c;通常会部署多实例&#xff0c;并且会存在同时对共享资源并发读写&#xff0c;这时候为了保证读写的安全&#xff0c;常规手段是会引入分布式锁&#xff0c;本文将介绍如何使用redis设计一个优雅的Go分布式锁。 设计 redis分布式锁是借助…