一般有以下四种把字符串转换成boolean的方法,各自有各自的实现思路和特点:
1.最基本的,先看JDK的做法:
java,lang.Boolean的toBoolean(String name)是个私有方法。
private static boolean toBoolean(String name) { return ((name != null) && name.equalsIgnoreCase("true")); }
可以看到,除了不区分大小写的字符串true外,其余的字符串均表示false;
2.jsp中的<jsp:setProperty>标签.
通过试验,发现在jsp标签中,on也会被转换成true的。
完整的javaBean:
-------------------------------------
package com.lizongbo;
/** * <p>Title: lizongbo demo</p> * <p>Description: lizongbo的测试程序</p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: lizongbo</p> * @author lizongbo <a href=" [email protected]">lizongbo</a> * @version 1.0 */
public class TestbooleanBean { private boolean sample = false;
//Access sample property public boolean getSample() { return sample; }
//Access sample property public void setSample(boolean newValue) { sample = newValue; } }
-------------------------------------
jsp文件
-------------------------------------
<%@ page contentType="text/html; charset=GBK" %> <html> <head> <title> testboolean </title> </head> <jsp:useBean id="testbooleanBeanId" scope="session" class="com.lizongbo.TestbooleanBean" /> <jsp:setProperty name="testbooleanBeanId" property="*" /> <body bgcolor="#ffffff"> <form method="post" action="testboolean.jsp"> <br>Enter new value : <input name="sample"><br> <br><br> <input type="submit" name="Submit" value="Submit"> <input type="reset" value="Reset"> <br> Value of Bean property is :<jsp:getProperty name="testbooleanBeanId" property="sample" /> </form> </body> </html>
-------------------------------------
通过察看tomcat源代码可以找到其实现boolean转换的代码
org.apache.jasper.runtime.JspRuntimeLibrary.java
public static Object convert(String propertyName, String s, Class t, Class propertyEditorClass) throws JasperException { try { if (s == null) { if (t.equals(Boolean.class) || t.equals(Boolean.TYPE)) s = "false"; else return null; } if (propertyEditorClass != null) { return getValueFromBeanInfoPropertyEditor( t, propertyName, s, propertyEditorClass); } else if ( t.equals(Boolean.class) || t.equals(Boolean.TYPE) ) { if (s.equalsIgnoreCase("on") || s.equalsIgnoreCase("true")) s = "true"; else s = "false"; return new Boolean(s); }
由此可以看出,jsp标签中是支持把不区分大小写的“on”和“true”都转换成true的。
3.org.apache.commons.lang.BooleanUtils.toBoolean(String str);
BooleanUtils提供了多种转换,这里只看它对字符串的转换。
看它的代码可以知道支持把不分大小写的“yes”,“on”,“true”都转换成true;
public static boolean toBoolean(String str) { if ("true".equalsIgnoreCase(str)) { return true; } else if ("on".equalsIgnoreCase(str)) { return true; } else if ("yes".equalsIgnoreCase(str)) { return true; } // no match return false; }
ps: apache commons configuration 中的Configuration getBoolean(String key)就是调用的这个转换方法。
4.com.opensymphony.util.TextUtils.parseBoolean(String in);
这个方法支持转换成true的范围最多最广也最灵活,容错性高,尽可能的满足了现实生活习惯,但是,不支持把“on”转换成true
/** * Convert a String to an boolean. * Accepts: 1/0, yes/no, true/false - case insensitive. If the value does * not map to "true,", <code>false</code> is returned. * * @param in String to be parsed. * @return boolean representation of String. */ public final static boolean parseBoolean(String in) { in = noNull(in);
if (in.length() == 0) { return false; }
switch (in.charAt(0)) { case '1': case 'y': case 'Y': case 't': case 'T': return true; }
return false; }
通过以上分析,在允许使用第三方java lib的时候,推荐使用后两种做法。

|