Show a bean property in jsf....
Hi....
i'm a simple problem , i have already written on this forum for a similar problem,but i don't found a working solution....
I have a jsf application and i have to show a bean value into a jsp
page.
I show you my passes...please correct me where i go wrong,,,
For the first i declare property in backing bean
private String[] arraylinee;
In my application an user clicks a button, the application calls bean's
method and creates this array of string,that's what i want to show in
my jsp page.
I've developed in my backing bean getter and setter methods..
public String[] getarraylinee(){
return arraylinee;
}
publicvoid setarraylinee(String[] arraylinee){
this.arraylinee=arraylinee;
}
1a Question)
It's not needed that bean return property?
That is...my bean's method is
public String retrieveblob()throws
IOException,SQLException,NullPointerException{
Database2 db2 =new Database2("nomeDB","root","shevagol");
if (db2.connetti())
k=1;
else k=0;
//try{
Connection dbo=db2.getConnection();
Statement st =null;
try{
st = dbo.createStatement();
}
catch (NullPointerException exc){
exc.printStackTrace();
}
ResultSet rs =null;
try{
rs = st.executeQuery("SELECT Data FROM tbl WHERE nome='1' ");
}
catch (NullPointerException exc){
exc.printStackTrace();
}
try{
rs.first();
}
catch (NullPointerException exc){
exc.printStackTrace();
}
try{
Blob blob = rs.getBlob("Data");
byte[] read = blob.getBytes(1, (int) blob.length());
st.close();
String lettura=new String(read);
String[] arraylinee=lettura.split(";");
}
catch (NullPointerException exc){
exc.printStackTrace();
}
return"Result";
}
Result is a string useful for navigation...
Let's go to jsp page
codice:<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<jsp:useBean id="myBean" class="giu.MyBean" scope="session"/>
<%@ page import="giu.MyBean" %>
<%-- Instantiateclass --%>
<html>
<head>
<title></title>
</head>
<body>
<f:view>
<f:verbatim>
<h:outputText value="#{MyBean.k}"/>
<table>
<c:forEach items="${session.myBean.arraylinee}" var="linea" >
<tr>
<td>
<c:out value="${linea}" />
</td>
</tr>
</c:forEach>
</table>
</f:verbatim>
</f:view>
</body>
</html>
Don't care for MyBean.k, i use it for controlling if db connection
works fine...
In summary i want this jsp page prints my array of string,one for line.
How can i do?
The bean is called MyBean and it's in the package giu.
Can you help me correcting my code directly?
Ah....with my actual application the jsp output is:
1
${linea}
[5527 byte] By [
giubata] at [2007-10-3 0:14:50]

Thanks very much....
you give me ever good helps.......but i have a question.
I have read your article about datatable and i see the use of a wrapper class to encapsulate the data from database.
Now i have a blob,using a string constructor i read the array of byte and i obtain a string such as:
name value1 value2 value3.....;giuseppe 23,3 43,2 45,4.....;daniele 11,2 43,2 23,2...;....
where ; indicate the end of row. Using split function i obtain
name value1 value2 value3...
giuseppe 23,3 43,2 45,4
daniele 11,2 43,2 23,2
.........................
whit an headline (name value1 value2 value3) that is an array of string and the others line that have a string and an array of double for each.
What collection can i use to collect these two different object?
I have to develop the wrapper class like your example?
Please help me, i need to go on...
You can use any kind of Collection for any kind of object and datatype.
And using a wrapper class should be very useful. So you can separate the data over columns and have control over data.
MyRecordprivate String name;
private Double value1;
// etc .. and getters & setters.
MyBeanString[] record = content.split(";");
List myRecords = new ArrayList();
for (int i = 0; i < record.length; i++) {
String[] fields = record.split(" ");
MyRecord myRecord= new MyRecord();
myRecord.setName(field[0]);
try {
myRecord.setValue1(new Double(field[1]));
} catch (NumberFormatException e) { // Value is not numeric.
myRecord.setValue1(new Double(0)); // Default to 0. Or do your thing.
}
// etc ..
myRecords.add(myRecord);
}
setMyRecords(myRecords);
JSF<h:dataTable value="#{MyBean.myRecords}" var="record">
<h:column>
<h:outputText value="record.name" />
</h:column>
<h:column>
<h:outputText value="record.value1" />
</h:column>
// etc ..
</h:dataTable>
thanks....
i read you helps....
my problem is i'm not able to know how much columns there are in my file, i know it also when i read the file, so i can't create a wrapper class....
i've developed 2 object, the headline and the generic row
package giu;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Head{
private String[]intest=new String[col];
private static int col=0;
public Head(String[] x) {
this.intest=x;
}
public String[] getHvalues(){
return this.intest;
}
public void setvalues(String[] x){
this.intest=x;
}
}
package giu;
public class Riga{
private String geneid=null;
private Double[] values=null;
public Riga(String idGene,Double[] x ) {
this.geneid=idGene;
this.values=x;
}
public String getgeneid(){
return this.geneid;
}
public void setgeneid(String idGene){
this.geneid=idGene;
}
public Double[] getvalues(){
return this.values;
}
public void setvalues(Double[] x){
this.values=x;
}}
I can see my file as an array of string(the first line) and an arraylist of Riga object(the others line)...but how can i implement a datatable in this situation?
Have i use another component like columns of Myfaces that allow variable columns datatable?
Please help me,i'm bit confused....