java 后端校验_java后端校验

1 /**

2 *3 */

4 packagecom.linker.util;5

6 importjava.io.File;7 importjava.io.IOException;8 importjava.util.ArrayList;9 importjava.util.Iterator;10 importjava.util.List;11 importjava.util.Map;12 importjava.util.Set;13

14 importjavax.xml.parsers.DocumentBuilder;15 importjavax.xml.parsers.DocumentBuilderFactory;16 importjavax.xml.parsers.ParserConfigurationException;17

18 importorg.w3c.dom.Document;19 importorg.w3c.dom.Element;20 importorg.w3c.dom.Node;21 importorg.w3c.dom.NodeList;22 importorg.xml.sax.SAXException;23

24 /**

25 * Title:Xmlparse4FormVerify26 * description: 读取xml中配置的表单验证信息27 * company: linker28 *@authorzhanjp29 * @date 2016年2月22日 上午9:33:5930 */

31 public classXmlparse4FormVerify {32

33 private static DocumentBuilder db = null;34

35 private static DocumentBuilderFactory dbf = null;36

37 private static Document dt = null;38

39 public staticXmlparse4FormVerify xp;40

41

42 /**

43 * 初始化DocumentBuilder等信息44 *@return

45 */

46 publicXmlparse4FormVerify getInstance(){47 if(xp==null){48 xp = newXmlparse4FormVerify();49 }50 returnxp;51 }52

53 static{54 try{55 //返回documentBuilderFactory

56 dbf =DocumentBuilderFactory.newInstance();57

58 db =dbf.newDocumentBuilder();59 String basePath = new Xmlparse4FormVerify().getClass().getClassLoader().getResource("/").getPath();60 File f = new File(basePath + "formVerify.xml");61 //获取xml文件的dom对象

62 dt =db.parse(f);63 } catch(ParserConfigurationException e) {64 e.printStackTrace();65 System.out.println("class:Xmlparse4FormVerify>>documentBuilder对象初始化失败。");66 } catch (SAXException |IOException e) {67 //TODO Auto-generated catch block

68 e.printStackTrace();69 System.out.println("class:Xmlparse4FormVerify>>解析formVerify.xml文档失败");70 }71 }72

73 /**

74 * 根据表单ID和表单参数进行校验75 *@paramformId 表单ID76 *@parampMap 表单参数77 *@returnflag-0:成功,1:失败; msg-消息78 *@throwsException79 */

80 public static String verifyForm(String formId,Map pMap)throwsException{81 if(dt==null){82 throw new Exception("未能正确初始化。");83 }84 String errorInfo = null;85 //开始解析xml文档86 //获取xml文件跟节点

87 Element element =dt.getDocumentElement();88 //获取根节点下的子节点列表

89 NodeList cList =element.getChildNodes();90 List forms = new ArrayList<>();91 for (int i = 0; i < cList.getLength(); i++) {92 Node tNode =cList.item(i);93 if(tNode.getNodeName().equals("form")){94 forms.add(tNode);95 }96 }97 for (int i = 0; i < forms.size(); i++) {98 Node node1 =forms.get(i);99 String nodeId = node1.getAttributes().getNamedItem("id").getNodeValue();100 //System.out.println("节点id:" + nodeId+","+"节点name"+node1.getNodeName());

101 if(nodeId.equals(formId)){102 NodeList cList2 =node1.getChildNodes();103 //开始校验

104 Set keys =pMap.keySet();105 Iterator iter =keys.iterator();106 while(iter.hasNext()) {107 String key =iter.next();108 for (int j = 0; j < cList2.getLength(); j++) {109 Node node =cList2.item(j);110 //System.out.println("clist2,node:"+node.getNodeName());

111 if(node.getNodeName().equals(key)){112 String value =pMap.get(key);113 String preText = "";114 String rule = "";115 NodeList cList3 =node.getChildNodes();116

117 for(int m = 0;m < cList3.getLength(); m++){118 if(cList3.item(m).getNodeName()=="text"){119 preText =cList3.item(m).getTextContent();120 }else if(cList3.item(m).getNodeName()=="rule"){121 rule =cList3.item(m).getTextContent();122 }123 }124 //TODO 校验

125 errorInfo = newVerifyRegularUtil().verify(value, preText, rule);126 if(errorInfo!=null){127 returnerrorInfo;128 }129 }130 }131

132 }133 break;134 }135 }136 returnerrorInfo;137 }138 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值