DTO或Form中的属性命名时,属性名称的前两个字母必须小写

本文讲述了在Java项目中,特别是DTO和Form对象中的属性命名规则。强调了所有属性名称必须遵循Java命名规范,且前两个字符必须小写的重要性。通过一个具体例子说明了不遵守这一规则可能导致的问题。
DTO或Form中的属性命名时,属性名称的前两个字母必须小写。
今天在一个系统中发现了一个问题,在某DTO中有一个属性名称为 tClassType ,但是,
当我在jsp页面中使用时,后台老是报
No getter method for property tClassType of bean org.apache.struts.taglib.html.BEAN,经过确认,
该DTO确实有这个属性的getter method。后经过网上搜索,发现是该属性的命名不规范,该属性的第二字母没有小写。

【结论: 注意表单属性的命名规则:所有属性名称必须遵照java命名规范,并且前两个字符必须小写】
Java 中,将一个 DTO 中的属性赋值给另一个 DTO 并删除第一个 DTO两个属性,可以通过以下步骤实现。首先,手动进行属性赋值,然后将第一个 DTO 中要删除的属性置为 `null` 以达到“删除”的效果。 以下是示例代码: ```java class SourceDTO { private String property1; private String property2; private String property3; private String property4; // 构造函数、getter 和 setter 方法 public SourceDTO() {} public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } public String getProperty3() { return property3; } public void setProperty3(String property3) { this.property3 = property3; } public String getProperty4() { return property4; } public void setProperty4(String property4) { this.property4 = property4; } } class TargetDTO { private String property1; private String property2; private String property3; private String property4; // 构造函数、getter 和 setter 方法 public TargetDTO() {} public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } public String getProperty3() { return property3; } public void setProperty3(String property3) { this.property3 = property3; } public String getProperty4() { return property4; } public void setProperty4(String property4) { this.property4 = property4; } } public class DTOConverter { public static void convertAndModify(SourceDTO source, TargetDTO target, String propertyToRemove1, String propertyToRemove2) { // 将 SourceDTO属性赋值给 TargetDTO target.setProperty1(source.getProperty1()); target.setProperty2(source.getProperty2()); target.setProperty3(source.getProperty3()); target.setProperty4(source.getProperty4()); // 删除 SourceDTO 中的指定属性 if ("property1".equals(propertyToRemove1)) { source.setProperty1(null); } else if ("property2".equals(propertyToRemove1)) { source.setProperty2(null); } else if ("property3".equals(propertyToRemove1)) { source.setProperty3(null); } else if ("property4".equals(propertyToRemove1)) { source.setProperty4(null); } if ("property1".equals(propertyToRemove2)) { source.setProperty1(null); } else if ("property2".equals(propertyToRemove2)) { source.setProperty2(null); } else if ("property3".equals(propertyToRemove2)) { source.setProperty3(null); } else if ("property4".equals(propertyToRemove2)) { source.setProperty4(null); } } public static void main(String[] args) { SourceDTO source = new SourceDTO(); source.setProperty1("value1"); source.setProperty2("value2"); source.setProperty3("value3"); source.setProperty4("value4"); TargetDTO target = new TargetDTO(); convertAndModify(source, target, "property2", "property4"); System.out.println("Target DTO - Property1: " + target.getProperty1()); System.out.println("Target DTO - Property2: " + target.getProperty2()); System.out.println("Target DTO - Property3: " + target.getProperty3()); System.out.println("Target DTO - Property4: " + target.getProperty4()); System.out.println("Source DTO - Property1: " + source.getProperty1()); System.out.println("Source DTO - Property2: " + source.getProperty2()); System.out.println("Source DTO - Property3: " + source.getProperty3()); System.out.println("Source DTO - Property4: " + source.getProperty4()); } } ``` 在上述代码中,`convertAndModify` 方法实现了属性赋值和属性删除的功能。首先将 `SourceDTO` 的属性值赋给 `TargetDTO`,然后根据传入的要删除的属性名称,将 `SourceDTO` 中对应的属性置为 `null`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值