Please let me know your suggestion. I want to build fileUpload with specific design and having issue with getting hold of the uploadedFile.
My design:
DataModel class:
#Named
#SessionScoped
public class NJDataModel implements Serializable {
private static final long serialVersionUID = 1L;
private String sectionName;
private UploadedFile file;
public NJDataModel(String sectionName) {
this.sectionName = sectionName;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
}
Data class:
#ManagedBean(name = "NJData")
#SessionScoped
public class Data implements Serializable {
private static final long serialVersionUID = 1L;
private List<NJDataModel> dataModel;
#PostConstruct
public void init() {
/** defaults */
dataModel = new ArrayList<NJDataModel>();
dataModel.add(new NJDataModel("Title page"));
}
public void upload() {
System.out.println("upload method triggered");
String msg = null;
for (NJDataModel i : dataModel) {
UploadedFile file = i.getFile();
// ERROR: file is always null? so could not get hold of file
if (file != null) {
System.out.println("Uploaded file:" + file.getFileName());
msg += "file:" + file.getFileName() + "size:" + file.getSize() + ", ";
}
}
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
}
}
XHTML:
<p:dataTable var="item" value="#{NJData.dataModel}"
draggableColumns="true">
<p:column>
<p:panel>
<f:facet name="header">
<h:outputText value="#{item.sectionName}" />
</f:facet>
<p:fileUpload value="#{item.file}" mode="simple" />
</p:panel>
</p:column>
</p:dataTable>
<p:commandButton value="Submit" ajax="false"
actionListener="#{NJData.upload}" />
And finally, i need to get hold of file in method: NJData.upload() - where i always get null?
My console output:
file object:null & section name:first page
file object:null & section name:second page
Related
I would like to create a JSF application
Where a user must provide their age. This will default to 18 and be editable (If the user wants to provide a different value).
How to achieve this functionality?
The input value provided by the user or the default value ie which is 18 would be saved in the Database
<h:form>
<p:panel id="panel" header="New User">
<h:panelGrid columns="3" cellpadding="5">
<p:outputLabel for="age" value="Age:" />
<p:inputText id="age" value="#{managedBean.entityBean.age}" required="true" label="Age">
</p:inputText>
</h:panelGrid>
<p:commandButton value="Save" update="panel" action="#{managedBean.save}" />
</p:panel>
</h:form>
Entity.java
public class Entity implements Serializable {
#Column(name = "AGE")
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
managedBean.java
#ManagedBean(name = "managedBeanJava")
#ViewScoped
public class managedBeanJava implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(managedBeanJava.class);
private Entity entityBean;
public Entity getEntityBean() {
return entityBean;
}
public void setEntityBean(Entity entityBean) {
this.entityBean = entityBean;
}
private Integer age = 50;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
#PostConstruct
public void init() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, String> params = externalContext.getRequestParameterMap();
}
public void save(AjaxBehaviorEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
entityBean.setAge(this.entityBean.getAge());
}
}
You could define default value of <h:inputText> in your managed bean directly.
private Integer age = 18;
I want to use the same View for different states of a bean. Therefore I need to Inject/pass/whatever get the current bean I want to modify, but I always get null.
I already tried it with , with #Inject, and the other solutions given in How can I pass selected row to commandLink inside dataTable or ui:repeat?
The View:
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import de.auticon.beans.Mitarbeiter;
import de.auticon.beans.Skill;
#Named(value = "skillView")
#ViewScoped
public class SkillView implements Serializable {
private static final long serialVersionUID = -3256509521249071048L;
private Mitarbeiter employee;
private Skill skill = new Skill();
private List<String> expertises = new ArrayList<String>();
#PostConstruct
public void init() {
List<Expertise_String> expertiseObjects = Queries.findAllExpertises();
for (Expertise_String singleExpertise : expertiseObjects) {
expertises.add(singleExpertise.getExpertise() + " - " + singleExpertise.getFullNameString());
}
}
//Here employee is null, but I need the new/existing employee
public void addSkill() {
employee.getSkills().add(skill);
PrimeFaces.current().dialog().closeDynamic(null);
}
public void closeDialog() {
PrimeFaces.current().dialog().closeDynamic(null);
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
public Mitarbeiter getEmployee() {
return employee;
}
public void setEmployee(Mitarbeiter employee) {
this.employee = employee;
}
}
XHTML from which it is called for existing employees:
<h:form id="form">
<p:panelGrid columns="2" styleClass="ui-noborder">
<p:menu toggleable="true">
<p:submenu label="Verwaltung">
<p:menuitem>
<p:commandButton value="Mitarbeiter anlegen" action="#{adminView.addNewEmployee}" icon="pi pi-users"
process="#this :form:skillsDialog">
<p:ajax event="dialogReturn" listener="#{mitarbeiterView.update}" update=":form, :form:table"/>
</p:commandButton>
</p:menuitem>
</p:submenu>
</p:menu>
<p:dataTable id="table" var="row" value="#{mitarbeiterView.mitarbeiter}" liveResize="true" resizableColumns="true">
<p:column headerText="Skillset">
<p:commandLink update=":form:skillsDialog, :form:skillsDetails" oncomplete="PF('skillsDialog').show()" title="Detail"
styleClass="ui-icon pi pi-search">
<f:setPropertyActionListener value="#{row}" target="#{mitarbeiterView.selectedEmployee}" />
</p:commandLink>
<p:commandLink action="#{adminView.openNewSkillDialog(row)}" title="add" styleClass="ui-icon pi pi-plus">
<!-- <f:setPropertyActionListener value="#{row}" target="#{skillView.employee}" /> -->
</p:commandLink>
</p:column>
</p:dataTable>
</p:panelGrid>
<p:dialog id="skillsDialog" header="Skillsheet von #{mitarbeiterView.selectedEmployee.vorname} #{mitarbeiterView.selectedEmployee.name}"
showEffect="fade" widgetVar="skillsDialog" modal="true" resizable="true">
<p:outputPanel id="skillsDetails">
<p:dataTable var="skillRow" value="#{mitarbeiterView.selectedEmployee.skills}">
<p:column headerText="Skill">
<h:outputText value="#{skillRow.name}" />
</p:column>
<p:column headerText="Ausprägung">
<h:outputText value="#{skillRow.expertise} - #{skillRow.expertiseString.fullName}" />
</p:column>
<p:column headerText="Beschreibung">
<h:outputText value="#{skillRow.description}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</p:dialog>
</h:form>
Second XHTML, for new employees:
<h:form id="newForm">
<h3>Skillsheet</h3>
<p:panelGrid id="skillPanel" columns="2" cellpadding="5" styleClass="ui-noborder">
<p:column style="width:50px">
<p:commandButton id="openAddSkill" process="#this" action="#{adminView.openNewSkillDialog}"
title="Neuer Skill" icon="pi pi-plus">
<!-- <f:setPropertyActionListener target="#{skillView.employee}" value="#{newEmployeeView.newEmployee}"/> -->
<p:ajax event="dialogReturn" update=":newForm :newForm:skillPanel"/>
</p:commandButton>
</p:column>
</p:panelGrid>
<p:commandButton value="Hinzufügen" id="addSkill" icon="pi pi-plus" action="#{skillView.addSkill()}"/>
</h:form>
AdminView.java:
#Named(value = "adminView")
#ViewScoped
public class AdminView implements Serializable {
private static final long serialVersionUID = 5252224062484767900L;
#Inject
private SkillView skillView;
public void addNewEmployee() {
Map<String, Object> options = new HashMap<String, Object>();
options.put("id", "newEmployeeDialogID");
options.put("widgetVar", "newEmployeeDialogVar");
options.put("resizable", false);
options.put("modal", false);
PrimeFaces.current().dialog().openDynamic("/dialogs/newEmployee", options, null);
}
public void openNewSkillDialog(Mitarbeiter employee) {
Map<String, Object> options = new HashMap<String, Object>();
options.put("resizable", true);
options.put("modal", false);
options.put("contentWidth", 420);
PrimeFaces.current().dialog().openDynamic("/dialogs/skill", options, null);
skillView.setEmployee(employee);
}
}
Skill.java:
public class Skill {
#NotNull(message = "Skill fehlt")
private String name;
#NotNull(message = "Bitte Ausprägung angeben")
private short expertise;
private String description;
private String expertiseFullname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getExpertise() {
return expertise;
}
public void setExpertise(short expertise) {
this.expertise = expertise;
}
public String getExpertiseFullname() {
return expertiseFullname;
}
public void setExpertiseFullname(String expertiseFullname) {
this.expertiseFullname = expertiseFullname;
}
public void setExpertise(String fullName) {
try {
this.expertise = Short.valueOf(fullName.substring(0, fullName.indexOf(" - ")));
} catch (Exception e) {
this.expertise = 0;
}
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Mitarbeiter.java:
public class Mitarbeiter {
private int id;
#NotNull(message = "Nachname fehlt")
private String name;
#NotNull(message = "Vorname fehlt")
private String vorname;
private Date entryDate;
private List<Skill> skills = new ArrayList<Skill>();
private int expertise;
public Mitarbeiter() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public List<Skill> getSkills() {
return skills;
}
public void setSkills(List<Skill> skills) {
this.skills = skills;
}
public int getExpertise() {
return expertise;
}
public void setExpertise(int expertise) {
this.expertise = expertise;
}
MitarbeiterView.java:
#Named(value = "mitarbeiterView")
#ViewScoped
public class MitarbeiterView implements Serializable {
private static final long serialVersionUID = 7924178697538784022L;
#Inject
MitarbeiterService mitarbeiterService;
private List<Mitarbeiter> mitarbeiter;
private Mitarbeiter selectedEmployee;
#PostConstruct
public void init() {
SessionConfig.initSession();
new Commons();
updateMitarbeiter();
}
private void updateMitarbeiter() {
mitarbeiter = new ArrayList<Mitarbeiter>();
List<EmployeeDTO> dtos = Queries.findAllEmployees();
for (EmployeeDTO employeeDTO : dtos) {
mitarbeiter.add(mitarbeiterService.convertToMitarbeiter(employeeDTO));
}
}
public void update() {
updateMitarbeiter();
PrimeFaces.current().ajax().update("form:table");
}
public List<Mitarbeiter> getMitarbeiter() {
return mitarbeiter;
}
public void setMitarbeiter(List<Mitarbeiter> mitarbeiter) {
this.mitarbeiter = mitarbeiter;
}
public void setSelectedEmployee(Mitarbeiter selectedEmployee) {
this.selectedEmployee = selectedEmployee;
}
public Mitarbeiter getSelectedEmployee() {
return selectedEmployee;
}
NewEmployeeView.java:
#Named(value = "newEmployeeView")
#ViewScoped
public class NewEmployeeView implements Serializable {
private static final long serialVersionUID = 789108010781037452L;
#ManagedProperty(value = "#{mitarbeiter}")
private Mitarbeiter newEmployee = new Mitarbeiter();
#PostConstruct
public void init() {
}
public Mitarbeiter getNewEmployee() {
return newEmployee;
}
public void setNewEmployee(Mitarbeiter mitarbeiter) {
this.newEmployee = mitarbeiter;
}
Calling adSkill() from the first XHTML should have the selected employee from the Datatable. In the second case, it should have a freshly created, "empty" employee, which I already provide.
I think that major cause of your problems is that skillView bean is #RequestScoped meaning that it is being constructed and destroyed on each request and thus your employee is being reinitialized/reset to null every time. Check out this accepted answer for details.
Hints that might lead you to solution:
put debug lines in at least init() and setEmployee(..) methods of skillView and observe behaviour,
change scope of skillView to #ViewScoped which will preserve employee object across multiple Ajax requests
I'm trying to delete one cell with a <p:commandButton> and after i click i want to update my table. But all what happens is, that i got this Exception.
javax.persistence.EntityNotFoundException: Unable to find de.test.Datei with id 5
But in my Database i can see the facts in each table. Also when i click the button, the dates will be delete correctly. But my table doesn't update, so the cell isn't disappeared. And when i click twice, i got the above error message.
Datei.class
#Entity
public class Datei implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "DATA_ID_GENERATOR", sequenceName = "SEQ_DATA", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DATA_ID_GENERATOR")
private long id;
private String name;
private long groesse;
#Temporal(TemporalType.TIMESTAMP)
private Date datum;
#Basic(fetch = FetchType.LAZY)
#Lob
private byte[] datei;
#ManyToOne
private Benutzer benutzer;
public Benutzer getBenutzer() {
return benutzer;
}
public void setBenutzer(Benutzer benutzer) {
this.benutzer = benutzer;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getGroesse() {
return groesse;
}
public void setGroesse(long groesse) {
this.groesse = groesse;
}
public byte[] getDatei() {
return datei;
}
public void setDatei(byte[] datei) {
this.datei = datei;
}
public Datei() {
// TODO Auto-generated constructor stub
}
public Date getDatum() {
return datum;
}
public void setDatum(Date datum) {
this.datum = datum;
}
#Override
public String toString() {
return "Datei [id=" + id + ", name=" + name + ", groesse=" + groesse
+ ", datum=" + datum + ", datei=" + Arrays.toString(datei)
+ "]";
}
}
Benutzer.class
#Entity
public class Benutzer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "CUSTOMER_ID_GENERATOR", sequenceName = "SEQ_CUSTOMER", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_ID_GENERATOR")
private long id;
private String name;
private String email;
private long kundennummer;
#OneToMany(mappedBy = "benutzer", orphanRemoval = true)
private List<Datei> datei;
#Override
public String toString() {
return "Benutzer [id=" + id + ", name=" + name + ", email=" + email
+ ", kundennummer=" + kundennummer + ", datei=" + datei + "]";
}
public List<Datei> getDatei() {
return datei;
}
public void setDatei(List<Datei> datei) {
this.datei = datei;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getKundennummer() {
return kundennummer;
}
public void setKundennummer(long kundennummer) {
this.kundennummer = kundennummer;
}
public Benutzer() {
// TODO Auto-generated constructor stub
}
}
My DateiDAO.class
[...]
public void loeschen(Datei datei, Benutzer benutzer) {
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
benutzer = em.merge(benutzer);
datei = em.merge(datei);
tr.commit();
benutzer.getDatei().remove(datei);
tr.begin();
em.merge(benutzer);
em.remove(em.merge(datei));
tr.commit();
}
So the only problem is, that my table dosen't update.
If i use richfaces, it works fine.
main.xhtml
<h:form id="myForm">
<p:dataTable id="dateien" var="data"
value="#{mainController.ben.datei}" rowKey="#{data.id}"
selectionMode="single" rows="5" paginator="true" rowsPerPageTemplate="5,10,15" paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">
<f:facet name="header">
<h:outputText value="Uploaded"/>
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{data.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{data.name}" />
</p:column>
<p:column headerText="Datum">
<h:outputText value="#{data.datum}" />
</p:column>
<p:column headerText="Delete">
<p:commandButton update="myForm"
actionListener="#{mainController.loeschen(data)}"
icon="ui-icon-trash" />
</p:column>
</p:dataTable>
</h:form>
Miss it in your process:
Delete this item from your mainController.ben.datei list after you delete item from database, and update your dataTable like Ouerghi Yassine told.
or
Get load again your data list from database on loeschen method after you delete item, its more security if you do this system and wish no have a concurrence problem.
So, you provide to your dataTable one list binding with your bean, in your case the datei list, so even if you delete item from database, this item cant be this list too.
I'm sorry for my english.
Add update="dateien" to your commandButton
<p:commandButton update="myForm"
actionListener="#{mainController.loeschen(data)}"
icon="ui-icon-trash"
update="dateien"/>
I want to download file in my jsf page, and below is my jsf page
<h:form id="MyListForm">
<p:commandButton value="Attachments" icon="ui-icon-transferthick-e-w" actionListener="#{myTeam.downloadImageDialogue}"
style="font-size: 10px;font-weight: bolder;vertical-align: text-bottom;width: 80px;" >
</p:commandButton>
</h:form>
and my bean code is:
public void downloadImageDialogue() {
RequestContext.getCurrentInstance().openDialog("AttachmentDialog");
}
and my dilogue xhtml page is:
<h:body>
<h:form>
<p:dataTable var="Attachment" value="#{attachmentDialogView.attachmentList}" rows="10">
<p:column headerText="File Name">
<h:outputText value="#{Attachment.fileName}" />
</p:column>
<p:column headerText="Download Attachment">
<p:commandLink ajax="false" id="dwldLink" action="#{attachmentDialogView.downloadImage}">
<f:setPropertyActionListener value="#{Attachment.id.itemId}" target="#{attachmentDialogView.imageItemId}" />
<f:setPropertyActionListener value="#{Attachment.fileName}" target="#{attachmentDialogView.fileName}" />
<f:setPropertyActionListener value="#{Attachment.id.callId}" target="#{attachmentDialogView.callID}" />
<p:graphicImage value="images/dnld1.png" width="25" height="25"/>
<p:fileDownload value="#{attachmentDialogView.file}"/>
</p:commandLink>
</p:column>
</p:dataTable>
</h:form>
</h:body>
and bean code is:
#ManagedBean(name = "attachmentDialogView")
#ViewScoped
public class AttachmentDialogView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Attachment> attachmentList;
private String imageItemId;
private String fileName;
private DefaultStreamedContent file;
String callID;
#ManagedProperty("#{myTeamService}")
private MyTeamService service;
#PostConstruct
public void init() {
attachmentList = service.getAttachmentList(service.getCallId());
}
public void downloadImage() {
Session session = HibernateUtil.getSessionFactory().openSession();
Attachment attachment = null;
byte[] bImage;
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
if (getCallID() == null || getCallID().equals("")) {
RequestContext.getCurrentInstance().execute("PF('check_dialog').show()");
} else {
attachment = (Attachment) session.createCriteria(Attachment.class).add(Restrictions.eq("id.itemId", getImageItemId())).add(Restrictions.eq("id.callId", getCallID())).uniqueResult();
bImage = attachment.getFileData();
InputStream stream = new ByteArrayInputStream(bImage);
file = new DefaultStreamedContent(stream,externalContext.getMimeType(getFileName()),getFileName());
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
session.close();
}
}
public List<Attachment> getAttachmentList() {
return attachmentList;
}
public void setAttachmentList(List<Attachment> attachmentList) {
this.attachmentList = attachmentList;
}
public MyTeamService getService() {
return service;
}
public void setService(MyTeamService service) {
this.service = service;
}
public String getImageItemId() {
return imageItemId;
}
public void setImageItemId(String imageItemId) {
this.imageItemId = imageItemId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public DefaultStreamedContent getFile() {
return file;
}
public void setFile(DefaultStreamedContent file) {
this.file = file;
}
public String getCallID() {
return callID;
}
public void setCallID(String callID) {
this.callID = callID;
}
}
here dilogue opens correctly but image will not opened please help me.
How to implement a simple add/remove dynamic <rich:tabPanel>?
(I've seen people asking this around so I thought postin a Q&A of a simple implementation)
The implementation has 3 custom classes:
Content: contains the values to be displayed in a tab;
ItemTab: contais an UITab object and a Content object;
MyTabs: EJB managed bean that provides access to the tabs and adding/removal methods.
The code is:
Content:
public class Content {
String name;
String job;
String dept;
public Content() {
name = "John Doe";
job = "None";
dept = "None";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
TabItem:
public class TabItem {
UITab component;
Content content;
public TabItem() {
component = new UITab();
content = new Content();
}
public UITab getComponent() {
return component;
}
public void setComponent(UITab tab) {
this.component = tab;
}
public Content getContent() {
return content;
}
public void setContent(Content content) {
this.content = content;
}
}
MyTabs:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named
#SessionScoped
public class MyTabs implements Serializable {
private List<TabItem> tabs;
public MyTabs() {
tabs = new ArrayList<TabItem>();
}
#PostConstruct
private void init() {
createTab();
createTab();
createTab();
}
public void createTab() {
TabItem tab = new TabItem();
tab.getComponent().setId("Tab" + (tabs.size()+1));
tab.getComponent().setHeader("Tab " + (tabs.size()+1));
tab.getContent().setName("John Doe " + (tabs.size()+1));
tab.getContent().setJob("Salesman " + (tabs.size()+1));
tab.getContent().setDept("Sales " + (tabs.size()+1));
tabs.add(tab);
}
public void removeTab(TabItem tab) {
tabs.remove(tab);
}
public List<TabItem> getTabs() {
return tabs;
}
public void setTabs(List<TabItem> tabs) {
this.tabs = tabs;
}
}
tabview.xhtml:
<h:commandLink value="Add Tab"
actionListener="#{myTabs.createTab()}"/>
<rich:tabPanel switchType="client">
<c:forEach items="#{myTabs.tabs}" var="tab">
<rich:tab value="#{tab.component}">
<f:facet name="header">
<h:outputLabel value="#{tab.component.header}"/>
<h:commandLink value=" X" actionListener="#{myTabs.removeTab(tab)}"/>
</f:facet>
<h:panelGrid columns="2">
<h:outputLabel value="Name: "/>
<h:outputLabel value="#{tab.content.name}"/>
<h:outputLabel value="Dept: "/>
<h:outputLabel value="#{tab.content.dept}"/>
<h:outputLabel value="Job: "/>
<h:outputLabel value="#{tab.content.job}"/>
</h:panelGrid>
</rich:tab>
</c:forEach>
</rich:tabPanel>