Coger Bean de la sesión

Para utilizar una clase Bean desde otro Bean solo hay que escribir:

BeanType pp = (VocabularyBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("NOMBRE DEL BEAN");

By Miguel Ruiz on jueves, 20 de enero de 2011 | | A comment?

Obtener datos de una Tabla desde un Bean


< h:dataTable var="item" value="#{MyBean.items}"
binding="#{MyBean.dataTable}" >
< h:column>
< h:outputText styleClass="output" value="#{item.productName}"/>
< /h:column>

< h:column>
< h:commandButton value="remove" action="#{MyBean.remove}" />
< /h:column>
< /h:dataTable>






En el bean solo tenemos que...


public class MyBean {
private ArrayList items = new ArrayList();
private HtmlDataTable dataTable;

public ArrayList getItems() {
return items;
}

public void setItems(List items) {
this.items = new ArrayList(items);
}

public void remove(){
ItemBean item = (ItemBean) getDataTable().getRowData();
items.remove(item);
}

public HtmlDataTable getDataTable() {
return dataTable;
}

public void setDataTable(HtmlDataTable dataTable){
this.dataTable = dataTable;
}

}



By Miguel Ruiz on miércoles, 19 de enero de 2011 | | A comment?

Leer archivo de texto

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadTextFileExample
{
public static void main(String[] args)
{
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

try
{
reader = new BufferedReader(new FileReader(file));
String text = null;

// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (reader != null)
{
reader.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}

// show file contents here
System.out.println(contents.toString());
}
}

By Miguel Ruiz on viernes, 14 de enero de 2011 | | A comment?