PieChart using Prime faces 3.5 and jsf 2.0 coming empty - jsf-2

I am currently working on JSF 2.0 application which works as follows:
1) upload excel file using Prime faces(woerking fine)\ 2) access data
and update the third column of excel based on ping testing,set the
failed and passed count values in MultiplePingtesting bean(works
fine) 3) ChartBean4 should access the passed and failed count and
generate graph(Not working) 4) Testing with different files should
produce proper graph (Not working)
Working enviroment: JSF2.0 with prime faces 3.5, netbeans 7.1 using Glassfish server
Issue: If i try to access the failed and passed count in Chartbean4.java it is coming as zero and the piechart comes wrong!!! Please let me know if am wrong any where
faces-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>multiplepingtesting</managed-bean-name>
<managed-bean-class>control.MultiplePingTesting</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>chartbean4</managed-bean-name>
<managed-bean-class>control.ChartBean4</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>multiplepingtesting</property-name>
<value>#{multiplepingtesting}</value>
</managed-property>
</managed-bean>
</faces-config>
From.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>:: From ::</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{multiplepingtesting.handleFileUpload}"
mode="advanced" auto="true"
allowTypes="/(\.|\/)(xls|xlsx)$/" />
<p:growl id="display" showDetail="true" />
<p:commandButton value="suraj" action="PieChart" ajax="false"></p:commandButton>
</h:form>
//**MultiplePingTesting.java**//
public class MultiplePingTesting implements Serializable
{
public int passed, failed; //added getters and setters
private int i=0,j=0;
private UploadedFile fileName;//added getters and setters
public void handleFileUpload(FileUploadEvent event)
{
try
{
FileInputStream fileInputStream;
File fName=new File(event.getFile().getFileName());
fileInputStream = new FileInputStream(fName);
System.out.println("insdie try block ");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fsFileSystem);
String ip,message;
double port;
message="";
HSSFSheet sheet = workbook.getSheet("IP");
FileOutputStream out = new FileOutputStream(fName);
short col=2;
int lastrow=sheet.getLastRowNum()+1;
for(int rownum=1;rownum<lastrow;rownum++)
{
System.out.println("********" + rownum);
HSSFRow row = sheet.getRow(rownum);
ip=row.getCell((short)0).getStringCellValue();
port=row.getCell((short)1).getNumericCellValue();
try{
Socket s1 = new Socket(ip, (int) port);
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
if (dis != null)
{
System.out.println("Connected with ip "+ip+" and port 21");
message="Connected";
i++;
}
else
{
System.out.println("Connection invalid");
message="Connection Timed Out";
}
dis.close();
s1.close();
}
catch(Exception e)
{
System.out.println("Server not Connected,Connection timed out");
message="Server not Connected,Connection timed out";
j++;
}
row.createCell(col).setCellValue(message);
}
workbook.write(out);
out.close();
setFailed(i);
setPassed(j);
System.out.println("value of failed" + i + "*******"+getFailed());
System.out.println("value of succes" + getPassed());
}
catch(Exception e)
{
System.out.println("exception\n"+ e.getStackTrace());
}
}
//*PieChart.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>:: Piechart::</title>
</h:head>
<h:body >
<h:form>
<td align="right">
<h:outputText value="#{chartbean4.sj}"/>
<p:pieChart id="sample" value="#{chartbean4.pieModel}" legendPosition="w"
title="Sample Pie Chart" style="width:400px;height:300px" />
<h:commandButton name ="multiple_testing" action="From"/>
</td>
</h:form>
</h:body>
</html>
//*ChartBean4.java*/
public class ChartBean4 implements Serializable {
private MultiplePingTesting multiplepingtesting;
public MultiplePingTesting getMultiplepingtesting() {
return multiplepingtesting;
}
public void setMultiplepingtesting(MultiplePingTesting multiplepingtesting) {
this.multiplepingtesting = multiplepingtesting;
}
private PieChartModel pieModel;
#PostConstruct
public void init()
{
System.out.print("***********multiple_test.failed" + multiplepingtesting.getFailed());
createPieModel();
}
public PieChartModel getPieModel() {
return pieModel;
}
private void createPieModel() {
System.out.print("*****Inside Create Pie model******multiple_test.failed" + multiplepingtesting.getFailed());
pieModel = new PieChartModel();
pieModel.set("failed", multiplepingtesting.getFailed());
pieModel.set("success", multiplepingtesting.getPassed());
System.out.print("***********'");
}
}

