jsp页面打jar包及其引用

1、前言

项目开发中,需要把一些公共页面抽取到公用的项目中,打成jar包全局使用,便于修改和维护。最常见的就是会把公用的方法或者实体类打成jar包,这种很好用,直接导入依赖,导包就完事了。但是页面的打包笔者开发的时候,搞了好久,终于搞定。记录一下,方便日后使用。

2、页面的打包

  • 正确的打包方式,开发者可以像页面在项目中一样的方式去引用(js、css等同理)。

  • jar工程只用两个主要的文件夹src/main/java和src/main/resources,前者只要用来存放java代码,后者存放页面和配置文件。在maven打jar包的时候,会生成这样的结构,如图:

  • 注意,因为是jar项目,所有不会打包webapp下的文件。

3、jsp页面的打包及使用

  • 在Servlet3.0协议规范中指出:${jar}/META-INF/resources/被视为根目录。那么将jsp等静态资源打入META-INF/resources/目录下就与实际项目没有区别了。

所以我们只需要在src/main/resources的文件夹下创建META-INF/resources/目录,然后将引用的jsp文件放在该目录下,即可正常引用。

  • 官方文档摘录:

The getResource and getResourceAsStream methods take a String with a leading “/” as an argument that gives the path of the resource relative to the root of the context or relative to the META-INF/resources directory of a JAR file inside the web application’s WEB-INF/lib directory.

These methods will first search the root of the web application context for the requested resource before looking at any of the JAR files in the WEB-INF/lib directory.

The order in which the JAR files in the WEB-INF/lib directory are scanned is undefined.

This hierarchy of documents may exist in the server’s file system, in a Web application archive file, on a remote server, or at some other location.

4、JSP打包demo

  • jar工程结构:
    这里写图片描述
    这样只要引入该jar就可以正常引入jsp页面了。

  • 页面引用:

<jsp:include page="/jsp/authentication/header.jsp"></jsp:include>

5、针对留言的笔友的问题回答以及注意事项

针对留言以及个别笔友私信我,打出的公用jar包在其他项目不可用的问题,笔者特地自己重新回顾并简单搭建了一次,整理一下遇到的问题:
5.1 公用jsp的封装,需要打成jar包。jsp页面需放置在src/main/resources的文件夹下创建的META-INF/resources/下。
注意:创建META-INF/resources这两个文件夹时,注意确认是否是两个文件夹。因为使用idea创建是使用META-INF.resources默认是一个文件夹,到时候会运行会失效
5.2 客户端使用springboot是,需要在src/main/下创建webapp文件夹,jsp页面放在webapp下,否则访问会404.
在这里插入图片描述5.3 pom的配置。springboot默认是不支持的jsp的,需要引入对jsp支持的依赖

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <scope>provided</scope>
 </dependency>
 <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>
 </dependency>
 <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <scope>provided</scope>
 </dependency>

5.4 pom打包的配置,使用jar -jar运行时需要将webapp下的jsp打包到META-INF/resources下。

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.xml</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            
            <!-- 这个是关键!!!!!!!!!!! -->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
    </build>

5.5 特别注意,springboot官方文档指出使用jsp的访问不支持java -jar xxx.jar,需要打成war包。同样使用java -jar xxx.war可行。使用使用jar运行,idea编辑器是可行的,但是直接使用命令还是会报404.
在这里插入图片描述pom中 配置<packaging>war</packaging>,打成war包

5.6 需要源码私信我,访问地址:http://localhost:8080/test

5.7 运行结果
在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
jar包和打war包的区别在于它们的用途和打包的内容不同。一般来说,jar包是Java程序打的,包含Java类的普通库、资源和辅助文件等,通常用于开发时要引用通用类,打成包便于存放管理。而war包是Java Web程序打的,是一个完整的可运行的Java Web程序,包含了Web应用程序的所有内容,例如JSP、Servlet、HTML、JavaScript、CSS、图片等,打成war包部署到服务器,目的是为了节省资源,提供效率。 下面是打jar包和打war包的示例代码: 1.打jar包 ```shell # 编译Java源代码 javac HelloWorld.java # 创建清单文件 echo Main-Class: HelloWorld > MANIFEST.MF # 打jar包 jar cvfm HelloWorld.jar MANIFEST.MF HelloWorld.class ``` 2.打war包 ```shell # 创建WEB-INF目录 mkdir -p WEB-INF/classes # 编译Java源代码 javac -d WEB-INF/classes HelloWorld.java # 创建web.xml文件 echo '<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>HelloWorld</display-name> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>' > WEB-INF/web.xml # 打war包 jar cvf HelloWorld.war * ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值