As I’m new to JSF framework, wanted to know how to add the user entered data to the database.
More clearly, i’m using jsf with jsp, my jsp contains some input fields and a submit button.How do i capture all the user entered data and send it as input paramters for the button, as my back end takes all these as input parameters and updates the table with a new record.
Please look into my code and lety me know my mistake
Registration.jsp
<body>
<f:view>
<h:form>
<h:panelGrid columns="2" rules="all" width="100%" style="background:#03547C;color:#FDD017">
<h:column>
<h:outputText value="Stu No : "></h:outputText>
<h:inputText value="#{RegBean.stuNo}"/>
</h:column>
<h:column>
<h:outputText value="Stu Name : "></h:outputText>
<h:inputText value="#{RegBean.stuName}"/>
</h:column>
<h:column>
<h:outputText value="Standard : "></h:outputText>
<h:inputText value="#{RegBean.standard}" />
</h:column>
<h:column>
<h:outputText value="School : "></h:outputText>
<h:inputText value="#{RegBean.school}" />
</h:column>
</h:panelGrid>
<h:panelGrid columns="2" rules="all" width="100%" style="background:#03547C;color:#FDD017">
<h:column>
<h:form>
<h:commandButton id="submitBtn" value="Submit" action="#{RegBean.submitDetails}">
<f:param name="sNo" value="#{RegBean.stuNo}" />
<f:param name="sName" value="#{RegBean.stuName}" />
<f:param name="std" value="#{RegBean.standard}" />
<f:param name="schl" value="#{RegBean.school}" />
</h:commandButton>
</h:form>
</h:column>
</h:panelGrid>
</h:form>
</f:view>
</body>
Registration Bean
public class VendorRegBean {
private String stuNo;
private String stuName;
private String standard;
private String school;
// getters and setters
public void submitDetails() {
Map requestMap = context.getExternalContext().getRequestParameterMap();
String stNo = (String) requestMap.get("sNo");
String stName = (String) requestMap.get("sName");
String stndrd = (String) requestMap.get("std");
String scl = (String) requestMap.get("schl");
vReg.stuRegistration(stNo ,stName ,stndrd ,scl );
}
}
You don’t need to get the parameters from the request parameter map as long as you register your bean as managed bean and provide getter and setter methodes for your bean members.
Use annotations for your bean to declare it as managed bean. Getter and setter example are given for
stuNomember:In the view you have to reference the managed bean with first letter lower case, such as:
Your command button doesn’t need
<f:param>, simply use:Then all parameters are automatically available in your
submitDetailsmethod and you don’t need to get them from the parameter map.