FormView is enable to update HTML generated by JSP to add, replace new HTML content, in order to manage state (CREATE, UPDATE, READ...) of form and fields characteristics (DATE, MAXLENGTH, REQUIRED). With FormView you can :
To use FormView in your WEB application, you must follow three steps :
Configure FormView consist to describes states, displayers, fields characteristcs and fields states into XML Form view file. A state is associated with a behaviour. A behaviour is the mode of your form. By default, FormView has three behaviours :
You can manage behaviour for fields with different level :
This section is required. Here, you define all states used and for each state you associate it with a behaviour (READ-WRITE, READ-ONLY or INVISIBLE), like this :
.... <!-- Describe CRU (CREATE, READ and UPDATE) states and INVISIBLE state --> <states> <state name="CREATE" behaviour="READ-WRITE" /> <state name="READ" behaviour="READ-ONLY" /> <state name="UPDATE" behaviour="READ-WRITE" /> <state name="INVISIBLE" behaviour="INVISIBLE" /> </states> ....
This section is NOT required. (Not implemented).
By default FormView implements displayers which can be customizable with XML Displayers-config file (see section How customize FormView DisplayerConfig ?)
This section is NOT required. You use this section when you want manage behaviour for particular fields of particular form. You can :
.... <form name="/project" > <field property="developersName" behaviour="READ-ONLY" /> ... Other Field configuration </form> ....
Field with name developersName will have READ-ONLY behaviour whatever form state.
.... <form name="/project" > .... <state name="READ" > <field property="buttonSave" behaviour="INVISIBLE" /> ... Other Field configuration for the state READ </state> .... </form> ....
Field with name buttonSave will have INVISIBLE behaviour when form state will be READ.
If you use FormView with Struts, name of form is the same name of path your action (like validation.xml).
Here an example of XML form view file :
<?xml version="1.0" encoding="UTF-8"?> <form-view> <!-- Describe CRU (CREATE, READ and UPDATE) states and INVISIBLE state --> <states> <state name="CREATE" behaviour="READ-WRITE" /> <state name="READ" behaviour="READ-ONLY" /> <state name="UPDATE" behaviour="READ-WRITE" /> <state name="INVISIBLE" behaviour="INVISIBLE" /> </states> <!-- Form with name of path (see mapping.getPath() Struts) --> <form name="/project" > <!-- developersName field is READ-ONLY whatever the form state --> <field property="developersName" behaviour="READ-ONLY" /> <state name="READ" > <!-- buttonSave field is INVISIBLE when form state is READ --> <field property="buttonSave" behaviour="INVISIBLE" /> ... Other Field configuration for the state READ </state> </form> </form-view>
Initialize FormView consist to :
If you use FormView with Struts, use FormView Plugin into struts-config, like this :
<!-- FormView Plugin --> <plug-in className="net.sourceforge.formview.struts.plugin.FormViewPlugIn"> <set-property property="formViewConfig" value="/WEB-INF/form-view.xml"/> <set-property property="validatorConfig" value="/WEB-INF/validation.xml"/> <set-property property="displayerConfig" value="/WEB-INF/displayers-config.xml"/> </plug-in>
It's possible to split your form view config to several files, Here an example of config, wich use two XML config file form-view1.xml and form-view2.xml :
<!-- FormView Plugin --> <plug-in className="net.sourceforge.formview.struts.plugin.FormViewPlugIn"> <set-property property="formViewConfig" value="/WEB-INF/form-view1.xml,/WEB-INF/form-view2.xml"/> .... </plug-in>
Using FormView consist to :
<%@ taglib uri="/WEB-INF/form-view.tld" prefix="formview" %> ... <formview:page> <html:form action="/project" > <html:text property="projectName" /> <html:text property="projectDate" /> <html:select property="managerId" > <html:options collection="managerList" property="id" labelProperty="name"/> </html:select> </html:form> </formview:page>
Pay attention to not forget to insert tld.
public final ActionForward loadRead(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Set state to CREATE WEBFormViewUtil.saveState(request, getServlet().getServletContext(), FormViewConstants.STATE_READ); // Save characteristics fields of form WEBFormViewUtil.saveFormView(request, getServlet().getServletContext(), mapping.getPath()); return mapping.getInputForward(); }
It is possible to manage field in your action with method saveFieldView of WEBFormViewUtil. For instance, You have list of fields (with the same name) and with behaviour=READ-ONLY. If you want that one field with particular index, was READ-WRITE, you can do it, like this :
Integer index = new Integer(1); // Save state and formview WEBFormViewUtil.saveState(request, getServlet().getServletContext(), FormViewConstants.STATE_UPDATE); WEBFormViewUtil.saveFormView(request, getServlet().getServletContext(), mapping.getPath()); // Set field name=developersName with index=index to READ-WRITE // And Save it into request FieldView fieldView = new FieldView("developersName"); fieldView.setIndex(index); fieldView.setBehaviour(FormViewConstants.BEHAVIOUR_READWRITE); WEBFormViewUtil.saveFieldView(request, getServlet().getServletContext(), mapping.getPath(), fieldView);