本文介绍了服务器端验证(使用StringBuilder对象,存储错误消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Hello guys 我正在尝试进行服务器端验证。我在StringBuilder对象中存储错误消息。如果,我以错误的格式输入值,那么它工作正常.....但是,如果我以正确的格式输入值,则无法显示成功消息... 我发现问题与StringBuilder对象有关。但无法弄清楚确切的问题。 如果我使用length(),方法而不是equals()方法那么它工作正常。 ...请帮我解决这个问题Hello guysI am trying to do server side validation. i am storing error messages in StringBuilder object. If, I enter values in wrong format, Then it is working fine.....but ,if I enter values in proper format, it is failing to display success msg...I found that Problem is with StringBuilder object. but unable to figure out the exact problem.If I use length(), method instead of equals() method then it works fine....pls help me to sort out this issuepublic void validateForm(ActionRequest request, ActionResponse actionResponse){ String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String telephone = request.getParameter("phone"); String email = request.getParameter("email"); String comments= request.getParameter("comments"); StringBuilder errorMsg=new StringBuilder(); //StringBuffer errorMsg=new StringBuffer(); System.out.println(errorMsg.equals("")); System.out.println(errorMsg.length()); System.out.println("errrr="+errorMsg); String fname="[a-zA-z]+([ '-][a-zA-Z]+)*"; String lname="[a-zA-z]+([ '-][a-zA-Z]+)*"; String matchPhoneNumberPattern="[0-9]*"; String emailPattern="^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"; if (firstName == null ||firstName.equals("")) { errorMsg.append("Please Enter First Name "+"<br />"); } else if(!firstName.matches(fname)) { errorMsg.append("Please Enter First Namet "+"<br />"); } if(lastName == null ||lastName .isEmpty()) { errorMsg.append("Please Enter Last Name "+"<br />"); } else if(!lastName.matches(lname)) { errorMsg.append("Enter lastname in Name Format "+"<br />"); } if(telephone == null || telephone.isEmpty()) { errorMsg.append("Please Enter Telephone Number "+"<br />"); } else if(!telephone.matches(matchPhoneNumberPattern)) { errorMsg.append("Invalid Number please Enter valid number"+"<br />"); } if(email == null || email.isEmpty()) { errorMsg.append("Please Enter Email "+"<br />"); } else if(!email.matches(emailPattern)) { errorMsg.append("Invalid email id"+"<br />"); } if (comments == null || comments.equals("")) { errorMsg.append("You must Enter Comments "+"<br />"); } /*IN all cases this if block is executing,EVen though i enter all fields in proper format */ System.out.println(errorMsg.equals("")); if(!errorMsg.equals("")) { request.setAttribute("errorMsg", errorMsg); request.setAttribute("phone", telephone); request.setAttribute("email", email); System.out.println("If block..........!"); } else { String successMsg="Form Submited Successfully"; request.setAttribute("successMsg", successMsg); System.out.println("done..........!"); } }推荐答案if(!errorMsg.equals("")) 然后,将此行更改为Then,change this line toif(errorList.size()>0){}if(errorMsg != String.Empty) 而不是instead ofif(!errorMsg.equals("")) 顺便说一句,你实际上可以使用System.Text.StringBuilder,因为你不需要处理拳击它会更便宜和解拳。Btw, you can actually use the System.Text.StringBuilder, it would be less expensive to use because you don't have to deal with boxing and un-boxing. 这篇关于服务器端验证(使用StringBuilder对象,存储错误消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 04:30