Using jsf I want to edit a employee profile, when user will click on any particular datatable row,then
I am able to get all that deatils of selected patient in an
arraylist. Now I want to set all the attritbutes in arraylist to
page1.xhtml backingbean , so When user will select a particular row,
he will navigate to page1.xhtml where he will get all these fields in
the form set already by arraylist attributes.
I am trying in this way.
> page1.xhtml
<h:outputLabel value="Name" />
<p:inputText id="name1" value="#{employeeBB.emp.name}" >
</p:inputText>
<h:outputLabel value="age" />
<p:inputText id="ag" value="#{employeeBB.emp.age}" >
</p:inputText>
<h:outputLabel value="code" />
<p:inputText id="code1" value="#{employeeBB.emp.code}" >
</p:inputText>
#ManagedBean(name = "employee")
#ViewScoped
public class emp {
private String name;
private String age;
private String code;
public String getName()
{ return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
#SessionScoped
#ManagedBean
public class EmployeeBB implements serializable{
private Employe emp;
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
}
#SessionScoped
#ManagedBean
public class AddEmployeeBB{
private ArrayList<Employee>empList;
private ArrayList<Employee>empList;
public ArrayList<Employee> getEmpList() {
if(empList==null){
empList=new ArrayList<Employee>();
}
return empList;
}
public void setEmpList(ArrayList<Employee> empList) {
this.empList = empList;
}
public void method() throws IOException{
String code='123';
EmployeeDAO obj=new EmployeeDAO(); // DAO class
empList=obj.getAllEmplInfo(code); // will get all needed information about employee of this code in this arrayist
for(int i=0;i<empList.size();i++){
String name=empList.get(i).getName();
String age=empList.get(i).getAge();
String code=empList.get(i).getCode();
Employee e=new Employee();
e.setName(name);
e.setAge(age);
e.setCode(code);
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e); // now according to my logic object e will set to emp object of Employee, and
// that means all these values name ,agem and code will be set to my page1.xhtml and I will be able to see it.
}
}
But I am unable to get pag1.xhtml with filled values.
Show me the way.
The reason for it not being shown is that you are setting values in a object which you are creating
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e);
JSF lifecycle doens't know about this object and everytime you are seeing blank.
In AddEmployeeBB add this
#ManagedProperty(value="employeeBB")
private EmployeeBB employeeBB = null; // create getter setter for this
then instead of this :
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e);
Use this:
this.employeeBB.setEmp(e);
Related
My requirement is, at beginning I want to show users data on page and when user make changes in form, I want to access changed data.
Below is my code in Action class,
public class DisplayData extends ActionSupport implements ModelDriven<List<User>>, Preparable {
private List<User> userList;
#Override
public void prepare() throws Exception {
userList = new ArrayList<User>();
userList.add(new User("Demo","N"));
userList.add(new User("Demo1","Y"));
userList.add(new User("Demo2","Y"));
userList.add(new User("Demo3","N"));
}
#Override
public List<User> getModel() {
return userList;
}
public String execute(){
for (User value: userList) {
System.out.println(value.getName() +":"+value.getFlag());
}
return "success";
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
User class,
public class User implements Serializable
{
private String name;
private String flag;
public User() {}
public User(String name,String flag) {
super();
this.name = name;
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
Code in Jsp page,
<s:form name="getData" action="getData" method="post">
<table>
<s:iterator value="model" status="rowStatus">
<tr>
<td>
<s:textfield name="model[%{#rowStatus.index}].name" value="%{model[#rowStatus.index].name}"/>
<s:select name="%{model[#rowStatus.index].flag}" value="%{model[#rowStatus.index].flag}"
list="#{'Y':'Yes','N':'No'}" />
</td>
</tr>
</s:iterator>
</table>
<s:submit name="ok" value="ok" />
</s:form>
When page get rendered, it shows appropriate value of textfield and dropdown.
If I changed the values in Textfield and droprdown and submit the form then I am getting modified value of textfield but for the dropdwon it shows old value. How can I access selected value of dropdown?
Got the answer... :)
It was syntax mistake. Instead of
<s:select name="%{model[#rowStatus.index].flag}" value="%{model[#rowStatus.index].flag}"
list="#{'Y':'Yes','N':'No'}" />
Use
<s:select name="model[#rowStatus.index].flag" value="%{model[#rowStatus.index].flag}"
list="#{'Y':'Yes','N':'No'}" />
I have used %{ } in name attribute..
I filled my rightList of the PickList with Objects from a web service, but when i select some elements, i want to get these elements in myManagedBean, because i'll affect them to an Object.
JSF :
<h:form>
<rich:panel>
<h:panelGrid columns="2" styleClass="criteresSaisie"
rowClasses="critereLigne" columnClasses="titreColonne,">
<h:outputLabel for="libelleCoplement" value=" "
size="20" />
<rich:pickList d="libelleCoplement" sourceCaption="Compléments"
targetCaption="Compléments sélectionnés"
value="#{listeCmpltDispoModel.listeCmpltSelect}" size="15"
addText=">" addAllText=">>" removeText="<"
removeAllText="<<" listWidth="270px" listHeight="110px"
orderable="true">
<f:converter converterId="cmpltsTitresConcerter" />
<f:selectItems value="#{listeCmpltDispoModel.listeCmpltDispo}"
var="liste" itemLabel="#{liste.libelleComplement}"
itemValue="#{liste.cdComplement}"/>
</rich:pickList>
</h:panelGrid>
<h:panelGroup>
<div align="right">
<h:panelGrid columns="8">
<h:commandButton value="Valider"
action="#{saisieCmpltsTitreCtrl.valider}" />
</h:panelGrid>
</div>
</h:panelGroup>
</rich:panel>
</h:form>
The bean :
#ManagedBean(name = "listeCmpltDispoModel")
#SessionScoped
public class ListeCmpltDispoModel implements Serializable {
private static final long serialVersionUID = 1L;
private Long cdComplement;
private String libelleComplement;
private int nbCompl;
private List<ComplementsDispoSortieDTO> listeCmpltDispo ;
private List<ComplementsDispoSortieDTO> listeCmpltSelect ;
public ListeCmpltDispoModel() {
}
public Long getCodeComplement() {
return cdComplement;
}
public void setCodeComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement1() {
return libelleComplement;
}
public void setLibelleComplement1(String libelleCoplement) {
this.libelleComplement = libelleCoplement;
}
public Long getCdComplement() {
return cdComplement;
}
public void setCdComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement() {
return libelleComplement;
}
public void setLibelleComplement(String libelleComplement) {
this.libelleComplement = libelleComplement;
}
public List<ComplementsDispoSortieDTO> getListeCmpltDispo() {
return listeCmpltDispo;
}
public void setListeCmpltDispo(List<ComplementsDispoSortieDTO> listeCmpltDispo) {
this.listeCmpltDispo = listeCmpltDispo;
}
public int getNbCompl() {
return nbCompl;
}
public void setNbCompl(int nbCompl) {
this.nbCompl = nbCompl;
}
public List<ComplementsDispoSortieDTO> getListeCmpltSelect() {
return listeCmpltSelect;
}
public void setListeCmpltSelect(List<ComplementsDispoSortieDTO> listeCmpltSelect) {
this.listeCmpltSelect = listeCmpltSelect;
}
}
Converter :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter {
#SuppressWarnings("null")
public Object getAsObject(FacesContext context, UIComponent component,
String value){
ComplementsDispoSortieDTO cmpltSelect = null;
cmpltSelect.setCdComplement(Long.parseLong(value));
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
return String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
}
Any help is greatly apprectiated!
Roughtly you need 3 things :
Custom converter for your object (Object to String, String to Object)
Getter/Setter with the List of your Objects choices
Getter/Setter with the List of your Objects selected
Everything is perfectly described here : RichFaces Showcase - pickList
EDIT :
Adding this should fix your problem :
<rich:pickList ...>
<f:converter converterId="cmpltsTitresConcerter" />
</rich:pickList>
also the converter property in <f:selectItems /> is not valid : f:selectItems
EDIT :
You should modify your converter like that to remove converting exceptions :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
ComplementsDispoSortieDTO cmpltSelect = null;
if(value != null)
{
cmpltSelect = new ComplementsDispoSortieDTO();
cmpltSelect.setCdComplement(Long.parseLong(value));
}
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
{
String result = null;
if(obj != null)
{
result = String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
return result;
}
}
Your selected objects are bound to the value attribute which must have a getter and setter.
I'm trying to implement on JSF data table a binding to a backed bean value with a nested data table with a binding to a value of the current data row, see this code:
<h:form prependId="false">
<h:dataTable binding="#{tableBean.mainDataTable}" var="row" >
<h:column>
<h:dataTable binding="#{row.nestedDataTable}" />
</h:column>
</h:dataTable>
</h:form>
And these are the backed beans and data:
#ManagedBean
#SessionScoped
public class TableBean implements Serializable{
private HtmlDataTable mainDataTable;
private List<TableBeanData> tableBeanDataLst;
public TableBean() {
tableBeanDataLst = new ArrayList<TableBeanData>();
DataModel<TableBeanData> mainDataModel =new ListDataModel<TableBeanData>(tableBeanDataLst);
mainDataTable = new HtmlDataTable();
mainDataTable.setValue(mainDataModel);
}
public HtmlDataTable getMainDataTable() {
return mainDataTable;
}
public void setMainDataTable(HtmlDataTable mainDataTable) {
this.mainDataTable = mainDataTable;
}
public List<TableBeanData> getTableBeanDataLst() {
return tableBeanDataLst;
}
public void setTableBeanDataLst(List<TableBeanData> tableBeanDataLst) {
this.tableBeanDataLst = tableBeanDataLst;
}
}
public class TableBeanData implements Serializable{
private HtmlDataTable nestedDataTable;
public TableBeanData (){
nestedDataTable = new HtmlDataTable();
}
public HtmlDataTable getNestedDataTable() {
return nestedDataTable;
}
public void setNestedDataTable(HtmlDataTable nestedDataTable) {
this.nestedDataTable = nestedDataTable;
}
}
But I get a property not found exception:
/index.xhtml #17,69 binding="#{row.nestedDataTable}": Target Unreachable, indentified 'row' resolved to null
I don't understand well, because I'm Initializing the datatables in the constructors.
How can I resolve this type of error to allow this configuration of table, nested table and data bindings?
i' m developing my first jsf2 richfaces application , and now i am faced with the following problem :
i have the following menù
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users " />
<rich:panelMenuItem label="Orders" name="Orders" />
</rich:panelMenuGroup>
</rich:panelMenu>
I want that when click on the panelMenuItem is created a new tab and within of this new tab must insert a table that contains all users of the my application
i saw a few example of this type
<rich:tabPanel switchType="client" id="tabPanel">
<c:forEach items="#{handlerTab.tabsName}" var="tabName">
<rich:tab name = ... >
</rich:tab>
</c:foreach>
but don't know as insert a table in my new tab
how can i do?
tanks for your reply, but i don't want declare all tabs in the view , i want add and remove tab dynamically , now i have managed to do this
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users" action="#{tabsBean.createTabs()}" render="tabs" />
<rich:panelMenuItem label="Orders" name="Orders" action="#{tabsBean.createTabs()}" render="tabs" />
</rich:panelMenuGroup>
<h:panelGrid id="tabs" binding="#{tabsBean.panelGrid}"/>
then i have a bean that manage the tabs
#ManagedBean
#SessionScoped
public class TabsBean {
private HtmlPanelGrid panelGrid;
private Integer numOfTabs;
#PostConstruct
public void init(){numOfTabs=1;}
public Integer getNumOfTabs() {
return numOfTabs;
}
public void setNumOfTabs(Integer numOfTabs) {
this.numOfTabs = numOfTabs;
}
public TabsBean() {
}
public HtmlPanelGrid getPanelGrid() {
return panelGrid;
}
public void setPanelGrid(HtmlPanelGrid panelGrid) {
this.panelGrid = panelGrid;
}
public void createTabs (){
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
UITabPanel tabPanel = (UITabPanel)application.createComponent(UITabPanel.COMPONENT_TYPE);
tabPanel.setSwitchType(SwitchType.ajax);
for (int i=0; i<numOfTabs; i++){
UITab tab = new UITab();
tab = (UITab)application.createComponent(UITab.COMPONENT_TYPE);
tab.setName("User Count "+i);
tabPanel.getChildren().add(tab);
}
numOfTabs++;
panelGrid.getChildren().clear();
panelGrid.getChildren().add(tabPanel);
}
}
now my problem is that i must add of components a this tabs (datatable that contains all user , form for insert a user and other)
how can i do?
If you want the menu to open tabs with specific content it's better if you pre-create the tab and hide it, showing it (rendering) on request. To create the Users do the following:
Create a User class (if you don't have one)
public class User {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Create a UserTab managed bean
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class UserTab implements Serializable {
private List<User> users;
private boolean rendered;
public UserTab() {
//Initialize list
users = new ArrayList();
//Initialize rendered attribute
rendered = false;
//Replace this for your User data retrieving method
createDummyUsers();
}
private void createDummyUsers() {
User user = new User();
user.setName("John Doe");
user.setCountry("USA");
users.add(user);
user = new User();
user.setName("Bill Doe");
user.setCountry("Canada");
users.add(user);
user = new User();
user.setName("Winston Doe");
user.setCountry("England");
users.add(user);
}
public void showTab() {
rendered = true;
}
public void hideTab() {
rendered = false;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public boolean isRendered() {
return rendered;
}
public void setRendered(boolean rendered) {
this.rendered = rendered;
}
}
Create a tab handler:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TabHandler implements Serializable {
private String activeTab;
public TabHandler() {
activeTab = "none";
}
public String getActiveTab() {
return activeTab;
}
public void setActiveTab(String activeTab) {
this.activeTab = activeTab;
}
}
Create the view
<h:body>
<h:form>
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users"
actionListener="#{userTab.showTab()}">
<f:ajax event="select"
execute="#this"
render="tabPanel"
listener="#{tabHandler.setActiveTab('usersTab')}"/>
</rich:panelMenuItem>
<rich:panelMenuItem label="Orders" name="Orders" />
</rich:panelMenuGroup>
</rich:panelMenu>
<rich:tabPanel id="tabPanel"
switchType="client"
activeItem="#{tabHandler.activeTab}">
<rich:tab id="mainTab">
</rich:tab>
<rich:tab id="usersTab"
rendered="#{userTab.rendered}">
<f:facet name="header">
<h:outputLabel value="Users"/>
<h:commandLink value=" X" actionListener="#{userTab.hideTab()}"/>
</f:facet>
<rich:dataTable value="#{userTab.users}"
var="user">
<rich:column>
#{user.name}
</rich:column>
<rich:column>
#{user.country}
</rich:column>
</rich:dataTable>
</rich:tab>
</rich:tabPanel>
</h:form>
</h:body>
Do the same steps and add the required tags for the Order class.
See:
RichFaces dynamic TabPanel
c:forEach is not an option if you want to add the tabs by click.
What you want is to add components dynamically:
How to dynamically add JSF components
Dynamically added input field in ui:repeat is not processed during form submit
How to Dynamically adding fields in JSF?
In Struts 1.x , I have prepopulated the forms text fields using form bean's with default values as follows,
<html:form action="/actions/signup1">
First name: <html:text property="firstName"/><BR>
And in form bean, I have default value as follows...
public class ContactFormBean {
private String firstName = "First name";
But in Struts 2.x , when I tried with struts-tags textfield as follows, its not prepopulating the default value from the bean,
<s:form action="signup1">
<s:textfield name="formBean.firstName" label = "First Name" />
I have the formBean declared in my Action class as follows with appropriate getter and setter methods...
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
#Override
public String execute() throws Exception {
....
}
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
Please let me know if this can be accomplished at Request level and not at session level.
Thanks in advance.
<--Edited-->
struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="signup">
<result>/SignUp.jsp</result>
</action>
<action name="signup1" class="coreservlets.action.SignupAction1" method="execute">
<result name="success">/SignUp-Confirmation.jsp</result>
<result name="error">/SignUp.jsp</result>
</action>
</package>
</struts>
SignUp.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign UP</title>
</head>
<body>
<H1 ALIGN="CENTER">Sign Up</H1>
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:textfield name="formBean.lastName" label = "Last Name" />
<s:textfield name="formBean.email" label = "Email Address" />
<s:textfield name="formBean.faxNumber" label = "Fax Number" />
<s:submit action="signup1" method="loginAfterSubmit" value="Click here to Submit"/>
</s:form>
</body>
</html>
ContactFormBean.java
public class ContactFormBean {
private String firstName = "First name";
private String lastName = "Last name";
private String email = "user#host";
private String faxNumber = "xxx-yyy-zzzz";
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return(lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return(email);
}
public void setEmail(String email) {
this.email = email;
}
public String getFaxNumber() {
return(faxNumber);
}
public void setFaxNumber(String faxNumber) {
this.faxNumber = faxNumber;
}
public boolean isMissing(String value) {
if ((value == null) || (value.trim().equals(""))) {
return(true);
} else {
for(int i=0; i<defaultValues.length; i++) {
if (value.equals(defaultValues[i])) {
return(true);
}
}
return(false);
}
}
}
SignupAction1.java
public class SignupAction1 extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private ContactFormBean formBean;
#Override
public String execute() throws Exception {
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() {
String firstName = formBean.getFirstName();
String lastName = formBean.getLastName();
String email = formBean.getEmail();
String faxNumber = formBean.getFaxNumber();
if (formBean.isMissing(firstName)) {
return ERROR;
} else if (formBean.isMissing(lastName)) {
return ERROR;
} else if ((formBean.isMissing(email)) ||
(email.indexOf("#") == -1)) {
return ERROR;
} else if (formBean.isMissing(faxNumber)) {
return ERROR;
} else {
return SUCCESS;
}
}
public ContactFormBean getFormBean(){
return this.formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
Change your signup action declaration to
<action name="signup" class="coreservlets.action.SignupAction1">
<result>/SignUp.jsp</result>
</action>
and signup1 to
<action name="signup1" class="coreservlets.action.SignupAction1" method="signup1">
create method signup1 in your SignupAction1 action class and move your logic from execute to it. In execute then create new instance of your ContactFormBean.
put your default value to the constructor:
public class ContactFormBean {
private String firstname;
public ContactFormBean (){
this.firstName = "First name";
}
}
You are returning a null formBean object with the getFormBean method,
then the constructor is never called and the attempt to acces the formBean attribute firstName is giving an error (that is not showed because it is wrapped by the Struts2 tag on the JSP.
You can
1) instantiate it on your execute method:
public String execute() throws Exception {
this.formBean = new ContactFormBean();
}
2) instantiate it lazily on your getter:
public ContactFormBean getFormBean(){
if (formBean==null)
this.formBean = new ContactFormBean();
return this.formBean;
}
EDIT (due to your comments):
I don't know how you've structured your web application;
But if you are on a JSP, an Action (and its execute() method, or another method if specified) was called BEFORE rendering the JSP.
So, regardless if you have ActionOne loading stuff and ActionTwo called after submit, or ActionOne loading stuff and another method of ActionOne called after submit, you can know if you are in a "pre-JSP" state or in a "post-submit" state...
That said, if you are exposing an Object, and you want its value or its attributes to be different from null, you have to instantiate it in one of the way described above.
Obviously, your object should contain getters and setters for its attributes, and they must have been bound to your JSP objects.
In your example, you could have:
ContactFormBean:
public class ContactFormBean {
private String firstName = "First name";
public String getFirstName(){
return this.firstName;
}
public String setFirstName(String firstName){
this.firstName = firstName;
}
}
Your Action:
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
#Override
public String execute() throws Exception {
/* this method is executed before showing the JSP */
/* first time initialization */
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() throws Exception {
/* this method is executed after the submit button was pressed */
/* i don't initialize nothing here,
ContactFormBean is coming from the page */
System.out.println("ContactFormBean's firstName value is: " +
this.formBean.getFirstName());
return "goSomewhereElse";
}
}
and in Your JSP:
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:submit action="signup1" method="loginAfterSubmit" value="Press here to submit" />
</s:form>
You can do this with two Action by splitting the execute() and the loginAfterSubmit() methods into two execute() methods of two different Actions;
Then you must have your formBean in BOTH the Actions, with at least the getter in the first, and the setter in the second.
You can also set the Name value in your bean in execute method, it will populate it in your JSP page. Example is :
public String execute() throws Exception {
this.formBean = new ContactFormBean();
//Set your Name value here
formBean.setName("John Doe");
return SUCCESS;
}
Once JSP is populated you will find this value in Name text box.
Hope this will help.
Tapan