Integrate with Other Framework
This chapter introduce how to integrate with other framework, such as WebWork, Struts.
NUI dose not provide default implementations with any framework before beta version.
NuiContext & Provider
NUI can be integrated with other framework by a interface NuiContextProvider.
NuiCcontextProvider should be register in NuiConfigServlet as following:
<web-app>
<servlet>
<servlet-name>NuiConfigServlet</servlet-name>
<servlet-class>nui.servlet.NuiConfigServlet</servlet-class>
<init-param>
<param-name>provider</param-name>
<param-value>some.mvc.framework.NuiContextProvider</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
NuiContextProvider do only one thing: create or retrieve a NuiContext instance. Current PageContext
be passed to provider as parameter "pagecontext", so implementor could get contetx
object(e.g. a ActionContext of WebWork) from pagecontext and warp it as a NuiContext.
Context Map(OGNL Context)
NuiContext should export Context Map to NUI by method getContextMap(). the context map maybe OgnlValueStack in WebWork.
NUI use it as the root of OGNL context.
WebWork is special because OgnlValueStack is not a Map, so there needs a warpper like this:
private OgnlValueStack vs ;
public Object get (Object key)
{
return vs.findValue (String.valueOf (key)) ;
}
public Object put (Object key, Object value)
{
vs.setValue (String.valueOf (key) , value) ;
return null ;
}
public Object remove (Object key)
{
// Just set value to null.
vs.setValue (String.valueOf (key) , null) ;
return null ;
}
NUI never call to containsKey, containsValue, size, keys, values methods of Map.
Server Side Error
Server side error report is an important function of a MVC framework. NUI can retrieve
error message from NuiContext and display it by p:error template.
NUI invoke getError() method of NuiContext to get error message. Here is WebWork implementation(pseudo code):
public String getError (String key)
{
ValidationAware action = (ValidationAware)ActionContext.getContext ()
.getActionInvocation ().getAction () ;
Collection errors = (Collection) action.getFieldErrors().get (key) ;
// Just return first error message.
return (errors == null || errors.isEmpty ()) ?
null : (String) errors.get (0) ;
}
NUI dose not use action error message, currently.
Message Bundles
Most framework use resource bundles to manage text message, decouple program and text content.
NUI include indepent message bundles support, and also can fetch message from framework.
Method getText() allows NUI fetch text message from other framework. Here is WebWork implementation(pseduo code):
public String getText(String key)
{
TextProvider action = (TextProvider)ActionContext.getContext ()
.getActionInvocation ().getAction () ;
return action.getText (key) ;
}
NUI use some rule to build message key and fetch message from framework,
see
NUI Message Bundles for details.
Validator Config
Most framework support server/client side validation. For example WebWork use XWork validation framework.
Validation is based on Action Class. And need many config file.
NUI can directlly import validation config and use it in NUI client side validation.
Most important thing is that validator name should be same. For example, WebWork provides validator int, stringlength and
regexpression(and more). NUI support them as well. Finally, you can create new validator by simplly add a validator template.
Method findPropertyValidators() used to transfer validator config from MVC framework to
NUI. It return a List, whose element is nui.p.Attr object contains validator config info. A List indicates a property
could define multiple validators. An Attr object maybe like this:
Attr
- name: "stringlength" // validator name
- params: // paramter list
- [0]: Parameter min
. name: "min"
. value: "10"
- [1]: parameter max
. name: "max"
. value: "30"
Notes that paramter value be evaluated as a OGNL expression.
NUI can't use error message defined in validator config(e.g. WebWork validation message).
Next