JSF
Sunday, 19 June 2016
JSF & Tomahawk File Upload
Programming concepts
- JSF 2.0
- Tomahawk
- Eclipse Helious
1.Index.xhtml
<!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://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:body>
<h2>JSF Tomwakh File Upload</h2>
<h:form id="uploadForm" enctype="multipart/form-data">
<h:panelGrid columns="3">
<h:outputLabel for="file" value="Select file" />
<t:inputFileUpload id="file" required="true" value="#{myBean.uploadedFile}"/>
<h:message for="file" style="color: red;" />
<h:panelGroup />
<h:commandButton value="Submit" action="#{myBean.submit}"/>
<h:message for="uploadForm" infoStyle="color: green;" errorStyle="color: red;" />
</h:panelGrid>
</h:form>
<br />
<h:outputLink value="file/#{myBean.fileName}" rendered="#{myBean.fileName != null}">
Download back
</h:outputLink>
</h:body>
</html>
2.MyBean.Java
package mypackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class MyBean {
private UploadedFile uploadedFile;
private String fileName;
//-----------Action-----------------
public void submit() {
// Just to demonstrate what information you can get from the uploaded file.
System.out.println("File type: " + uploadedFile.getContentType());
System.out.println("File name: " + uploadedFile.getName());
System.out.println("File size: " + uploadedFile.getSize() + " bytes");
// Prepare filename prefix and suffix for an unique filename in upload folder.
String prefix = FilenameUtils.getBaseName(uploadedFile.getName());
String suffix = FilenameUtils.getExtension(uploadedFile.getName());
// Prepare file and outputstream.
File file = null;
OutputStream output = null;
try {
// Create file with unique name in upload folder and write to it.
file = File.createTempFile(prefix + "_", "." + suffix, new File("c:/upload"));
output = new FileOutputStream(file);
IOUtils.copy(uploadedFile.getInputStream(), output);
fileName = file.getName();
// Show success message.
FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
FacesMessage.SEVERITY_INFO, "File upload succeed!", null));
} catch (IOException e) {
// Cleanup.
if (file != null) file.delete();
// Show error message.
FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
// Always log stacktraces (with a real logger).
e.printStackTrace();
} finally {
IOUtils.closeQuietly(output);
}
}//end of Method
//-----------Getter and Setter-----------------
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}//end of Class
3.web.xml
<filter>
<filter-name>Extensions Filter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
4.faces_config.xml
<br />
<b>Parse error</b>: syntax error, unexpected T_STRING in <b>/home/dharmara/public_html/JSF/jsfFileUp/Browse Code/d.php</b> on line <b>1</b><br />
Manage Book CRUD Application
|
1.Index.xhtml
<p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <br />
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></p>
<p><html xmlns="http://www.w3.org/1999/xhtml"<br />
xmlns:ui="http://java.sun.com/jsf/facelets"<br />
xmlns:h="http://java.sun.com/jsf/html"<br />
xmlns:f="http://java.sun.com/jsf/core"><br />
<head><br />
<title>Dharmaraj.Net</title><br />
</head><br />
<body><br />
<h1>Book Manager Jsf Crud By Dharmaraj.Net</h1><br />
<h:dataTable var="book" value="#{bookController.listBook}" border="1" bgcolor="Silver"><br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Title"/><br />
</f:facet><br />
<h:outputText value="#{book.title}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Author"/><br />
</f:facet><br />
<h:outputText value="#{book.author}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Pages"/><br />
</f:facet><br />
<h:outputText value="#{book.pages}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Publishing House"/><br />
</f:facet><br />
<h:outputText value="#{book.publishingHouse}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="ISBN"/><br />
</f:facet><br />
<h:outputText value="#{book.isbn}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Grading"/><br />
</f:facet><br />
<h:outputText value="#{book.grading}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Change"/><br />
</f:facet><br />
<h:form><br />
<h:commandButton action="#{bookController.prepareChangeBook}" value="Change"/><br />
</h:form><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Delete"/><br />
</f:facet><br />
<h:form><br />
<h:commandButton action="#{bookController.deleteBook}" value="Delete"/><br />
</h:form><br />
</h:column><br />
<br />
</h:dataTable><br />
<br />
<br/><br />
<h:form><br />
<h:commandLink value="New Book" action="#{bookController.addBookToPrepare}"/><br />
</h:form> <br />
</body><br />
</html></p>
2.manageBook.xhtml
<p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <br />
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></p>
<p><html xmlns="http://www.w3.org/1999/xhtml"<br />
xmlns:ui="http://java.sun.com/jsf/facelets"<br />
xmlns:h="http://java.sun.com/jsf/html"<br />
xmlns:f="http://java.sun.com/jsf/core"></p>
<p><head><br />
<title>Manage Books</title><br />
</head><br />
<h:body><br />
<h1>Add Book</h1><br />
<h:form><br />
<h:commandLink action="index" value="Return"/><br />
<h:panelGrid columns="2" border="1" bgcolor="Silver"><br />
Title <h:inputText value="#{bookController.book.title}"/> <br /><br />
Author <h:inputText value="#{bookController.book.author}"/> <br /><br />
Pages <h:inputText value="#{bookController.book.pages}"/> <br /><br />
PublishingHouse <h:inputText value="#{bookController.book.publishingHouse}"/> <br /><br />
ISBN <h:inputText value="#{bookController.book.isbn}"/> <br /><br />
Grading <h:selectOneMenu value="#{bookController.book.grading}"><br />
<f:selectItem itemLabel="1" itemValue="1"/><br />
<f:selectItem itemLabel="2" itemValue="2"/><br />
<f:selectItem itemLabel="3" itemValue="3"/><br />
<f:selectItem itemLabel="4" itemValue="4"/><br />
<f:selectItem itemLabel="5" itemValue="5"/><br />
</h:selectOneMenu><br />
<h:commandButton value="Add New Book" action="#{bookController.addBook}"/><br />
<h:commandButton value="change Book" action="#{bookController.changeBook}"/><br />
</h:panelGrid> <br />
</h:form><br />
</h:body><br />
</html></p>
3.HibernateUtil.java
<p>package Utils;</p>
<p>import org.hibernate.SessionFactory;<br />
import org.hibernate.cfg.AnnotationConfiguration;</p>
<p>import Model.Book;</p>
<p>public class HibernateUtil {<br />
private static SessionFactory sessionFactory;<br />
<br />
private HibernateUtil() {} // Constructor<br />
<br />
public static SessionFactory getSessionFactory(){<br />
if(sessionFactory == null){<br />
try {<br />
// Create the SessionFactory from standard (hibernate.cfg.xml)<br />
//config file.<br />
AnnotationConfiguration ac = new AnnotationConfiguration();<br />
ac.addAnnotatedClass(Book.class);<br />
sessionFactory = ac.configure().buildSessionFactory();<br />
} catch (Throwable ex) {<br />
System.err.println("Session Factory Error: "+ex);<br />
throw new ExceptionInInitializerError(ex);<br />
}//end of try catch block <br />
return sessionFactory;<br />
}else{<br />
return sessionFactory;<br />
}//end of else part<br />
}//end of Method<br />
}//end of Class<br />
</p>
4.Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><br />
<!DOCTYPE hibernate-configuration PUBLIC<br />
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br />
<hibernate-configuration><br />
<session-factory><br />
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><br />
<property name="hibernate.connection.password">root</property><br />
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property><br />
<property name="hibernate.connection.username">root</property><br />
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><br />
<property name="hibernate.hbm2ddl.auto">update</property><br />
<property name="show_sql">true</property><br />
</session-factory><br />
</hibernate-configuration><br />
5.BookBeanControler.java
<p>package Controller;</p>
<p>import java.util.List;</p>
<p>import javax.faces.model.DataModel;<br />
import javax.faces.model.ListDataModel;</p>
<p>import Dao.BookDao;<br />
import Dao.BookDaoImpl;<br />
import Model.Book;</p>
<p>public class BookBeanControler {<br />
private Book book;<br />
private DataModel listBook;<br />
//=========================================================<br />
public DataModel<Book> getListOfBook(){<br />
List<Book> books = new BookDaoImpl().listOfBooks();<br />
listBook = new ListDataModel<Book>(books);<br />
return listBook; <br />
}//end of Method<br />
//=========================================================<br />
public String addBookToPrepare(){<br />
book = new Book();<br />
return "manageBook";<br />
}//end of addBookToPrepare()<br />
//=========================================================<br />
public String prepareChangeBook(){<br />
book = (Book)(listBook.getRowData());<br />
return "manageBook";<br />
}//end of prepareChangeBook()<br />
//=========================================================<br />
public String deleteBook(){<br />
Book bookTemp = (Book)(listBook.getRowData());<br />
BookDao dao = new BookDaoImpl();<br />
dao.remove(bookTemp);<br />
return "index";<br />
}//end of deleteBook()<br />
//=========================================================<br />
public String addBook(){<br />
BookDao dao = new BookDaoImpl();<br />
dao.save(book);<br />
return "index";<br />
}//end of addBook()<br />
//=========================================================<br />
public String changeBook(){<br />
BookDao dao = new BookDaoImpl();<br />
dao.update(book);<br />
return "index";<br />
}//end of ChangeBook<br />
//=========================================================<br />
public Book getBook() {<br />
return book;<br />
}<br />
public void setBook(Book book) {<br />
this.book = book;<br />
}<br />
public DataModel getListBook() {<br />
List<Book> books = new BookDaoImpl().listOfBooks();<br />
listBook = new ListDataModel<Book>(books);<br />
return listBook; <br />
}<br />
public void setListBook(DataModel listBook) {<br />
this.listBook = listBook;<br />
}<br />
<br />
}//end of Class<br />
</p>
Subscribe to:
Posts (Atom)