我的问题目前与设计有关。我有一个 jsf 参数页面,它提交不同的参数来生成碧玉报告。例如国籍、旅行类型、签证类型、性别等。参数可以是组合。例如,有一次用户可以选择国籍和签证类型并将其他留空,这将使其他人的默认值为 ALL。我从参数页面提交 id。如果用户没有选择任何内容,我将在 setter 方法中手动将值设置为 ALL。这是我的托管 bean 方法和 POJO 的快照。
private ReportBean generateReportBean(TravelDetailSearchParams searchParams, String reportPath){
TravelDetailReportBean travelDetailReportBean = new TravelDetailReportBean();
if(searchParams.getGender().getId() != 0){
for(Lookup lookup : gender){
if(lookup.getId() == searchParams.getGender().getId()){
travelDetailReportBean.setGender(lookup.getDescEnglish());
break ;
}
}
}
else{
travelDetailReportBean.setGender(searchParams.getGender().getDescEnglish());
}
if(searchParams.getTravelType().getId() != 0){
for(Lookup lookup : travelType){
if(lookup.getId() == searchParams.getTravelType().getId()){
travelDetailReportBean.setTravelType(lookup.getDescEnglish());
break ;
}
}
}
else{
travelDetailReportBean.setTravelType(searchParams.getTravelType().getDescEnglish());
}
if(searchParams.getPort().getId() != 0){
for(Lookup lookup : port){
if(lookup.getId() == searchParams.getPort().getId()){
travelDetailReportBean.setPort(lookup.getDescEnglish());
break ;
}
}
}
else{
travelDetailReportBean.setPort(searchParams.getPort().getDescEnglish());
}
if(searchParams.getNationality().getId() != 0){
for(Lookup lookup : country){
if(lookup.getId() == searchParams.getNationality().getId()){
travelDetailReportBean.setCountry(lookup.getDescEnglish());
break ;
}
}
}
else{
travelDetailReportBean.setCountry(searchParams.getNationality().getDescEnglish());
}
if(searchParams.getVisaType().getId() != 0){
for(Lookup lookup : visaType){
if(lookup.getId() == searchParams.getVisaType().getId()){
travelDetailReportBean.setVisaType(lookup.getDescEnglish());
break ;
}
}
}
else{
travelDetailReportBean.setVisaType(searchParams.getVisaType().getDescEnglish());
}
logger.debug("nationality: " + travelDetailReportBean.getCountry());
logger.debug("travelType: " + travelDetailReportBean.getTravelType());
logger.debug("visatype: " + travelDetailReportBean.getVisaType());
logger.debug("port: " + travelDetailReportBean.getPort());
travelDetailReportBean.setReportName(BorderEntryExitConstants.TRAVEL_DETAIL_REPORT_NAME);
travelDetailReportBean.setReportPath(reportPath);
return travelDetailReportBean ;
}
POJO代码如下所示
public class TravelDetailReportBean extends ConcreteReportBean {
private String gender ;
private String travelType ;
private String port ;
private String country ;
private String visaType;
public String getGender() {
return gender;
}
public void setGender(String gender) {
if(gender == null || gender.equals("")){
this.gender="ALL";
}
else{
this.gender = gender;
}
}
public String getTravelType() {
return travelType;
}
public void setTravelType(String travelType) {
if(travelType == null || travelType.equals("")){
this.travelType ="ALL";
}
else{
this.travelType = travelType;
}
}
public String getPort() {
return port;
}
public void setPort(String port) {
if(port == null || port.equals("")){
this.port ="ALL";
}
else{
this.port = port;
}
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
if(country == null || country.equals("")){
this.country ="ALL";
}
else{
this.country = country;
}
}
public String getVisaType() {
return visaType;
}
public void setVisaType(String visaType) {
if(visaType == null || visaType.equals("")){
this.visaType ="ALL";
}
else{
this.visaType = visaType;
}
}
}
问题是 generateReportBean 方法。我正在使用许多 if 来查看 id 是否不为零从查找中获取该 id 的描述,否则只需像这样设置它,在 bean setter 内部我正在检查 null。如果为 null,则将其设置为 ALL。
我的问题是目前我的参数很少,这些如果可以工作一段时间,但如果搜索参数增长怎么办。如果会看起来很难看。有人可以建议我一个更好的方法来摆脱这些如果。谢谢,彼得