Finally i got my answer with some workaround , here are the changes done
Changed both the scope in faces-config,xml to "request"
Introduced i and j variables as static in MultiplePingTesting.java.
In forward method of MultiplePingTesting failed and passed properties are set using i and j variables
***** Below are the updated files*******
From.xhtml
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{multiplepingtesting.handleFileUpload}"
mode="advanced" auto="true"
allowTypes="/(\.|\/)(xls|xlsx)$/" />
<p:commandButton value="PieChart" action="#{multiplepingtesting.forward}" ajax="false"></p:commandButton>
<p:growl id="display" showDetail="true" />
</h:form>
MultiplePingTesting.java
(Commented setFailed() and setPassed() code & appended forward() method in file
public String forward()
{
setFailed(j);
setPassed(i);
return "PieChart" ;
}

Related

Pass param from one jsf page to another

I want to pass selectedExamId from chooseexam page to exam page, what is the good way to do that? is it a good practice to use two baking bean for single jsf page?
One more thing is I am getting same Question list each time?
I have following jsf page
chooseExam.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 lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<ui:composition template="/templates/admin/template.xhtml">
<ui:define name="content">
<h:selectOneRadio value="#{examBean.selectedExamId}">
<f:selectItems value="#{chooseExamBean.exams}" var="exam" itemValue="#{exam.examId}" itemLabel="#{exam.examName}"/>
</h:selectOneRadio>
<h:commandButton value="Submit" action="/user/exam?faces-redirect=true"/>
</ui:define>
</ui:composition>
</h:body>
</html>
and
exam.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 lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<ui:composition template="/templates/admin/template.xhtml">
<ui:define name="content">
<ui:repeat value="#{examBean.questions}" var="question">
<h:outputLabel value="#{question.question}"/>
<h:selectOneRadio value="#{examBean.questionAnswerMap[question]}">
<f:selectItem itemValue="#{question.choice1}" itemLabel="#{question.choice1}"/>
<f:selectItem itemValue="#{question.choice2}" itemLabel="#{question.choice2}"/>
<f:selectItem itemValue="#{question.choice3}" itemLabel="#{question.choice3}"/>
<f:selectItem itemValue="#{question.choice4}" itemLabel="#{question.choice4}"/>
</h:selectOneRadio>
</ui:repeat>
<h:commandButton value="Submit" action="#{examBean.calculate}"/>
</ui:define>
</ui:composition>
</h:body>
</html>
following are the backing beans
ChooseExamBean.java
#Component
#ManagedBean
public class ChooseExamBean {
List<Exam> exams;
#Autowired
private ExamService examService;
#PostConstruct
public void init(){
exams = examService.getAllExams();
}
public List<Exam> getExams() {
return exams;
}
public void setExams(List<Exam> exams) {
this.exams = exams;
}
}
and
ExamBean.java
#Component
#ManagedBean
public class ExamBean {
private List<Question> questions;
private Map<Question, String> questionAnswerMap = new HashMap<>();
private int score;
private Long selectedExamId;
#Autowired
private QuestionService questionService;
#PostConstruct
public void init() {
if(selectedExamId != null)
questions = questionService.getQuestionsForExam(selectedExamId);
}
public Map<Question, String> getQuestionAnswerMap() {
return questionAnswerMap;
}
public void setQuestionAnswerMap(Map<Question, String> questionAnswerMap) {
this.questionAnswerMap = questionAnswerMap;
}
public List<Question> getQuestions() {
if(questions == null)
questions = questionService.getQuestionsForExam(selectedExamId);
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Long getSelectedExamId() {
return selectedExamId;
}
public void setSelectedExamId(Long selectedExamId) {
this.selectedExamId = selectedExamId;
}
public String calculate() {
score = questionAnswerMap.size();
return "result?faces-redirect=true";
}
}
Since you are using a RequestScope Beans for the (ExamBean and the ChooseExamBean) you cant keep the values after the response , so you should user the viewParam tag to pass the value from the first page to the second page.
you should make something like the following :
1- the ChooseExam.jsf , you have your radioButtons that will save its value in the chooseExamBean :
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<html xmlns="http://www.w3.org/1999/xhtml">
<h:head></h:head>
<h:body>
<h:form>
<h:selectOneRadio label="examType" value="#{chooseExamBean.examNumber}">
<f:selectItem itemLabel="exam1" itemValue="1"/>
<f:selectItem itemLabel="exam2" itemValue="2"/>
</h:selectOneRadio>
<h:commandButton value="commandButton1" action="#{chooseExamBean.navigateToExamPage}" />
</h:form>
</h:body>
</html>
</f:view>
2- in the action of the commandButton you will call a method in the bean to navigate , before navigation you will append a param to the url like the following in the navigateToExamPage method :
#ManagedBean(name = "chooseExamBean")
#RequestScoped
public class ChooseExamBean {
public ChooseExamBean() {
super();
}
private String examNumber;
public void setExamNumber(String examNumber) {
this.examNumber = examNumber;
}
public String getExamNumber() {
return examNumber;
}
public Object navigateToExamPage() {
return "exam?faces-redirect=true&examId="+getExamNumber();
}
}
3- in the exam.jsf page , you have to get the value of the parameter , here you will use the tag like the following :
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<html xmlns="http://www.w3.org/1999/xhtml">
<h:head></h:head>
<h:body>
<h:form>
<f:metadata>
<f:viewParam name="examId" value="#{examBean.examNumber}"/>
<f:event type="preRenderView" listener="#{examBean.onLoad}" />
</f:metadata>
<h:outputText value="#{examBean.examNumber}"/>
</h:form>
</h:body>
</html>
</f:view>
the view param have to attributes :
1- name :which is the name of the parameter that you want to get from the url.
2- value :which is where you want to set the value of the parameter.
so in our case the name is "examId" and we want to set the value in "examBean.examNumber" .
here you will find a problem if you didnt use tag , because you want to get the examId onPage Load in the postConstrct method , but the f:param will be called after the postConstruct , so we have to use the like the following :
<f:event type="preRenderView" listener="#{examBean.onLoad}" />
this will help us to perform custom task before a JSF page is displayed.
4- in you examBean :
#ManagedBean(name = "examBean")
#RequestScoped
public class ExamBean {
public ExamBean() {
super();
}
private String examNumber;
public void setExamNumber(String examNumber) {
this.examNumber = examNumber;
}
public String getExamNumber() {
return examNumber;
}
public void onLoad () {
System.out.println("onLoad = "+getExamNumber());
}
}
Please use this with your requirements and everything will go smooth.
Please refer to the following answer here, here and here .
Hope that Helps.

a4j:ajax keyup event not working

After a long time I'm using a4j:ajax to get simple text from h:inputText and render to h:outputText but it's not working
I found many examples like this but none of them worked for me.
this id my bean
import javax.faces.bean.*;
#ManagedBean(name="usertest")
#RequestScoped
public class UserBean {
private String name = "john";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and this is my index.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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: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:head>
</h:head>
<body>
<h:form id="f1">
<h:inputText id="name1" value="#{usertest.name}">
<a4j:ajax event="keyup" render="name2" execute="#form"></a4j:ajax>
</h:inputText>
<h:outputText id="name2" value="#{usertest.name}"></h:outputText>
</h:form>
</body>
</html>
I'm using JSF 2.0 and RichFaces 4.0
Any solution would help me.
Tanks

rich:dataScroller only displaying first page

my datascroller displays only first 5 records whenever i try to navigate.. i Am sure m making some silly mistake..
this my xhtml file
<!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:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="/template/BasicTemplate.xhtml">
<ui:define name="content">
<h:form>
<h:panelGrid>
<rich:dataScroller for="sampleData" maxPages="20" id="scroller"
page="#{richBean.scrollerPage}">
<rich:dataTable id="sampleData" value="#{richBean.employees}"
var="loc" rows="5">
<rich:column>
<h:outputText value="#{loc.empId}" />
</rich:column>
<rich:column>
<h:outputText value="#{loc.empName}" />
</rich:column>
<rich:column>
<h:outputText value="#{loc.dept}" />
</rich:column>
</rich:dataTable>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</html>
and this my java file
#ManagedBean(name = "richBean", eager = true)
#SessionScoped
public class JavaBeatRichfacesBean {
private List<employee> employees;
private int scrollerPage=1;
public int getScrollerPage() {
System.out.println("getScrollerPage");
return scrollerPage;
}
public void setScrollerPage(int scrollerPage) {
System.out.println("setScrollerPage");
this.scrollerPage = scrollerPage;
}
public JavaBeatRichfacesBean() {
this.employees = new ArrayList<employee>();
employee employee = null;
for (int i = 1; i < 50; i++) {
employee = new employee();
employee.setEmpId(String.valueOf(i));
employee.setEmpName("Name : " + i);
employee.setDept("JSF");
employees.add(employee);
}
}
public List<employee> getEmployees() {
System.out.println(employees.size());
return employees;
}
public void setEmployees(List<employee> employees) {
this.employees = employees;
}
}
EDITED
Two thing i have noticed is
I have to refresh each time to change page navigation.
if use h:dataTable instead of rich:dataTable.. it is working
properly ,Surprisingly ..
and if i add execute="#form" render="#form" in rich:datatable is working properly but i have double click everything..
any explanation why this happening would be useful..

jsf doesn't look for its beans

I am building a web app based on ICEmobile with JSF 2.1 and Maven. I'm running into this problem when my jsf page doesn't look for its bean but I still can build and deploy the project with no errors, I deploy the project via run configuration in Eclipse: clean tomcat7:run-war. Even if I declare the bean via annotation or applicationContext.xml, the page still doesn't call the bean correctly. I can get the value of the field in the bean but cannot call the method within that bean System.out.println("Pressed"); gives no output to the console. I'm guessing it must be something to do with my configuration but I don't know where to look into. Please give me some hints. Thanks
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:util="http://java.sun.com/jsf/composite/components"
xmlns:mobi="http://www.icesoft.com/icefaces/mobile/component"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:icecore="http://www.icefaces.org/icefaces/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<meta name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>ICEfaces Mobile Showcase</title>
<mobi:deviceStylesheet media="screen" />
</h:head>
<h:body>
<mobi:pagePanel>
<f:facet name="body">
<h:form>
<mobi:commandButton value="#{buttonBean.buttonName}" buttonType="important" actionListener="#{buttonBean.buttonPress}">
<f:attribute name="buttonState" value="change"/>
</mobi:commandButton>
</h:form>
</f:facet>
</mobi:pagePanel>
</h:body>
</html>
The Bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
#ManagedBean(name = ButtonBean.BEAN_NAME)
#SessionScoped
public class ButtonBean implements Serializable {
private static final long serialVersionUID = 1L;
public static final String BEAN_NAME = "buttonBean";
private String buttonName = "0";
public String getButtonName() {
return buttonName;
}
public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
public void buttonPress(ActionEvent event){
System.out.println("Pressed");
String buttonState = (String)event.getComponent().getAttributes().get("buttonState");
if (buttonState.equals("change") && buttonName.equals("0")) buttonName = "1";
else if (buttonState.equals("change") && buttonName.equals("1")) buttonName = "0";
}
}
I found out that my web.xml was set as version 2.5. Change to version 3.0. All works now

Spring Web flow with JSF evaluation

I am new to Spring Web flow.
This is my code.
main-flow.xml
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="hello" class="org.springframework.webflow.samples.booking.hello"/>
<view-state id="hello">
<transition on="click" to="test"></transition>
</view-state>
<action-state id="test">
<evaluate expression="hello.saytest()">
</evaluate>
<transition on="yes" to="page1" ></transition>
<transition on="no" to="page2"></transition>
</action-state>
<view-state id="page1"></view-state>
<view-state id="page2"></view-state>
</flow>
Hello.java
package org.springframework.webflow.samples.booking;
import java.io.Serializable;
public class hello implements Serializable {
String name;
/**
*
*/
#Override
public String toString() {
return "hello [name=" + name + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private static final long serialVersionUID = 1L;
public boolean saytest() {
System.out.println(name);
String test = "new";
if (name == test) {
return true;
} else {
return false;
}
}
}
hello.xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 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:p="http://primefaces.org/ui"
template="/WEB-INF/layouts/standard.xhtml">
<ui:define name="notes">
<p>
Check the calendar widget and the tooltips on the credit card fields.
The form uses Ajax-based validations to redisplay server-side errors without refreshing the entire page.
The booking bean backing the form is a flow-scoped object (see <strong>booking-flow.xml</strong>).
</p>
</ui:define>
<ui:define name="content">
<h:form>
<h:outputLabel for="price">Enter the Name:</h:outputLabel>
<h:inputText id="name" value="#{hello.name}">
</h:inputText>
<p:commandButton id="click" action="click" value="click" />
</h:form>
</ui:define>
</ui:composition>
Here I am trying to get some value from hello.xhtml and try to evaluate it in main-flow.xml.
If the value which i tried is new then it have to navigate to page1 or it have to take it to page2.xhtml.Now I my case even i enter new in inputtext box it take to me to page2 only.
Can anyone help me.
Change
if (name == test)
to
if (name.equals(test))
Or, simply,
if (name.equals("new"))
See http://www.javabeginner.com/learn-java/java-string-comparison

Resources