setDetail.getRelatedItemNo().equals(waybillItemNo)这个代码setDetail.getRelatedItemNo()为空会报错空指针,有没有什么方法在对比字符串的时候不会报错空指针,我记得StringUtils里面好像有这样的方法?
时间: 2024-10-15 14:14:06 浏览: 20
确实,在Spring框架的`org.springframework.util.StringUtils`类中有提供避免空指针异常的方法,比如`StringUtils.isEmpty()`或`StringUtils.isNotBlank()`。你可以先判断`setDetail.getRelatedItemNo()`是否为空,然后再进行字符串比较。例如:
```java
String waybillItemNo = ...; // 待比较的值
String relatedItemNo = setDetail.getRelatedItemNo(); // 可能为空的字段
if (StringUtils.isNotEmpty(relatedItemNo)) {
if (StringUtils.equals(relatedItemNo, waybillItemNo)) {
// 字符串相等的操作
} else {
// 字符串不等的操作
}
} else {
// 相关项不存在或未获取到的情况
}
```
这样,如果`relatedItemNo`为空,就不会抛出空指针异常了。
相关问题
优化这段代码if (ResMeta.PROTOCOL_HTTPS.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_HTTP.equals(token.getLoginProtocol())) { ssoTicket.setHostDomainName(token.getTargetIp()); ssoTicket.setWebappUrl(token.getLoginProtocol() + "://" + token.getTargetIp()); ssoTicket.setAgreementType(token.getLoginProtocol()); if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) { String err = "加载应用发布错误"; rv.setDetail(err); rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR); logger.error(err); return null; } } else if (ResMeta.RESTYPE_CSAPP.equals(token.getResType())){ if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) { String err = "加载应用发布错误"; rv.setDetail(err); rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR); logger.error(err); return null; } } else if (ResMeta.PROTOCOL_RDP.equals(token.getLoginProtocol())) { if (ssoAcc != null && ssoAcc.getWinAccType() == AccMeta.WIN_ACC_DOMAIN_TYPE) { this.fillHostDomain(ssoAcc, ssoTicket, token.getAccName()); } else if (resInfo.getHostName() != null && resInfo.getHostName().length() > 0) { ssoTicket.setAcc(resInfo.getHostName() + "\\" + ssoTicket.getAcc()); } } else if (ResMeta.PROTOCOL_SSH2.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_SSH1.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_TELNET.equals(token.getLoginProtocol())) { CMDSSOTicket cmdTicket = (CMDSSOTicket) ssoTicket; /* 设置提权(原5.0的扩展策略)如果存在工单号 按工单处理 规则授权暂时不支持设置提权 */ if (token.getWorkId() != null && token.getWorkId().length() > 0) { try { SSOWorkOrderAuthz authz = new SSOWorkOrderAuthz(); authz.setWorkOrderId(token.getWorkId()); authz.setResId(token.getResId()); int suPermission = ssoWorkOrderRepository.findWorkOrderAuthcSuPermission(authz); if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) { cmdTicket.setSu_cmd(resInfo.getSuCmd()); cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc())); cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt()); } } catch (Exception e) { } } else if (token.getAuthzId() != null && token.getAuthzId().length() > 0) { try { int suPermission = ssoResroleRepository.findResroleAuthcSuPermission(token.getAuthzId()); if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) { cmdTicket.setSu_cmd(resInfo.getSuCmd()); cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc())); cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt()); } } catch (Exception e) { } }
这段代码可以进行如下优化:
1. 将重复的代码块提取出来,避免代码重复:
```java
if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) {
String err = "加载应用发布错误";
rv.setDetail(err);
rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR);
logger.error(err);
return null;
}
```
可以将其提取为一个方法,减少代码冗余。
2. 使用 switch-case 替代多个 if-else if 语句:
```java
switch (token.getLoginProtocol()) {
case ResMeta.PROTOCOL_HTTPS:
case ResMeta.PROTOCOL_HTTP:
ssoTicket.setHostDomainName(token.getTargetIp());
ssoTicket.setWebappUrl(token.getLoginProtocol() + "://" + token.getTargetIp());
ssoTicket.setAgreementType(token.getLoginProtocol());
break;
case ResMeta.RESTYPE_CSAPP:
if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) {
String err = "加载应用发布错误";
rv.setDetail(err);
rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR);
logger.error(err);
return null;
}
break;
case ResMeta.PROTOCOL_RDP:
if (ssoAcc != null && ssoAcc.getWinAccType() == AccMeta.WIN_ACC_DOMAIN_TYPE) {
this.fillHostDomain(ssoAcc, ssoTicket, token.getAccName());
} else if (resInfo.getHostName() != null && resInfo.getHostName().length() > 0) {
ssoTicket.setAcc(resInfo.getHostName() + "\\" + ssoTicket.getAcc());
}
break;
case ResMeta.PROTOCOL_SSH2:
case ResMeta.PROTOCOL_SSH1:
case ResMeta.PROTOCOL_TELNET:
CMDSSOTicket cmdTicket = (CMDSSOTicket) ssoTicket;
if (token.getWorkId() != null && token.getWorkId().length() > 0) {
try {
SSOWorkOrderAuthz authz = new SSOWorkOrderAuthz();
authz.setWorkOrderId(token.getWorkId());
authz.setResId(token.getResId());
int suPermission = ssoWorkOrderRepository.findWorkOrderAuthcSuPermission(authz);
if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) {
cmdTicket.setSu_cmd(resInfo.getSuCmd());
cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc()));
cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt());
}
} catch (Exception e) {
}
} else if (token.getAuthzId() != null && token.getAuthzId().length() > 0) {
try {
int suPermission = ssoResroleRepository.findResroleAuthcSuPermission(token.getAuthzId());
if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) {
cmdTicket.setSu_cmd(resInfo.getSuCmd());
cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc()));
cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt());
}
} catch (Exception e) {
}
}
break;
}
```
使用 switch-case 可以使代码更加简洁和易读。
这些优化可以提高代码的可维护性和可读性。
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String action = req.getParameter("action"); StudentService ss = new StudentServiceImpl(); Person person = (Person) req.getSession().getAttribute("session_person"); String userAccount = person.getUserAccount(); ManageService ms = new ManageServiceImpl(); if(action.equals("index")){ //跳转到首页 req.setAttribute("mainRight","blank.jsp"); req.getRequestDispatcher("main.jsp").forward(req,resp); }else if(action.equals("goMyDetail")){ req.setAttribute("mainRight","detail.jsp"); req.getRequestDispatcher("main.jsp").forward(req,resp); }else if(action.equals("upPassword")){ //upPassword req.getRequestDispatcher("upPassword.jsp").forward(req,resp); }else if(action.equals("sureUpPassword")){ //common.action?action=sureUpPassword String userPassword = req.getParameter("userPassword"); ms.sureUpPassword(userAccount,userPassword); req.setAttribute("tip","修改成功,请关闭此页面"); req.getRequestDispatcher("success.jsp").forward(req,resp); } }
这段代码是一个 Java Servlet 中的 service 方法,用于处理来自客户端的 HTTP 请求。首先,它通过 req.getParameter("action") 获取请求参数 action 的值,并且设置请求编码为 UTF-8。
接下来,它根据 action 参数的不同值来执行不同的操作。如果 action 等于 "index",则将 "blank.jsp" 设置为请求属性 mainRight,并且将请求转发到 "main.jsp" 页面。如果 action 等于 "goMyDetail",则将 "detail.jsp" 设置为请求属性 mainRight,并且将请求转发到 "main.jsp" 页面。如果 action 等于 "upPassword",则将请求转发到 "upPassword.jsp" 页面。如果 action 等于 "sureUpPassword",则通过 req.getParameter("userPassword") 获取用户输入的密码,调用 ms.sureUpPassword() 方法修改密码,并且将 "修改成功,请关闭此页面" 设置为请求属性 tip,然后将请求转发到 "success.jsp" 页面。
在方法的开头,它还通过 req.getSession().getAttribute("session_person") 获取了当前登录用户的信息,并且从中获取了用户账号(userAccount)。同时,它还创建了一个 StudentService 和 ManageService 的实例,用于执行业务操作。
阅读全文