Skip to main content

JSF 2.0 Custom Tag Library

3 replies [Last post]
josealvarezdelara
Offline
Joined: 2008-12-26
Points: 0

Hello
I am developing a workflow that download and upload files from the FileSystem to an Oracle database. Based on Jim Driscoll's blog and the and BalusC's blog at
http://balusc.blogspot.com/2009/12/uploading-files-with-jsf-20-and-servl...
I am developing a needed custom tag HtmlInputFile. Here it is,
<code>
import javax.faces.component.FacesComponent;
import javax.faces.component.html.HtmlInputText;
/**
* Faces component for <code>input type="file"</code> field.
*
* @author BalusC
* @link http://balusc.blogspot.com/2009/12/uploading-files-with-jsf-20-and-servl...
*/
@FacesComponent(value = "HtmlInputFile")
public class HtmlInputFile extends HtmlInputText {
// Getters ------------------------------------------------------------------------------------
@Override
public String getRendererType() {
return "javax.faces.File";
}
}
</code>
This class extends HtmlInputText so the idea is that it heritage all the properties and attributes.
Now the tag library,
<code>
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 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-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://balusc.net/jsf/html</namespace>
<tag>
<tag-name>inputFile</tag-name>
<component>
<component-type>HtmlInputFile</component-type>
</component>
</tag>
</facelet-taglib>
</code>
and the web.xml,
<code>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/balusc.taglib.xml</param-value>
</context-param>
</code>
Finally the xhtml,
<hh:inputFile id="file" value="#{tomaRequisitosManagedBean.file}" />
Well, the xhtml file does not recognize the atributte 'value' and I do not know why.
Any idea about it?
Thanks in advance,
Jose

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
healeyb
Offline
Joined: 2010-01-26
Points: 0

Do you mean that your IDE doesn't recognise the attribute? If so add an <attribute> with the name
"value" to the taglib. When you say that you're developing it, it's already sort of developed isn't it?

Regards,
Brendan.

josealvarezdelara
Offline
Joined: 2008-12-26
Points: 0

Hi Brendan,
Sorry for my late reply. I did what you say but take a look to the last email I have received from this forum,
According to the HTML 4 Recommendation paragraphs 17.13.2 and 17.13.3, only
"successful controls" may be submitted as part of a form submission. Among
other conditions, successful controls must have a control name.

The W3C Working draft for HTML 5 has a similar condition expressed in
paragraph 4.11.2.3 Form Submission Algorithm, step 7, substep 1, condition
".... either the field element does not have a name attribute specified, or
its name attribute's value is the empty string."

The AJAX submission code included in Mojarra does not verify that submitted
controls have a name. The result is parameters without a name, which the
server cannot handle anyway. (for instance, "=ABC&=DEF").

*Patch in file jsf-uncompressed.js, line 2121 :*
*if (!el.disabled && el.name != '') { *

So I am afraid the issue has no solution.

Thanks a lot,
Jose

josealvarezdelara
Offline
Joined: 2008-12-26
Points: 0

Hi,
What I pretend is to convert a faces HtmlInputText in an html input type file.

This is the class that does it that,

<code>
@FacesComponent(value = "HtmlInputFile")
public class HtmlInputFile extends HtmlInputText {

// Getters ------------------------------------------------------------------------------------

@Override
public String getRendererType() {
return "javax.faces.File";
}

}
</code>

First I have defined a tag library,

<code>
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 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-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://balusc.net/jsf/html</namespace>
<tag>
<tag-name>inputFile</tag-name>
<component>
<component-type>HtmlInputFile</component-type>
</component>
</tag>
</facelet-taglib>
</code>

And over this I have created a composite component because it is the only way I can define a value attribute,

<code>
<?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:bc="http://balusc.net/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Esto no sale</title>
</h:head>
<h:body>
<!-- INTERFACE -->
<composite:interface>
<composite:attribute name="value" type="bean.Fichero" required="true" />
</composite:interface>

<!-- IMPLEMENTATION -->
<composite:implementation>
<bc:inputFile id="fileID" />
</composite:implementation>
</h:body>
</html>
</code>

It was created in a folder 'resources/ezcomp/inputFile.xhtml' of the project
so I call the composite component from the Jsf page as follows,

<code>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ez="http://java.sun.com/jsf/composite/ezcomp"
xmlns:p="http://primefaces.prime.com.tr/ui"
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="./css/default.css" rel="stylesheet" type="text/css" />
<title>Toma de Requisitos</title>
<p:resources />
</h:head>
<h:body>
<h:form id="formId" enctype="multipart/form-data">

<!- ****************************************************************** ->
<h:outputLabel for="file">File:</h:outputLabel>
<ez:inputFile id="file" value="#{fichero.path}" />
<h:commandButton value="Submit" action="#{tomaRequisitosManagedBean.submit}">
<f:attribute name="#{tomaRequisitosManagedBean.filePath}" value="#{fichero.path}" />
</h:commandButton>
<h:outputText value="File #{tomaRequisitosManagedBean.file.name} successfully uploaded!"
rendered="#{not empty tomaRequisitosManagedBean.file}" />
<!- ****************************************************************** ->

<h:messages />
</h:form>
</h:body>
</html>
</code>

Where the class Fichero is a session scope managed bean,

<code>
@ManagedBean
@SessionScoped
public class Fichero {

private String path;

/**
* @return the path
*/
public String getPath() {
System.out.println("Desde get: " + path);
return path;
}

/**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
System.out.println("Desde set: " + path);
}

}
</code>

Well, the issue is that I cannot get and neither set the 'path' property that I want to pass
to my managed bean TomaRequisitosManagedBean that is the bean that do all the
process.

Do you see the problem? What am I doing wrong?

Any help will be appreciated.

Thanking in advance,
Jose