Clearing form fields to add new form field data
valatharv Oct 5, 2008 7:45 PMScenario:
a) User fills form say Pname, Color, etc. fields(these belongs to reagent entity fields) from a form say study.xhtml and clicks Save
, values are persisted properly.
b) After above event, user clicks a link(Add More Reagents
) and passes some id to a function in reagentHome. On clicking link it loads a particular reagent.xhtml page, with same fields as Pname, Color, etc.
Issue is, it holds the values as submitted at point (a), for Pname, Color, etc. in reagent.xhtml and I cannot create a new Reagent (it will be updated always) because that reagent instance is still there.
I think I need to check for creating new instance or clear instance somehow or override any function of entity home, etc... please suggest...
What is the best way so that form fields are cleared and we should be able to persist new entry.
Please suggest
study.xhtml
<h:commandLink target="_new" id="name" action="#{reagentHome.getQuantId}" immediate="true"> <h:outputText value="Add More Reagents"/> <f:param name="qid" value="#{quantExperimentHome.instance.hjid}"/> </h:commandLink>
reagent.xhtml
<s:decorate id="pnameDecoration" template="layout/edit.xhtml"> <ui:define name="label">PName</ui:define> <h:inputText id="name" required="true" immediate="true" value="#{reagentHome.instance.pname}"> </h:inputText> </s:decorate> <s:decorate id="colorDecoration" template="layout/edit.xhtml"> <ui:define name="label">Color</ui:define> <h:inputText id="name" required="true" value="#{reagentHome.instance.color}"/> </s:decorate>
reagentHome.java
@Name("reagentHome") public class ReagentHome extends EntityHome<Reagent> { @RequestParameter Long reagentId; @In(required=false) private Long hjid; @Factory("reagent") public Reagent initReagent() { return getInstance(); } @Override public Object getId() { if (reagentId==null) { return super.getId(); } else { return reagentId; } } @Override @Begin(join=true) public void create() { super.create(); } //JUST for Testing public String getQuantId(){ HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); String qid = req.getParameter("name"); return "reagent"; }