1

I just started learning jsf. I have used JSF2.0 and java version 1.8 and tomcat server to build a simple form page.

I am trying to get values from form and save it in a list. Following is the code i wrote.

createDemand.java(managed bean)

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "dem")
@RequestScoped
public class DemandBean implements Serializable
    {
        private static final long serialVersionUID = 1L;
        private String role;
        private String portfolio;
        //getters and setter for role and portfolio

        public String submitted(){
         System.out.println("Bean executed");
         setRole(role);
         setPortfolio(portfolio);
         List<String> form = new ArrayList<String>();
         form.add(role);
         form.add(portfolio);
         System.out.println(form.toString());
         return "new";
    }
    //tostring 
}

When i submit the page no action is taking place. demand.xhtml

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Resource Manager</title>
    </h:head>
    <h:body>
        <h3>Create Demand Request</h3>
        <h:panelGrid columns="2">
            <h:form>
                <h:selectOneMenu value="#{dem.role}">
                    <f:selectItem itemValue="Project Manager"
                        itemLabel="Project Manager" />
                    <f:selectItem itemValue="Technical Lead" 
                        itemLabel="TechnicalLead" />
                    <f:selectItem itemValue="Business Analyst"
                        itemLabel="Business Analyst" />
                </h:selectOneMenu>
            </h:form>
            <h:form>
                <h:selectOneMenu value="#{dem.portfolio}">
                    <f:selectItem itemValue="RND IT DDL" itemLabel="RND IT DDL" />
                    <f:selectItem itemValue="APX" itemLabel="APX" />
                    <f:selectItem itemValue="CMR" itemLabel="CMR" />
                </h:selectOneMenu>
            </h:form>
        </h:panelGrid>
        <h:commandButton id="submitButton" value="submit" type="submit"
            action="#{dem.submitted}" />
    </h:body>
    </html>

This html to view some message to know form is submitted new.xhtml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">
    <head>
    <h:outputStylesheet library="css" name="table-style.css" />
    <title>Resource manager</title>
    </head>
    <body>
        <h:outputText value="Form submitted" />
        <h:outputText value="#{dem.role}"/>
    </body>
    </html>
Nirmala.V
  • 11
  • 2
  • Please use good tutorials, you have many mistakes. Start on http://www.stackoverflow.com/tags/jsf/info and https://jsf.zeef.com – Kukeltje Dec 15 '17 at 10:29

0 Answers0