freemarker 处理 HashMap<Integer, String>()

本文介绍在Freemarker模板引擎中处理整型数据的方法。由于Freemarker不支持直接从Integer类型获取value,因此需要将Map中的int类型的key转换为String类型。文章提供了具体的代码示例,演示了如何在java后台代码中创建Map,并在html代码中通过Freemarker正确显示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在freemarker中无法通过Integer获取value所有必须将key转换成String

如:HashMap<String,String>

如果存的数据是int类型在数据后面添加一个 ( +“” )将数据转换成String类型

 

代码如下:

后台java代码:Map<String,String> _mapList = new HashMap<String, String>();

html代码:
<#list _mapList?keys as key>
  <option value="${key}">_mapList[key]</option>
</#list>

转载于:https://www.cnblogs.com/NuLLBTF/p/10077854.html

您可以使用FreeMarker作为模板引擎,通过编写模板来自动生成所需的实体类、指令、Controller、Service、Dao和HTML等文件。以下是一些示例模板以供参考: 实体类模板: ```java package ${basePackage}.entity; import lombok.Data; @Data public class ${className} { <#list properties as prop> /** * ${prop.comment} */ private ${prop.type} ${prop.name}; </#list> } ``` 指令模板: ```java package ${basePackage}.command; import lombok.Data; @Data public class ${className}Command { <#list properties as prop> /** * ${prop.comment} */ private ${prop.type} ${prop.name}; </#list> } ``` Controller模板: ```java package ${basePackage}.controller; import ${basePackage}.command.${className}Command; import ${basePackage}.entity.${className}; import ${basePackage}.service.${className}Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/${classPath}") public class ${className}Controller { @Autowired private ${className}Service ${className?uncap_first}Service; @PostMapping public ${className} create(@RequestBody ${className}Command ${className?uncap_first}Command) { return ${className?uncap_first}Service.create(${className?uncap_first}Command); } @GetMapping("/{id}") public ${className} getById(@PathVariable Long id) { return ${className?uncap_first}Service.getById(id); } @PutMapping("/{id}") public ${className} updateById(@PathVariable Long id, @RequestBody ${className}Command ${className?uncap_first}Command) { return ${className?uncap_first}Service.updateById(id, ${className?uncap_first}Command); } @DeleteMapping("/{id}") public void deleteById(@PathVariable Long id) { ${className?uncap_first}Service.deleteById(id); } } ``` Service模板: ```java package ${basePackage}.service; import ${basePackage}.command.${className}Command; import ${basePackage}.entity.${className}; import ${basePackage}.dao.${className}Dao; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ${className}Service { @Autowired private ${className}Dao ${className?uncap_first}Dao; public ${className} create(${className}Command ${className?uncap_first}Command) { ${className} ${className?uncap_first} = new ${className}(); BeanUtils.copyProperties(${className?uncap_first}Command, ${className?uncap_first}); return ${className?uncap_first}Dao.save(${className?uncap_first}); } public ${className} getById(Long id) { return ${className?uncap_first}Dao.getById(id); } public ${className} updateById(Long id, ${className}Command ${className?uncap_first}Command) { ${className} ${className?uncap_first} = ${className?uncap_first}Dao.getById(id); BeanUtils.copyProperties(${className?uncap_first}Command, ${className?uncap_first}); return ${className?uncap_first}Dao.save(${className?uncap_first}); } public void deleteById(Long id) { ${className?uncap_first}Dao.deleteById(id); } } ``` Dao模板: ```java package ${basePackage}.dao; import ${basePackage}.entity.${className}; import org.springframework.data.jpa.repository.JpaRepository; public interface ${className}Dao extends JpaRepository<${className}, Long> { } ``` HTML模板: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>${className}列表</title> </head> <body> <h1>${className}列表</h1> <table> <thead> <tr> <th>ID</th> <#list properties as prop> <th>${prop.comment}</th> </#list> <th>操作</th> </tr> </thead> <tbody> <#list dataList as data> <tr> <td>${data.id}</td> <#list properties as prop> <td>${data[prop.name]}</td> </#list> <td> <a href="/${classPath}/${data.id}">详情</a> <a href="/${classPath}/${data.id}/edit">编辑</a> <a href="/${classPath}/${data.id}?_method=DELETE">删除</a> </td> </tr> </#list> </tbody> </table> <a href="/${classPath}/new">新建${className}</a> </body> </html> ``` 以上仅是模板的示例,您可以根据自己的需求进行修改和扩展。使用FreeMarker生成文件的具体步骤如下: 1. 定义模板文件,可以使用FreeMarker的语法编写模板; 2. 定义模板数据,可以是Java对象或者Map等; 3. 使用FreeMarker的Template类加载模板文件,得到Template对象; 4. 创建Writer对象,将渲染后的模板内容输出到文件中。 以下是一个示例代码,用于生成实体类文件: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class EntityGenerator { public static void generate(String basePackage, String className, Map<String, Object> properties) throws IOException, TemplateException { Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); cfg.setClassLoaderForTemplateLoading(EntityGenerator.class.getClassLoader(), "templates"); cfg.setDefaultEncoding("UTF-8"); Template template = cfg.getTemplate("entity.ftl"); Map<String, Object> data = new HashMap<>(); data.put("basePackage", basePackage); data.put("className", className); data.put("properties", properties); File file = new File(String.format("%s/src/main/java/%s/entity/%s.java", System.getProperty("user.dir"), basePackage.replaceAll("\\.", "/"), className)); FileWriter writer = new FileWriter(file); template.process(data, writer); writer.close(); System.out.println(String.format("Entity generated: %s", file.getAbsolutePath())); } } ``` 使用方式示例: ```java Map<String, Object> properties = new HashMap<>(); properties.put("id", new Property("Long", "ID")); properties.put("name", new Property("String", "名称")); properties.put("age", new Property("Integer", "年龄")); EntityGenerator.generate("com.example.demo", "User", properties); ``` 这段代码将会生成一个名为User.java的实体类文件,包含id、name和age三个属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值