java项目中html_Java如何获取项目中的Html文件内容

前言

在单页面应用(SPA,Single Page Application)开发中,点击不同的菜单,通常需要动态获取其对应的Html页面代码,返回给前端,再将这一整块append到主框架页面的某个指定div中。所以,Java如何获取Html代码呢?

开发环境中

如在Eclipse中开发一个基于SpringBoot的SPA,每次右键-Run As/Debug As主类来运行项目,那如何找到某指定页面的html文件呢?如下:

/**

* 获取对应pageName的html内容(本地eclipse里直接run)

*/

private String getHtmlByPageName(String pageName) {

URL url = this.getClass().getClassLoader().getResource("templates/" + pageName + ".html");

LOGGER.info("pageName : {} - {}", pageName, url);

// File f = new File("E:\\code_svn\\srp_trunk\\target\\classes\\templates\\404.html");

StringBuffer sb = new StringBuffer();

BufferedInputStream bis = null;

try {

File f = new File(url.toURI());

FileInputStream fis = new FileInputStream(f);

bis = new BufferedInputStream(fis);

int len = 0;

byte[] temp = new byte[1024];

while ((len = bis.read(temp)) != -1) {

sb.append(new String(temp, 0, len));

}

LOGGER.debug("page content:\n{}...", sb.toString().substring(0, 200));

} catch (Exception e) {

LOGGER.error("Error occurred, cause by: ", e);

} finally {

if (bis != null) {

try {

bis.close();

} catch (IOException e) {

LOGGER.error("Error occurred, cause by: ", e);

}

}

}

return sb.toString();

}

生产环境中

在生产环境中,不同于开发环境,我们经常需要java -jar来运行目标jar包,那在SpringBoot打出来的jar包中,如何找到某指定页面的html文件呢?如下:

/**

* 获取对应pageName的html内容(生产环境java -jar直接run)

*/

private String getHtmlByPageName2(String pageName) throws IOException {

// /BOOT-INF/classes/templates/dashboard.html

String path = "/BOOT-INF/classes/templates/" + pageName + ".html";

// 返回读取指定资源的输入流

InputStream is = this.getClass().getResourceAsStream(path);

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String s = "";

StringBuffer sb = new StringBuffer();

while ((s = br.readLine()) != null) {

sb.append(s).append("\n");

}

return sb.toString();

}

注意每次readLine后都别忘了append换行符,否则会导致html中的

所以,我们需要在代码中,判断当前程序所处的环境是开发环境还是生产环境,以进入不同的获取页面代码的逻辑。

SpringBoot多环境配置

一般可放置多个application-{profile}.properties文件,并在application.properties中设置一默认环境,如下:

spring.profiles.active=test

要想改变运行环境,可修改上述配置为dev或其他,亦可通过命令行参数的方式设置,如下:

java -jar xxx.jar --spring.profiles.active=dev

判断SpringBoot运行环境

import java.util.Locale;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

@Component

public class SpringContextUtil implements ApplicationContextAware {

private static ApplicationContext context = null;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.context = applicationContext;

}

// 传入线程中

public static T getBean(String beanName) {

return (T) context.getBean(beanName);

}

// 国际化使用

public static String getMessage(String key) {

return context.getMessage(key, null, Locale.getDefault());

}

/// 获取当前环境

public static String getActiveProfile() {

return context.getEnvironment().getActiveProfiles()[0];

}

}

因此, 对于Java根据SpringBoot运行环境的不同,而走不同的getHtmlByPageName逻辑,如下:

String profile = SpringContextUtil.getActiveProfile();

if ("dev".equals(profile)) {

return getHtmlByPageName(pageName);

} else {

return getHtmlByPageName2(pageName);

}

以上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值