Maven + JSF + RichFaces + JDBC + JXL API For Excel Export (Using Maven Project in Eclipse IDE)
Maven + JSF + RichFaces + JDBC + JXL API For Excel Export (Using Maven Project in Eclipse IDE)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude>**/*local.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src\main\resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.vm</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>src\main\webapp\WEB-INF</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<server>devserver</server>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
This should automatically start downloading maven jar files related to the
pom.xml entries. If not, then
1) right click on project from project explorer
2) select Run As -> Maven Install
<context-param>
<param-name>org.richfaces.skin</param-name>
<param-value>blueSky</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.faces</welcome-file>
</welcome-file-list>
</web-app>
<managed-bean>
<managed-bean-name>dbConnectionController</managed-bean-name>
<managed-bean-class>XXXX.database.DbConnectionController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>registeredUsersUIController</managed-bean-name>
<managed-bean-class>XXXX.ui.RegisteredUsersUIController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>dbConnectionController</property-name>
<value>#{dbConnectionController}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>registeredUsersReport</from-outcome>
<to-view-id>/registeredUsersReport.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config>
DbConnectionController.java
It is always advisable to create a seperate controller class for storing database connection details. In entire project, only
this file should have hard coded database connection details so that in case of changing the connection details, only
one file needs to be modified.
package XXXX.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DbConnectionController {
Connection conn;
Statement stmt;
DriverManager driverManager;
public DbConnectionController() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/DB_NAME", "DB_USER",
"DB_PASSWD");
stmt = conn.createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
public DriverManager getDriverManager() {
return driverManager;
}
ApplicationConstants.java
Store standard error messages, file names etc in this file. Don't use file names anywhere in the project explicitely. This file
should be the only file where you should be able to update file names (apart from faces-config.xml)
package XXXX.constants;
public class ApplicationConstants {
public static final String ERROR_STRING = "There is some error executing the database query. Go to home page and
please try again.";
public static final String HOME_PAGE = "index";
public static final String REGISTERED_USERS = "registeredUsersReport";
}
RegisteredUsersTO.java
Use a transfer object as resultset. This List of these objects will store the data retrieved from the database. This is a
standard coding practice.
package XXXX.resultsets;
import java.util.Date;
public class RegisteredUsersTO {
String firstName;
String lastName;
String emailAddress;
String addressLine1;
String addressLine2;
String country;
String state;
String city;
String pinCode;
Date dateOfBirth;
String gender;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the emailAddress
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @param emailAddress the emailAddress to set
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
/**
* @return the addressLine1
*/
public String getAddressLine1() {
return addressLine1;
}
/**
* @param addressLine1 the addressLine1 to set
*/
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
/**
* @return the addressLine2
*/
public String getAddressLine2() {
return addressLine2;
}
/**
* @param addressLine2 the addressLine2 to set
*/
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(String country) {
this.country = country;
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the pinCode
*/
public String getPinCode() {
return pinCode;
}
/**
* @param pinCode the pinCode to set
*/
public void setPinCode(String pinCode) {
this.pinCode = pinCode;
}
/**
* @return the dateOfBirth
*/
public Date getDateOfBirth() {
return dateOfBirth;
}
/**
* @param dateOfBirth the dateOfBirth to set
*/
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
BaseUIController.java
You can store all common methods/procedures which all controller classes will access under this class. Controller classes
will inherit this class.
package XXXX.ui;
import java.io.Serializable;
import javax.faces.context.FacesContext;
public class BaseUIController implements Serializable {
private static final long serialVersionUID = 1L;
private static final String appVersion = "1.0";
public String getRealFileStoragePath() {
return FacesContext.getCurrentInstance().getExternalContext()
.getRealPath("/");
}
}
RegisteredUsersUIController.java
package XXXX.ui;
import
import
import
import
import
java.io.File;
java.sql.ResultSet;
java.util.ArrayList;
java.util.List;
java.util.Locale;
import javax.faces.application.FacesMessage;
import
import
import
import
import
import
import
import
jxl.Workbook;
jxl.WorkbookSettings;
jxl.write.Label;
jxl.write.WritableSheet;
jxl.write.WritableWorkbook;
XXXX.constants.ApplicationConstants;
XXXX.database.DbConnectionController;
XXXX.resultsets.RegisteredUsersTO;
.getString("STATE_NAME"));
registeredUsersTO.setCountry(resultSet
.getString("COUNTRY_NAME"));
registeredUsersTO.setDateOfBirth(resultSet
.getDate("DATE_OF_BIRTH"));
registeredUsersTO.setGender(resultSet
.getString("GENDER"));
registeredUsersTOList.add(registeredUsersTO);
}
this.excelFile = ApplicationConstants.REGISTERED_USERS_EXCEL;
exportToExcel();
return ApplicationConstants.REGISTERED_USERS;
} catch (Exception e) {
addFacesMessage(FacesMessage.SEVERITY_ERROR,
ApplicationConstants.ERROR_STRING);
e.printStackTrace();
return null;
}
}
public void exportToExcel() {
try {
File file = new File(getRealFileStoragePath() + excelFile);
int rowcntr = 2;
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file,
wbSettings);
workbook.createSheet(
"Registered Users",
0);
WritableSheet excelSheet = workbook.getSheet(0);
excelSheet.addCell(new Label(0, 1, "FIRST NAME"));
excelSheet.addCell(new Label(1, 1, "LAST NAME"));
excelSheet.addCell(new Label(2, 1, "EMAIL ADDRESS"));
excelSheet.addCell(new Label(3, 1, "ADDRESS LINE 1"));
excelSheet.addCell(new Label(4, 1, "ADDRESS LINE 2"));
excelSheet.addCell(new Label(5, 1, "PIN CODE"));
excelSheet.addCell(new Label(6, 1, "CITY"));
excelSheet.addCell(new Label(7, 1, "STATE"));
excelSheet.addCell(new Label(8, 1, "COUNTRY"));
excelSheet.addCell(new Label(9, 1, "DATE OF BIRTH"));
excelSheet.addCell(new Label(10, 1, "GENDER"));
for (RegisteredUsersTO registeredUsersTO : registeredUsersTOList) {
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
excelSheet.addCell(new
Label(0,
Label(1,
Label(2,
Label(3,
Label(4,
Label(5,
Label(6,
Label(7,
Label(8,
Label(9,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
rowcntr,
registeredUsersTO.getFirstName()));
registeredUsersTO.getLastName()));
registeredUsersTO.getEmailAddress()));
registeredUsersTO.getAddressLine1()));
registeredUsersTO.getAddressLine2()));
registeredUsersTO.getPinCode()));
registeredUsersTO.getCity()));
registeredUsersTO.getState()));
registeredUsersTO.getCountry()));
String.valueOf(registeredUsersTO.getDateOfBirth())));
*/
public ResultSet getResultSet() {
return resultSet;
}
/**
* @param resultSet the resultSet to set
*/
public void setResultSet(ResultSet resultSet) {
this.resultSet = resultSet;
}
/**
* @return the excelFile
*/
public String getExcelFile() {
return excelFile;
}
/**
* @param excelFile the excelFile to set
*/
public void setExcelFile(String excelFile) {
this.excelFile = excelFile;
}
registeredUsersReport.xhtml
<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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:form>
<div style="float: left; margin-left: 235px; margin-bottom: 5px;">
<br /> <br />
<h2>List of Registered Users of Website</h2>
<rich:dataTable id="registeredUsersDataTable" var="registeredUsrs"
style="width:960px;" rows="20"
value="#{registeredUsersUIController.registeredUsersTOList}"
rendered="#{not empty registeredUsersUIController.registeredUsersTOList}">
<f:facet name="header">
<rich:columnGroup>
<rich:column>
<h:outputText value="Name" />
</rich:column>
<rich:column>
<h:outputText value="Email Address" />
</rich:column>
<rich:column>
<h:outputText value="Complete Address" />
</rich:column>
<rich:column>
<rich:column>
<h:outputText
value="#{registeredUsrs.firstName} #{registeredUsrs.lastName}" />
</rich:column>
<rich:column>
<h:outputText value="#{registeredUsrs.emailAddress}" />
</rich:column>
<rich:column>
<h:outputText value="#{registeredUsrs.addressLine1}" />
, <h:outputText value="#{registeredUsrs.addressLine2}" />
, <h:outputText value="#{registeredUsrs.city}" />
, <h:outputText value="#{registeredUsrs.state}" />
, <h:outputText value="#{registeredUsrs.pinCode}" />
</rich:column>
<rich:column>
<h:outputText value="#{registeredUsrs.country}" />
</rich:column>
<rich:column>
<h:outputText value="#{registeredUsrs.dateOfBirth}" />
</rich:column>
<rich:column>
<h:outputText value="#{registeredUsrs.gender}" />
</rich:column>
</rich:dataTable>
<br />
<div align="center">
<rich:dataScroller for="registeredUsersDataTable" />
</div>
<br />
<h:outputLink
value="#{registeredUsersUIController.excelFile}">Export To Excel</h:outputLink>
<h:commandLink
action="#{registeredUsersUIController.goToHome()}"
value="Go to Home Page" style="margin-left:760px !important;" />
</div>
</html>
</h:form>
index.xhtml
When the application starts, by default index.xhtml will get called. Once the user clicks on the commandLink present in
this file, registeredUsersReport.xhtml will show the detailed report.
<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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:form>
<div style="float: left; margin-left: 235px; margin-bottom: 5px;">
<h2>List Of Registered Users</h2>
</html>
<a4j:commandLink
action="#{registeredUsersUIController.getRegisteredUsers()}"
value="List of Registered Users of Website" />
</div>
</h:form>
This will now generate war file. You need to copy this war file and deploy it on tomcat.