We have a large Webapp that is still using JSF 1.2 with Myfaces and Tomahawk. The migration of JSF itself seems to be no big problem but as tomahawk isn't developed anymore we have to get rid of all our savestate usages. I know that we should use the Viewscope or similar Scopes to remove the savestate but this causes the problem that the behaviour is different to our savestate usage. We only stored a few specific values inside the savestate not the whole bean.
So if we migrate by replacing the savestate with the scope we have to test every single site if it still works how it sould.
Would it be possible to develop an own savestate component that'll work with JSF2.3 ? If yes we just could replace t:savestate with ne new component and migrate the old views when rebuilding them.
Would it be possible to develop an own savestate component that'll work with JSF2.3 ?
Yes.
Here's a kickoff example based off the original source code of <t:saveState>
:
@FacesComponent(createTag=true)
public class SaveState extends UIParameter {
public SaveState() {
setRendererType(null);
}
@Override
public Object saveState(FacesContext context) {
Object[] values = new Object[2];
values[0] = super.saveState(context);
if (getValueExpression("value") != null) {
values[1] = getValue();
}
return values;
}
@Override
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
ValueExpression valueExpression = getValueExpression("value");
if (valueExpression != null) {
valueExpression.setValue(context.getELContext(), values[1]);
}
}
}
In order to use it, declare against the predefined XML name space of http://xmlns.jcp.org/jsf/component
:
<anyElement ... xmlns:my="http://xmlns.jcp.org/jsf/component">
...
<my:saveState value="#{bean.property}" />
...
</anyElement>
That's all. Thanks to the createTag=true
feature of the @FacesComponent
you don't need to explicitly register the custom component in any XML file.
An alternative would be to use OmniFaces <o:inputHidden>
, but that would potentially require an explicit converter as it's passed as a HTTP request parameter rather than via JSF state.