📄 typeconversion.html
字号:
<html><head><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 6. Type Conversion</title><link href="../docbook.css" rel="stylesheet" type="text/css"><meta content="DocBook XSL Stylesheets V1.65.0" name="generator"><link rel="home" href="index.html" title="OGNL Developer Guide"><link rel="up" href="index.html" title="OGNL Developer Guide"><link rel="previous" href="classReferences.html" title="Chapter 5. Class References"><link rel="next" href="memberAccessManager.html" title="Chapter 7. Member Access"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table summary="Navigation header" width="100%"><tr><th align="center" colspan="3">Chapter 6. Type Conversion</th></tr><tr><td align="left" width="20%"><a accesskey="p" href="classReferences.html"><img src="../images/navigation/prev.gif" alt="Prev"></a> </td><th align="center" width="60%"> </th><td align="right" width="20%"> <a accesskey="n" href="memberAccessManager.html"><img src="../images/navigation/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="typeConversion"></a>Chapter 6. Type Conversion</h2></div></div><div></div></div><p>When performing set operations on properties or calling methods it is often the case that the values you want to set have a different type from the expected type of the class. <span class="acronym">OGNL</span> supports a context variable (set by
<tt class="function">OgnlRuntime.setTypeConverter(Map context, TypeConverter typeConverter)</tt>) that will allow types to be converted from one to another. The default type converter that is uses is the <tt class="classname">ognl.DefaultTypeConverter</tt>,
which will convert among numeric types <tt class="classname">(Integer</tt>, <tt class="classname">Long</tt>, <tt class="classname">Short</tt>, <tt class="classname">Double</tt>, <tt class="classname">Float</tt>, <tt class="classname">BigInteger</tt>,
<tt class="classname">BigDecimal</tt>, and their primitive equivalents), string types (<tt class="classname">String</tt>, <tt class="classname">Character</tt>) and <tt class="classname">Boolean</tt>. Should you need specialized type conversion (one popular
example is in Servlets where you have a <tt class="classname">String[]</tt> from an <tt class="function">HttpServletRequest.getParameters()</tt> and you want to set values with it in other objects; a custom type converter can be written (most likely
subclassing <tt class="classname">ognl.DefaultTypeConverter</tt>) to convert <tt class="classname">String[]</tt> to whatever is necessary.</p><pre class="programlisting">public interface TypeConverter
{
public Object convertValue(Map context,
Object target,
Member member,
String propertyName,
Object value,
Class toType);
}</pre><p>Note that <tt class="classname">ognl.DefaultTypeConverter</tt> is much easier to subclass; it implements <tt class="classname">TypeConverter</tt> and calls it's own <tt class="function">convertValue(Map context, Object value, Class toType)</tt>
method and already provides the numeric conversions. For example, the above converter (i.e. converting <tt class="classname">String[]</tt> to <tt class="classname">int[]</tt> for a list of identifier parameters in a request) implemented as a subclass
of <tt class="classname">ognl.DefaultTypeConverter</tt>: <pre class="programlisting">HttpServletRequest request;
Map context = Ognl.createDefaultContext(this);
/* Create an anonymous inner class to handle special conversion */
Ognl.setTypeConverter(context, new ognl.DefaultTypeConverter() {
public Object convertValue(Map context, Object value, Class toType)
{
Object result = null;
if ((toType == int[].class) && (value instanceof String[].class)) {
String sa = (String[])value;
int[] ia = new int[sa.length];
for (int i = 0; i < sa.length; i++) {
Integer cv;
cv = (Integer)super.convertValue(context,
sa[i],
Integer.class);
ia[i] = cv.intValue();
}
result = ia;
} else {
result = super.convertValue(context, value, toType);
}
return result;
}
});
/* Setting values within this OGNL context will use the above-defined TypeConverter */
Ognl.setValue("identifiers",
context,
this,
request.getParameterValues("identifier"));</pre></p></div><div class="navfooter"><hr><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="classReferences.html"><img src="../images/navigation/prev.gif" alt="Prev"></a> </td><td align="center" width="20%"><a accesskey="u" href="index.html"><img src="../images/navigation/up.gif" alt="Up"></a></td><td align="right" width="40%"> <a accesskey="n" href="memberAccessManager.html"><img src="../images/navigation/next.gif" alt="Next"></a></td></tr><tr><td valign="top" align="left" width="40%">Chapter 5. Class References </td><td align="center" width="20%"><a accesskey="h" href="index.html"><img src="../images/navigation/home.gif" alt="Home"></a></td><td valign="top" align="right" width="40%"> Chapter 7. Member Access</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -