the f:attribute tag is very useful when you want to pass an attributer inside a commandButton tag.
It plays in this way:
<h:commandButton id="bottoneForm"
actionListener="#{managebBean.actionListenerFunction}">
<f:attribute name="attributeName" value="attributeValue" >
</f:attribute>
</h:commandButton>
For to read it in the called actionListener function of the managebBean:
public void actionListenerFunction(ActionEvent e){
Object valueOfAttribute = e.getComponent().getAttributes().get("attributeName");
}
That's all Folks!
How to use h:selectOneRadio
Example
<h:selectOneRadio binding="#{visProfiliManagedBean.profiloDefaultModificato}" immediate="true"
value="#{visProfiliManagedBean.profDaModificare.profiloDefault}"
rendered="#{f.codProfilo==visProfiliManagedBean.profDaModificare.codProfilo&&visProfiliManagedBean.visModifica}">
<f:selectItem itemLabel="Si" itemValue="1" />
<f:selectItem itemLabel="No" itemValue="0" />
</h:selectOneRadio>
where visProfiliManagedBean.profiloDefaultModificato is a HtmlSelectOneRadio object and visProfiliManagedBean.profDaModificare.profiloDefault is a boolean, but it can be any type of object (ex.: String, int, etc.)
<h:selectOneRadio binding="#{visProfiliManagedBean.profiloDefaultModificato}" immediate="true"
value="#{visProfiliManagedBean.profDaModificare.profiloDefault}"
rendered="#{f.codProfilo==visProfiliManagedBean.profDaModificare.codProfilo&&visProfiliManagedBean.visModifica}">
<f:selectItem itemLabel="Si" itemValue="1" />
<f:selectItem itemLabel="No" itemValue="0" />
</h:selectOneRadio>
where
Empty JBoss Cache
I did a new Login page for my web application. I spent 3 days for the new layout.
So the only thing that separeted me from my new satisfaction was the deploy in the production environment
After the deploy in Jboss, i connect with the web application and ... oops: the login page was not changed!
OK! The first thing i I thinked was IE cache! But it wasn't...
The problem was the JBoss cache.
you've to delete the file {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}.war
and {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}
So the only thing that separeted me from my new satisfaction was the deploy in the production environment
After the deploy in Jboss, i connect with the web application and ... oops: the login page was not changed!
OK! The first thing i I thinked was IE cache! But it wasn't...
The problem was the JBoss cache.
you've to delete the file {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}.war
and {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}
sendRedirect or writing to a Servlet response stream in JSF
Problem: i want to do a sendRedirect from an actionListener method of a JSF backingBean.
After reading the servlet response i do a sendRedirect but it is not sufficent, because i got this exception:
java.lang.IllegalStateException: Cannot forward after response has been committed
So, i must include this code after the sendRedirect method:
FacesContext context= FacesContext.getCurrentInstance();
context.responseComplete();
Ex.:
try {
HttpJsfUtil.getResponse().sendRedirect(url);
FacesContext context=FacesContext.getCurrentInstance();
context.responseComplete();
} catch (IOException e1) {
e1.printStackTrace();
}
The code above is the same in the case of a response stream: if you want to write in the response stream inside the actionListener method, after you close the response you have to include the above code before exit the method.
Ex.:
try{
HttpJsfUtil.getResponse().setContentType("application/x-download");
HttpJsfUtil.getResponse().setHeader("Content-Disposition", "attachment; filename="+fileName);
HttpJsfUtil.getResponse().setHeader("Cache-Control", "no-cache");
ServletOutputStream out= HttpJsfUtil.getResponse().getOutputStream();
out.write(imageBytes);
out.close();
FacesContext context=FacesContext.getCurrentInstance();
context.responseComplete();
}
catch (IOException ex)
{
System.out.println ("Envelope error: " + ex.getMessage ());
}
where HttpJsfUtil.getResponse() is
public static HttpServletResponse getResponse()
{
return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
}
After reading the servlet response i do a sendRedirect but it is not sufficent, because i got this exception:
java.lang.IllegalStateException: Cannot forward after response has been committed
So, i must include this code after the sendRedirect method:
FacesContext context= FacesContext.getCurrentInstance();
context.responseComplete();
Ex.:
try {
HttpJsfUtil.getResponse().sendRedirect(url);
FacesContext context=FacesContext.getCurrentInstance();
context.responseComplete();
} catch (IOException e1) {
e1.printStackTrace();
}
The code above is the same in the case of a response stream: if you want to write in the response stream inside the actionListener method, after you close the response you have to include the above code before exit the method.
Ex.:
try{
HttpJsfUtil.getResponse().setContentType("application/x-download");
HttpJsfUtil.getResponse().setHeader("Content-Disposition", "attachment; filename="+fileName);
HttpJsfUtil.getResponse().setHeader("Cache-Control", "no-cache");
ServletOutputStream out= HttpJsfUtil.getResponse().getOutputStream();
out.write(imageBytes);
out.close();
FacesContext context=FacesContext.getCurrentInstance();
context.responseComplete();
}
catch (IOException ex)
{
System.out.println ("Envelope error: " + ex.getMessage ());
}
where HttpJsfUtil.getResponse() is
public static HttpServletResponse getResponse()
{
return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
}
From InputStream to byte array
Oh! And finally happen!
This is the code from converting(/writing/copying) an InputStream object into an array of byte:
InputStream is=urlc.getInputStream();
ByteArrayOutputStream outb = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = is.read(buffer)) > -1) {
outb.write(buffer, 0, bytesRead);
}
is.close();
byte[] imageBytes = outb.toByteArray();
And it works!
This is the code from converting(/writing/copying) an InputStream object into an array of byte:
InputStream is=urlc.getInputStream();
ByteArrayOutputStream outb = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = is.read(buffer)) > -1) {
outb.write(buffer, 0, bytesRead);
}
is.close();
byte[] imageBytes = outb.toByteArray();
And it works!
NT Authorization in a web application
This time i speak about the authorization microsoft windows based code in a java web application.
This is the the (servlet) code:
String auth = request.getHeader("Authorization");
if (auth == null)
{
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
response.flushBuffer();
return;
}
if (auth.startsWith("NTLM "))
{
byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
int off = 0, length, offset;
if (msg[8] == 1)
{
byte z = 0;
byte[] msg1 = {(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S', (byte)'S', (byte)'P', z,(byte)2, z, z, z, z, z, z, z,(byte)40, z, z, z, (byte)1, (byte)130, z, z,z, (byte)2, (byte)2, (byte)2, z, z, z, z, z, z, z, z, z, z, z, z};
response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(msg1));
response.sendError(response.SC_UNAUTHORIZED);
return;
}
else if (msg[8] == 3)
{
off = 30;
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
String domain = new String(msg, offset, length);
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
String user = new String(msg, offset, length);
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+18];
String computerName = new String(msg, offset, length);
}
}
This is the the (servlet) code:
String auth = request.getHeader("Authorization");
if (auth == null)
{
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
response.flushBuffer();
return;
}
if (auth.startsWith("NTLM "))
{
byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
int off = 0, length, offset;
if (msg[8] == 1)
{
byte z = 0;
byte[] msg1 = {(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S', (byte)'S', (byte)'P', z,(byte)2, z, z, z, z, z, z, z,(byte)40, z, z, z, (byte)1, (byte)130, z, z,z, (byte)2, (byte)2, (byte)2, z, z, z, z, z, z, z, z, z, z, z, z};
response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(msg1));
response.sendError(response.SC_UNAUTHORIZED);
return;
}
else if (msg[8] == 3)
{
off = 30;
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
String domain = new String(msg, offset, length);
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
String user = new String(msg, offset, length);
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+18];
String computerName = new String(msg, offset, length);
}
}
How to set a datasource in tomcat
It seems a simple thing:
create a context.xml in the PUBLIC-HTML/META-INF folder of the web app.
The content of the context.xml must be something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/webAppName" docBase="webAppName">
<Resource name="jdbc/DataSourceName" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@dataBase:1521:ORCL"
username="user" password="pwd" maxActive="20" maxIdle="10"
maxWait="-1"/>
</Context>
create a context.xml in the PUBLIC-HTML/META-INF folder of the web app.
The content of the context.xml must be something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/webAppName" docBase="webAppName">
<Resource name="jdbc/DataSourceName" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@dataBase:1521:ORCL"
username="user" password="pwd" maxActive="20" maxIdle="10"
maxWait="-1"/>
</Context>
Iscriviti a:
Post (Atom)