springboot集成freemark国际化问题(完美方案)

1、ftl页面引入spring.ftl

<#import "spring.ftl" as spring />

 

2、ftl页面中国际化使用示例

<@spring.message "base.site"/>

 

3、application.properties里添加spring.messages.basename=i18n/messages

resources/目录下创建文件messages.propertiesmessages_zh.propertiesmessages_en.properties

 

4、在获取ftl页面内容的时候实现国际化

ByteArrayOutputStream output = new ByteArrayOutputStream();

try{

Template tp = config.getTemmplate("test.ftl");

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();

HttpServletRequest response= ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getResponse();

RequestContext context = new RequestContext(request,response,request.getServletContext(),values);

context.changeLocale(Locale.CHINA);//国际化切换

values.put("springMacroRequestContext",requestContext);//这句是关键

tp.process(values, new OutputStreamWriter(output));//values为ftl初始化的map数据

}catch(Exception e){

e.printStackTrace();

}

 

以下是使用Spring Boot和Freemarker根据Word模板生成Word的详细实现方法: ### 1. 项目依赖 在`pom.xml`文件中添加Spring Boot、Freemarker和Apache POI的依赖: ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Starter Freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- Apache POI for Word processing --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> </dependencies> ``` ### 2. 准备Word模板 创建一个Word模板文件(例如`template.docx`),并在需要动态填充数据的位置使用Freemarker的占位符,如`${name}`、`${age}`等。将模板文件放在`src/main/resources/templates`目录下。 ### 3. 配置Freemarker Spring Boot会自动配置Freemarker,通常不需要额外的配置。但可以在`application.properties`或`application.yml`中进行一些自定义配置: ```properties spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.suffix=.ftl ``` ### 4. 创建数据模型 创建一个Java类来表示要填充到Word模板中的数据: ```java public class User { private String name; private int age; // 构造函数、Getter和Setter方法 public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` ### 5. 生成Word文件 创建一个服务类来处理Word文件的生成: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.*; import java.util.HashMap; import java.util.Map; @Service public class WordGeneratorService { @Autowired private Configuration freemarkerConfig; public void generateWord(User user, String outputPath) throws IOException, TemplateException { // 加载Freemarker模板 Template template = freemarkerConfig.getTemplate("template.ftl"); // 准备数据模型 Map<String, Object> data = new HashMap<>(); data.put("name", user.getName()); data.put("age", user.getAge()); // 将模板和数据合并为文本 StringWriter writer = new StringWriter(); template.process(data, writer); String content = writer.toString(); // 创建Word文档 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(content); // 保存Word文档 try (FileOutputStream out = new FileOutputStream(outputPath)) { document.write(out); } } } ``` ### 6. 控制器层调用 创建一个控制器来处理生成Word文件的请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import freemarker.template.TemplateException; @RestController public class WordController { @Autowired private WordGeneratorService wordGeneratorService; @GetMapping("/generate-word") public String generateWord() { User user = new User("John Doe", 30); String outputPath = "output.docx"; try { wordGeneratorService.generateWord(user, outputPath); return "Word file generated successfully at: " + outputPath; } catch (IOException | TemplateException e) { e.printStackTrace(); return "Error generating Word file: " + e.getMessage(); } } } ``` ### 7. 运行项目 启动Spring Boot应用程序,访问`http://localhost:8080/generate-word`,即可生成包含动态数据的Word文件。 ### 总结 通过以上步骤,实现了使用Spring Boot和Freemarker根据Word模板生成Word文件的功能。主要步骤包括添加依赖、准备模板、配置Freemarker、创建数据模型、生成Word文件和提供控制器接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值