1、查看session下都存放了哪些值,并打印下来。
HttpSession session = request..getSession();
for ( Enumeration e = session.getAttributeNames(); e.hasMoreElements(); )
{ System.out.println(e.nextElement()); }2、select框的选择
$("#id").val() //获取select option的value值
$("#id option:selected").text() //获取选中的option的文本信息
3、县及县以上行政区划代码划分,转换成json格式
数据源:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/
步骤: 1.复制数据源(原始数据源是根据空格来划分的)。查找替换,将^(.*)$
替换为'$1',并给其设定一个var数组array。
2.新建.html页面,拷贝刚才的数据源,同时加入如下script代码。并运行代码,接着在控制台就能输出想要的结果。
1 //记录结果 2 var areas = []; 3 //堆栈记录层级信息 4 var stack = []; 5 //前一个区域和父节点 6 var prev, parent; 7 //缩进位置 8 var pos = 0; 9 for(var i = 0; i < array.length; i++){10 //正则匹配缩进的空格、代码、名称,[^\x00-\xff]+可以匹配中文11 var groups = /^(\s*)(\d+?)\s*([^\x00-\xff]+)$/g.exec(array[i]);12 var start = groups[1].length;13 var area = {code: groups[2], name: groups[3]};14 if(start == 0){15 //没有缩进时16 stack = [];17 parent = areas;18 } else if(start > pos){19 //如果有缩进,就使用上一个区域作为上级区域20 stack.push(parent);21 prev.children = [];22 parent = prev.children;23 } else if(start < pos){24 //取出上级区域25 parent = stack.pop();26 }27 parent.push(area);28 pos = start;29 prev = area;30 }31 console.log(JSON.stringify(areas,null,4));
输出格式:[ { "code": "110000", "name": "北京市",
"children": [ { "code": "110100", "name": "市辖区",
"children": [ { "code": "110101", "name": "东城区" },....
也可以直接获取到所有的省份、城市和地区的单独json格式串,代码见下:
1 //记录结果 2 var prov = []; 3 var city = []; 4 var county = []; 5 6 //第一个后继和第二个后继 7 var next, next2; 8 9 for(var i = 0; i < array.length; i++){10 //正则匹配缩进的空格、代码、名称,[^\x00-\xff]+可以匹配中文11 var groups = /^(\s*)(\d+?)\s*([^\x00-\xff]+)$/g.exec(array[i]);12 var start = groups[1].length;13 var area1 = {id: groups[2], prov: groups[3]};14 var area2 = {id: groups[2], city: groups[3], parent:''};15 var area3 = {id: groups[2], county: groups[3], parent:''};16 17 if(start == 0){18 //没有缩进时19 prov.push(area1);20 next = area1.id;21 }if(start == 1){22 //缩进为123 area2.parent = next;24 city.push(area2);25 next2 = area2.id;26 }if(start == 2){27 //缩进为228 area2.parent = next2;29 county.push(area2);30 } 31 32 }33 console.log(JSON.stringify(prov,null,4));34 console.log(JSON.stringify(city,null,4));35 console.log(JSON.stringify(county,null,4));
输出结果:
var provinceJson = [{"id":"110000","province":"北京市"},...
var provinceJson = [{"id":"110100","city":"市辖区","parent":"110000"},...var provinceJson = [{"id":"110101","county":"东城区","parent":"110100"},...
4、finally不一定必须执行,return在catch/finally中处理情况(建议亲自操刀试一下)
5、如何彻底修改Eclipse中项目的名称
一般来说,只需要一步即可:右键工程:Refactor->Rename,或选中工程按F2,修改名称。
6、MD5工具实现编码
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Tool { public static String md5(String string) { byte[] hash; try { hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Huh, MD5 should be supported?", e); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Huh, UTF-8 should be supported?", e); } StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) hex.append("0"); hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); } }
7、ArrayList集合中删除某个元素用Iterator进行操作
Iterator iterator = answerList.iterator(); while(iterator.hasNext()){ answerList.remove(iterator.next());}