Show a bean property in jsf....it's so difficult?????

G

gbattine

Hi....
i'm a simple 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;
}
public void 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" %>
<%-- Instantiate class --%>




<html>
<head>
<title></title>
</head>
<body>
<f:view>
<f:verbatim>
<h:eek:utputText value="#{MyBean.k}"/>
<table>
<c:forEach items="${session.myBean.arraylinee}" var="linea" >
<tr>
<td>
<c:eek:ut 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}
 
M

Moiristo

gbattine said:
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.

Have you tested whether the bean method is actually called?
1a Question)
It's not needed that bean return property?

It is, or your bean is 'malformed'. Some JSF implementations persist on
always having getters and setters, even though you don't always use both.

<jsp:useBean id="myBean" class="giu.MyBean" scope="session"/>

What's this? Don't use this. JSTL can access managed beans as easy as
JSF, you don't need to use this tag.
<%@ page import="giu.MyBean" %>

I don't think you need this one either.

<c:forEach items="${session.myBean.arraylinee}" var="linea" >

So I would say, just use ${myBean.arraylinee}.



Hope this helps,

Moiristo
 
T

Timo Stamm

gbattine said:
I've developed in my backing bean getter and setter methods..


public String[] getarraylinee(){
return arraylinee;
}
public void setarraylinee(String[] arraylinee){
this.arraylinee=arraylinee;
}


You have to use camelcase. The first letter following "get" "set" or
"is" must be uppercase.

getArraylinee
setArraylinee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top