Disclaimer:
These pages about different languages / apis / best practices were mostly jotted down quckily and rarely corrected afterwards.
The languages / apis / best practices may have changed over time (e.g. the facebook api being a prime example), so what was documented as a good way to do something at the time might be outdated when you read it (some pages here are over 15 years old).
Just as a reminder.

Java Server Faces Developer Notes

This is some programming notes I did when starting off with JSF in 2007

Also see My Facelets notes



Notes regarding Java Server Faces

There are a few work arounds one has to use, for example passing arguments to commands etc. These are my notes for stuff like that I have found out about Java Server Faces.

IDE

http://www.eclipse.org/webtools/
after eclipse has been started, an update of the tools must be done to receive the JSF plugin
Note that there are two menu items to modify for proxy settings in ecplise (in case internet is accessed through proxy)

Tell Tomcat to always check for local changes

I am using also Facelets for JSF, and there is an important context parameter in web.xml I can to set in order to be able to modify my XHTML files with an automatic refresh (i.e. I don't need to restart Tomcat to see my changes). So, in my web.xml, I have this:

<context-param>
       
<param-name>facelets.REFRESH_PERIOD</param-name>
       
<param-value>1</param-value>
</context-param>


Note, when in production you should set the param value to "-1" so that it isnt constnatly verify if the xhtml has changed (since it never changed anyway in production)

Eclipse JSF plugin

In the file .settings/org.eclipse.wst.common.component, there exist the dependencies regarding which .jars that should be included in the .war file.
I.e. it seems to be easier editing this file, instead of going over the annoying JSF "add library" interface. That way the project should be much easier to compile/deploy for someone that checks it out fresh from CVS.

General

All HTML should be within <f:verbatim> tags. (Works within form etc anyway, but to have a nice code it should be within verbatim)

How to include $Id: $ in html output on the page

<h:outputText
   escape="false"
   value="&lt;!-- $Id: somefile.xhtml,v 1.16 2007-03-20 12:44:07 dilbert Exp $ --&gt;" />

Viewhandler

A way to decouple html from the JSF functionality to have full control over the web design. The JSP would not be involved in that case. Facelets is based on a custom ViewHandler.
http://www.onjava.com/pub/a/onjava/2004/06/09/jsf.html

http://www.jroller.com/page/cenkcivici?entry=custom_jsf_view_handler

JSF 1.1 vs JSF 1.2

JSF 1.2 reqiures JSP 2.1 hence JSF 1.1 can be executed on Tomcat 5.5 whereas JSF 1.2 can only be executed on Tomcat 6.0
To get JSF 1.2 up and running:
1. Using the JSF 1.2 [1], copy the jsf-api.jar and jsf-impl.jar to
TOMCAT_HOME/lib (just to make it easy). No need to install any
commons libraries, as the necessary dependencies are bundled
in jsf-impl.jar (under the namespace com.sun.org.apache....).
2. downloaded JSTL 1.2 [2] and install it in TOMCAT_HOME/lib
as well.
3. start tomcat
4. deploy jsf-guessNumber

Use the above, guessNumber worked like a charm without
having to add any parameters in order to get JSF to work.
[1] https://javaserverfaces.dev.java.net (1.2_02 or the latest RC should be fine)
[2] https://maven-repository.dev.java.net/nonav/repository/jstl/jars

Decoupling the controller from the model - MVC

<managed-bean>
        <description>
            Bean to change the password, contains only properties
        </description>
        <managed-bean-name>passwordBean_Backing</managed-bean-name>
        <managed-bean-class>
            com.example.web.backing.Password
        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

    <managed-bean>
        <description>
            The Controller method to change the password
        </description>
        <managed-bean-name>passwordHandler_Backing</managed-bean-name>
        <managed-bean-class>
            com.example.web.backing.PasswordHandler
        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>password</property-name>
            <value>#{passwordBean_Backing}</value>
        </managed-property>
    </managed-bean>


public class PasswordHandler {
    private Password password;

    public void setPassword(Password p) {
        this.password = p;
    }
}

http://www.oreilly.com/catalog/jsvrfaces/chapter/ch02.pdf

Phaselistener

See also about Weblets further down, weblets seems like a better idea to use.
To be able to package together javascript and images in one jar, a phaselistener must be implemented.
Phaselistener for loading resources:
http://www.jroller.com/page/mert?entry=js_css_img_loading_resources

http://www.nabble.com/JSF-Authentication-t4235729.html

Phaselistener - redirect if user is not logged in

in faces-config.xml
<lifecycle>
        <phase-listener>
            com.example.utils.LoginPhaseListener
        </phase-listener>
    </lifecycle>

public class LoginPhaseListener implements PhaseListener {
    private static final long serialVersionUID = 1L;

    /** Logger */
    private static final Log log = LogFactory.getLog(LoginPhaseListener.class);

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    public void afterPhase(PhaseEvent phaseEvent) {
        log.debug("AfterPhase working");
        FacesContext fc = phaseEvent.getFacesContext();

        //determine if the user is logged in or not
        boolean loggedIn=false;
       
        //FacesContext facesContext = FacesContext.getCurrentInstance();
        ValueBinding valueBinding = fc.getApplication().createValueBinding("#{loginHandler_backing}");
        LoginHandler  loginHandlerBB= (LoginHandler) valueBinding.getValue(fc);
       
        if (loginHandlerBB == null) {
            loginHandlerBB = new LoginHandler();
            fc.getApplication().createValueBinding("#{loginHandler}").setValue(fc, loginHandlerBB);
            loggedIn = false;
        } else {
            loggedIn = loginHandlerBB.isLoggedIn();
        }

        //if not logged in, we must check the page the user is at
        boolean loginPage = false;
        if(! loggedIn){
            List<String> validPages = new ArrayList<String>();
            validPages.add("start");
            validPages.add("alarm");
            validPages.add("keepsessionalive");
           
            for (String p: validPages){
                if(! loginPage){
                    loginPage = fc.getViewRoot().getViewId().lastIndexOf(p) > -1 ? true
                        : false;
                }
            }
        }
       
        log.debug("loginPage="+loginPage+", loggedIn="+loggedIn);
       
        if (!loginPage && !loggedIn) {
            log.debug("User is not logged - redirecting to login page insteaf of " + fc.getViewRoot().getViewId());

            //save the page to redirect to           
            loginHandlerBB.setRedirect(fc.getViewRoot().getViewId().replaceFirst(".xhtml", ".jsf") + requestParameters(fc) );
           
            NavigationHandler nh = fc.getApplication().getNavigationHandler();
            nh.handleNavigation(fc, null, "navigation_start");
        } else {
            log.debug("User is logged in, or the user is looking at the login page - go to page "
                    + fc.getViewRoot().getViewId());
        }
        log.debug(fc);
    }

    public void beforePhase(PhaseEvent phaseEvent) {
    }

    /**
     * parse any request paramerts so they can be appended in the redirect url
     * @param fc
     * @return empty string if no parameters
     */
    @SuppressWarnings("unchecked")
    private String requestParameters(FacesContext fc){
        //add parameters
        Map<String, String> paramMap =
            fc.getExternalContext().getRequestParameterMap();
        List<String> params = new ArrayList<String>();
        for (String paramKey : paramMap.keySet()) {
            UIComponent component = fc.getViewRoot().findComponent(paramKey);
            if (component == null){
                String paramname = paramKey.substring(paramKey.lastIndexOf(':') + 1);
                String paramvalue = paramMap.get(paramKey);
                params.add(paramname + "=" + paramvalue);
            }
        }
        String tmpUrl="";;
        for (int i = 0; i < params.size(); i++) {
            tmpUrl += (i == 0 ? "?" : "&") + params.get(i);
        }
        return tmpUrl;
    }
}

Weblets

To be able to provide resources from a jar to the web front end.
in the JAR file with the resources, in the META-INF directory a file weblets-config.xml must be added.
weblets-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<weblets-config xmlns="http://weblets.dev.java.net/config">
    <weblet>
        <weblet-name>com.uicomponents</weblet-name>
        <weblet-class>
            net.java.dev.weblets.packaged.PackagedWeblet
        </weblet-class>
        <init-param>
            <param-name>package</param-name>
            <param-value>web.img</param-value>
        </init-param>
    </weblet>
    <weblet-mapping>
        <weblet-name>com.uicomponents</weblet-name>
        <url-pattern>/test/*</url-pattern>
    </weblet-mapping>
</weblets-config>


In the WAR, the weblets jars must be added to WEB-INF/lib, and in web.xml the following must be entered
<servlet>
        <servlet-name>Weblets Servlet</servlet-name>
        <servlet-class>
            net.java.dev.weblets.WebletsServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Weblets Servlet</servlet-name>
        <url-pattern>/weblets/*</url-pattern>
    </servlet-mapping>

The resources can now be accessed through
http://localhost:8080/<appname>/weblets/test/someimage.jpg

https://weblets.dev.java.net/
http://java.sys-con.com/read/159161.htm

Including files within other files (e.g. navigation/menus/etc)

For static content (pure html etc), static include can be used, that way there is no need for the f:verbatim tag (on the other hand: if the whole file is static, you only need to add f:verbatim tags at the start and end of the file)
 
For dynamic content this technique is used. To make the menu global, the from-view-id should be set to a wild card in the xml for the navigation.
Since the from-outcome will be global, it makes sense to add prefix (e.g. "menu_") to avoid name clashes with other from-outcome names
<navigation-rule>
        <display-name>left hand navigation menu</display-name>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <from-outcome>menu_status</from-outcome>
            <to-view-id>/confirm.jsp</to-view-id>
            <redirect />
        </navigation-case>
        <navigation-case>
            <from-outcome>menu_login</from-outcome>
            <to-view-id>/login.jsp</to-view-id>
            <redirect />
        </navigation-case>
        <navigation-case>
            <from-outcome>menu_page2</from-outcome>
            <to-view-id>/page2.jsp</to-view-id>
            <redirect />
        </navigation-case>
</navigation-rule>

1. Always make sure that the entire included page is wrapped in f:subview tags (only for JSF 1.1)
<f:subview id="myNestedPage">
  <jsp:include page="menu.jsp" />
</f:subview>.

2. Do NOT include another h:form tag on your included page.
(unless the first page do not have a h:form) h:form should never be nested

3. ALL plain text, HTML, or non-JSF tags on the included page MUST be wrapped in f:verbatim tags.
menu.jsp looks like:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<h:form>
<f:verbatim><table bgcolor="#ffff00"><tr><td><b>Included menu</b></f:verbatim>

<f:verbatim><p/></f:verbatim>

<h:commandLink action="menu_login">
  <h:outputText value="Login" />
</h:commandLink>

<f:verbatim> <p/></f:verbatim>

<h:commandLink action="menu_page2">
  <h:outputText value="Page2" />
</h:commandLink>

<f:verbatim> <p/></f:verbatim>

<h:commandLink action="menu_status">
  <h:outputText value="Status" />
</h:commandLink>

<f:verbatim> </td></tr></table></f:verbatim>

</h:form>

Dynamic include modules in JSF

E.g. based on some parameter in the URL different modules are included
http://forum.java.sun.com/thread.jspa?threadID=584037&messageID=3058462
http://forum.java.sun.com/thread.jspa?threadID=564963&tstart=105

Passing parameter in URL

example.faces?parameter=apa

the parameter variable can be reached with
#{param.parameter}

Note: This probably clears the session data(?)

Calling methods with parameters (via GET/POST)

in the backing bean:

public void someMethod(){
String nameOfParameterToRead="myParameter";
FacesContext context = FacesContext.getCurrentInstance();
String tmpData= (String) context.getExternalContext().getRequestParameterMap().get(nameOfParameterToRead);
System.out.println("...reading out parameters: "+ nameOfParameterToRead+"="+tmpData);
}

in the JSF page:

<h:form>	
<a4j:commandLink reRender="someData" action="#{myBean_Backing.someMethod}" value="Link">
<f:param name="myParameter" value="444"/>
</a4j:commandLink>
</h:form>

see also similar attribute for ajax4jsf

http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters

discussion:
http://forum.java.sun.com/thread.jspa?threadID=737472&messageID=4414296


http://www.jsftutorials.net/param.html


http://balusc.xs4all.nl/srv/dev-jep-com.html


NOTE: param does NOT work within commandButton when Facelets is used. See this thread:

http://www.nabble.com/f%3Aparam-and-commandButton-t1517965.html#a4284131

The recommended t:updateActionListener (for CommandButton) does NOT work with facelets, but instead one can use
                    <f:setPropertyActionListener
                       target="#{userManagementCustomer_Backing.id}" value="3123123123123"/>
http://justfiveminutes.wordpress.com/2006/07/12/passing-parameters-to-links-in-facelets/

Commandbutton with params

best way seem to use hidden variables to store the values in

                            <input type="hidden" name="returntopage"
                                value="#{param.returntopage}" />
                            <input type="hidden" name="moid"
                                value="#{param.moid}" />
                            <input type="hidden" name="ctxt"
                                value="#{param.ctxt}" />

and the use h:commandButton just as normal, reading out the values from requestmap just as nromal.

otherwise one can use a4j:commandButton:

<script>           
            function reloadAndClose(){
                opener.location.href='java-server-faces-faq.jsf';
                self.close();
            }
            </script>   
            <a4j:form>                           
                <a4j:commandButton action="#{productedit_Backing.activateProduct}" oncomplete="reloadAndClose();"   
                    styleClass="submit"
                    value="#{action}">
                    <f:param  name="activate" value="#{param.activate}"/>
                    <f:param  name="itemId" value="#{param.id}"/>
                </a4j:commandButton>
           
                <input type="submit" value="abbrechen" class="submitgray" onclick="self.close();"/>
            </a4j:form>

Calling methods with parameters without GET/POST/ajax4jsf

Works with binding of component variable in backing bean and use of attribute

<h:selectBooleanCheckbox 
         value="#{searchHandler_Backing.mediaType}" binding="#{searchHandler_Backing.bindingMediaTypeCheckBox}"  >
            <f:attribute name="argument" value="${argument}"/>

UIComponent bindingMediaTypeCheckBox;
public UIComponent getBindingMediaTypeCheckBox() {
        return bindingMediaTypeCheckBox;
    }

    public void setBindingMediaTypeCheckBox(UIComponent bindingMediaTypeCheckBox) {
        this.bindingMediaTypeCheckBox = bindingMediaTypeCheckBox;
    }

public boolean getMediaType() {
        String parameterArgument = "argument";
        String argument = (String) this.bindingMediaTypeCheckBox.getAttributes().get("argument") ;
        System.out.println(" getting mediatype....read out mediaType="
                + argument);
}
public void setMediaType(boolean b) {
        String parameterArgument = "argument";
        String argument = (String) this.bindingMediaTypeCheckBox.getAttributes().get("argument") ;
        System.out.println(" setting mediatype...read out mediaType="
                + argument);
}


See also discussion:
http://forum.java.sun.com/thread.jspa?tstart=0&forumID=427&threadID=563036&trange=15

Localization

To override the language delivered by the browser, one can call setLocale on UIViewRoot
FacesContext con = FacesContext.getCurrentInstance();
con.getViewRoot()
.setLocale( new Locale( this.getChoosenLocale() ) );




Within the f:view tag:
<f:loadBundle basename="com.whatever.bundles.Resources" var="bundle" />

The resources should now be calles Resources.properties, Resources_de.properties, etc and should be placed in a package com.whatever.bundles
If the resource is not specific to some package (e.g. used only by some jsp), it makes sense to place the resources in a specific package.
Should be named something reasonable so it is clear where the resources are used, com.whatever.bundles.jsp or something.

In faces-config.xml the supported languages must be defined
    <application>
        <locale-config>
            <default-locale>de</default-locale>
            <supported-locale>de</supported-locale>
            <supported-locale>nl</supported-locale>
        </locale-config>
    </application>


To display a value from the resources, example:
<h:outputText value="#{bundle.title}" />
would display a localized title in the language that matched the users browser.


Changing language dynamically from user interface
http://richardbrenner.com/joomla/index.php?option=com_content&task=view&id=56&Itemid=

Custom h:message

http://www.oracle.com/technology/pub/articles/masterj2ee/j2ee_wk7.html
http://www.oracle.com/webapps/online-help/jdeveloper/10.1.3/state/content/navId.4/navSetId._/vtTopicFile.jsf_apps%7Ceventvalidate%7Csf_amg_messagesoverride~html/

Session

How to terminate the session?

In order to terminate the session you can use session invalidate method.
This is an example how to terminate the session from the action method of a backing bean:

 

public String logout() {

  FacesContext fc = FacesContext.getCurrentInstance();
  HttpSession session = (HttpSessionfc.getExternalContext().getSession(false);
  session.invalidate();
  return "login_page";
}

Validation

Note that if MyFaces Tomahawk 1.0.5 or higher is used, there exist the tag t:validateEqual that performs the same validation as below.
Validating several form fields together (if Tomahawk is not used), e.g. comparing two password fields:
private String password = null;
private String verifiedPassword = null;
private UIInput uiPassword = null;
private UIInput uiVerifiedPassword = null;

public void verifyPassword2(FacesContext context,
            UIComponent componentToValidate, Object value)
    throws ValidatorException {
       
        String uiP = uiPassword.getLocalValue().toString();
        String uiVP = uiVerifiedPassword.getLocalValue().toString();
        String errMsg = "Validation failed: ";

        if(uiPassword == null || uiVerifiedPassword ==null || uiP.equals("") || uiVP.equals("")){
            errMsg = errMsg + "both fields for the new password must be filled";
            this.displayError(errMsg);
        }

        if (!uiP.equals(uiVP)) {
            errMsg = errMsg + "new password does not match verified password";
            this.displayError(errMsg);
        }

        // validation ok
        return;
    }

New password:
<h:inputSecret value="#{passwordBean_Backing.password}"
                    binding="#{ passwordBean_Backing.uiPassword}">
                </h:inputSecret>

Re-enter new password:
<h:inputSecret id="verifiedPassword"
                    value="#{passwordBean_Backing.verifiedPassword}"
                    binding="#{ passwordBean_Backing.uiVerifiedPassword}">
                </h:inputSecret>
<h:inputHidden id="validationId" value="dummy"
            validator="#{passwordBean_Backing.verifyPassword2}" />
        <font color="red"> <h:message for="validationId" /> </font>
<h:inputHidden id="validationId" value="dummy"
            validator="#{passwordBean_Backing.verifyPassword2}" />
        <font color="red"> <h:message for="validationId" /> </font>

The chapter on validation describes how to validate several fields together:
http://www.horstmann.com/corejsf/

How to validate several fields at once, e.g. verify that a fromDate is before a toDate
http://weblogs.java.net/blog/johnreynolds/archive/2004/07/improve_jsf_by_1.html
http://forum.java.sun.com/thread.jspa?forumID=427&start=0&threadID=534262&range=15
http://forum.java.sun.com/thread.jspa?tstart=0&forumID=427&threadID=500322&trange=15
http://forum.java.sun.com/thread.jspa?forumID=427&threadID=510887

Outputting text without html escape

set escape="false"
e.g. dumping text without translating < to &lt;

<h:outputText value="some text" escape="false"/>

Sending PDF as stream (i.e. save-as)

bais = (ByteArrayInputStream) reportClientDocument.getPrintOutputController().export(Repor tExportFormat.PDF);
byte[] bytes = new byte[bais.available()];
bais.read(bytes, 0, bais.available());
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
response.setHeader( "Content-disposition", "inline; filename=report.pdf");
response.getOutputStream().write(bytes);
faces.responseComplete();

Splitting faces-config.xml

Place all navigation in a file called navigation.xml, note that the dtd and <faces-config> tag must be there:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
    <navigation-rule>
        <display-name>login</display-name>
        <from-view-id>/login.jsp</from-view-id>
        <navigation-case>
            <from-outcome>whatever</from-outcome>
            <to-view-id>/page2.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>login</from-outcome>
            <to-view-id>/confirm.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <display-name>page2</display-name>
        <from-view-id>/page2.jsp</from-view-id>
        <navigation-case>
            <from-outcome>seeresult</from-outcome>
            <to-view-id>/confirm.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>login</from-outcome>
            <to-view-id>/login.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
        <display-name>left hand navigation menu</display-name>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <from-outcome>menu_status</from-outcome>
            <to-view-id>/confirm.jsp</to-view-id>
            <redirect />
        </navigation-case>

        <navigation-case>
            <from-outcome>menu_login</from-outcome>
            <to-view-id>/login.jsp</to-view-id>
            <redirect />
        </navigation-case>

        <navigation-case>
            <from-outcome>menu_page2</from-outcome>
            <to-view-id>/page2.jsp</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>
</faces-config>

Analogue for faces-managed-beans.xml

In web.xml, the following line must be added:
<context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>
        /WEB-INF/faces-config.xml,/WEB-INF/navigation.xml,/WEB-INF/faces-managed-beans.xml
        </param-value>
</context-param>

AJAX

RichFaces (jboss) contains ajax4jsf and is most popular/stable ajax/component framework.
https://mabon.dev.java.net/

Google Toolkit integrated with ajax4jsf
http://www.theserverside.com/tt/articles/article.tss?l=GWTandJSF

More frameworks:
http://ajaxpatterns.org/Java_Ajax_Frameworks

German paper comparing different frameworks (from December 2006)
http://www.swisstech.net/jsfajax/jsf_ajax_frameworks.pdf

AJAX4JSF - To transfer data from a field immediately to the server (without clicking submit)
<h:inputText id="name" value="#{wizBean.name}" required="true">
    <a4j:support event="onkeydown"/>
</h:inputText>

To transfer the value to some other field, add the attribute reRender="theNameAjax" where theNameAjax is the name of the other form field that should be updated with the value.

The commands MUST be within a form tag, otherwise it will not work:
    <h:form>
    <div id="asd">Module1 - <a4j:commandLink reRender="module1"
        action="#{module1Bean.changeValue}" value="do something" />
    <p />
    <font color="blue"> <h:outputText id="module1"
        value="#{module1Bean.name}" /> </font>
    </div>
    </h:form>


Bugs in the ajax4jsf framework?

Including other files causes ajax popup error sometimes
Very strange, when including an independent file, as the beanstatus.jsp above, I sometimes get a javascript alert box with a 500 error when triggering the ajax actions (even though the file that I include has nothing to do with ajax).
This seems to be a bug in MyFaces:
http://forum.java.sun.com/thread.jspa?threadID=782821&tstart=90
"Ok. It is the same very known bug in the MyFaces 1.1.2 and 1.1.3 that appears even without Ajax4jsf.
http://issues.apache.org/jira/browse/MYFACES-1278
http://issues.apache.org/jira/browse/MYFACES-1296
People recommend to upgrade to MyFaces 1.1.5 code base to avoid it."

Ajax request not sent to server
If a field is required before the field you added ajax support to, the ajax request will never be sent to the server. (tested in Firefox 2.0 on Suse Linux)
Example:
Name: <h:inputText id="name" value="#{wizBean.name}" required="true"></h:inputText>
<h:message for="name" />
Password:
<h:inputSecret value="#{wizBean.password}">
  <a4j:support event="onkeyup" reRender="thePwdAjax" />
</h:inputSecret>
<p />
The password, fetched from server via AJAX <br />
<font color="red"> <h:outputText id="thePwdAjax" value="#{wizBean.password}" /> </font>

In the example above, the field "thePwdAjax" will only be updated with the password if something has been entered into the field "name", otherwise no ajax request is sent.

Should look for a workaround for this, maybe it is just a matter of defining correct attributes for the a4j:support.

MyFaces

MyFaces Tomahawk provides many user interface controls, and seems reasonable to use as default.
The myfaces.apache.org does not contain that much useful information, look at the wiki instead: http://wiki.apache.org/myfaces/FrontPage

Tomahawk

Note: within myfaces-all.1.1.1.jar the extensions (Tomahawk) is included.
However, the instructions on the MyFaces pages on how to configure your web.xml does only apply to the newer versions of Tomahawk.
To configure Tomahawk (not clear which version that is included, but it is lower than 1.0.5) that are included in myfaces-all.1.1.1.jar, the following class must be changed in in the web.xml.
replace:
<filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>
            org.apache.myfaces.webapp.filter.ExtensionsFilter
        </filter-class>
        <init-param>
            <param-name>maxFileSize</param-name>
            <param-value>20m</param-value>
        </init-param>
</filter>

with:
<filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>
            org.apache.myfaces.component.html.util.ExtensionsFilter
        </filter-class>
        <init-param>
            <param-name>maxFileSize</param-name>
            <param-value>20m</param-value>
        </init-param>
</filter>

See: http://www.mail-archive.com/users@myfaces.apache.org/msg28183.html

Tomahawk - Tree2 component

http://www.nabble.com/How-to-use-TreeNodeChecked--t2724371.html
http://www.jroller.com/page/plainoldweblog?entry=use_tomahawk_tree2_and_ajax4jsf

Shale

"Shale is a modern web application framework, fundamentally based on JavaServer Faces. Architecturally, Shale is a set of loosely coupled services that can be combined as needed to meet particular application requirements. Shale provides additional functionality such as application event callbacks, dialogs with conversation-scoped state, a view technology called Clay, annotation-based functionality to reduce configuration requirements and support for remoting. Shale also provides integration links for other frameworks, to ease development when combinations of technologies are required."
http://shale.apache.org/

Disable cache

See
http://turbomanage.wordpress.com/2006/08/08/disable-browser-caching-in-jsf/
http://www.it-eye.nl/weblog/2006/11/20/adf-faces-and-cache-control/

Lifecycle

http://balusc.xs4all.nl/srv/dev-jep-djl.html

Converters

How to read value from a backing bean from a converter:

    try {
                   ELContext elContext = ctx.getELContext();              
                   ValueExpression ve = ctx.getApplication().getExpressionFactory().createValueExpression(elContext, "#{mediaObjectPm.pmBean.languageId}", String.class);
                   String lang = (String) ve.getValue(elContext);              
                   if(StringUtils.isNotEmpty(lang)){
                       paragraph = "<p lang=\""+lang+"\" class=\"lang-"+lang+"\" />";
                   }
               } catch(Exception e){
                   log.warn("Exception",e);
               }
              

Directory structure

An example how the files could be arranged.
The name of the entries in the navigation.xml would have a prefix of <subdir>_<filename>_ to avoid name clashes (since the names are all global). E.g. a link within the search/navigation.jsp module could for example be "search_navigation_listall"
src/
    com/
        example/
            projectname/         
                ui/
                    common/
                        userbean.java
                        navigationbean.java                     
                    search/       
                        searchbean.java
web/
    META-INF/
    WEB-INF/
        web.xml
        faces-config.xml
        faces-managed-beans.xml
        navigation.xml
        classes/
          <here goes all compiled sources>
       lib/
          <here goes all libs that will not be installed directly on the app server>
    resources/
        css/
           common/
               general.css       
        images/
           navigation/
               someimage.jpg
               otherimage.gif
        js/
            common/
                general.js
            navigation/
                somescript.js
        etc/
           info/
               somestats.xls
    common/
        topnavigation.jsp
        usernavigation.jsp
        searchbox.jsp
        footer.jsp
    search/
        navigation.jsp
        allresult.jsp
        oneentry.jsp
        menu.jsp
        relatedsearchterms.jsp
    modules/
        lastsearches.jsp
        newsletter.jsp
        messages.jsp
        dpa_recommends.jsp
        24hnews.jsp  

f:verbatim

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=82&t=001136
The f:verbatim content is transient. It means it is not stored in the component tree. Yes, the components will be rendered, but not do not stay in the component tree and will not be in the component tree when request comes to the server and the tree is restored on the first phase of the JSF lifecycle.

So, you have no chance to have jsf components working inside the f:verbatim.

What you cant to do:
* use f:verbatim about the non-jsf content only. Yep. The page code will not look nasted well, but the result html will be fine

* do not use pure html tag, but only the JSF tags. I know it is hard.

* use html layout tag library ( http://www.jsftutorials.net/htmLib/index.html )

* use facelets. ( https://facelets.dev.java.net/ ) . It work with html tags without having verbatim

* use JSF 1.2

http://forum.java.sun.com/thread.jspa?threadID=5119738&messageID=9416967

Conditional statements

Generate html code in backing bean (i.e. put the condition in the backing bean)
http://forum.java.sun.com/thread.jspa?threadID=702883&messageID=4075853

How to change the UI

http://jsftutorials.net/htmLib/

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=82&t=001190

Use coccon to generate xml and use xslt?
http://www.developer.com/java/other/article.php/10936_3294951_2

Custom HTML components (for use with AJAX etc)
UI, LI:
http://thepeninsulasedge.com/blog/2006/08/28/jsf-html-div-and-ul-tags/
DIV:
http://www.it-eye.nl/weblog/2006/04/25/creating-a-jsf-div-component/

h:Datatable

How to display the index of each row
http://forum.java.sun.com/thread.jspa?forumID=427&threadID=549084

Creating components that render for other devices (wap etc)

http://www.jsfcentral.com/articles/unwired_components-2.html

Useful links

http://jsfcentral.com/
http://www.jsftutorials.net/

JSF, Facelts, Spring and hibernate:
http://www.thearcmind.com/confluence/dashboard.action

c:forEach vs ui:repeat and other comparisions of c:, h:ui: tags
http://www.ninthavenue.com.au/blog/c:foreach-vs-ui:repeat-in-facelets

Forum
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=forum&f=82
http://forum.java.sun.com/forum.jspa?forumID=427
http://tech.groups.yahoo.com/group/jsf-developers/

Facelets

See the separate document for info about facelets.

Tutorials

http://www.eclipse.org/webtools/jsf/dev_resource/JSFTutorial/JSFTools_milestone1_tutorial.html
http://www.coreservlets.com/JSF-Tutorial/
Source code for examples from the JSF reference book:
http://www.jsfcompref.com/
JSF tags guide
http://www.exadel.com/tutorial/jsf/jsftags-guide.html

Criticism of JSF

http://www.oreillynet.com/onjava/blog/2007/01/jsf_in_2007_1.html
http://www.theserverside.com/news/thread.tss?thread_id=43705
http://icoloma.blogspot.com/2006/10/myfaces-emperor-has-no-clothes.html
http://www.michaelyuan.com/blog/2006/10/23/is-jsf-really-that-bad/
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=82&t=002078
http://www.theserverside.com/blogs/thread.tss?thread_id=39129
http://sfjsf.blogspot.com/2006/02/usability-problems-in-jsf.html

How to create relation between backing beans

<managed-bean>
  <description>
     Bean for modifying passwords, only contains properties
  </description>
   <managed-bean-name>passwordBean_Backing</managed-bean-name>
   <managed-bean-class>
      com.example.web.backing.Password
    </managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<description>
 Controller methods to modify password
</description>
<managed-bean-name>passwordHandler_Backing</managed-bean-name>
<managed-bean-class>
com.example.web.backing.PasswordHandler
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>password</property-name>
<value>#{passwordBean_Backing}</value>
</managed-property>
</managed-bean>

public class PasswordHandler {
private Password password;

public void setPassword(Password p) {
this.password = p;
}

Redirect to another page within JSF code

BackingBeanHelper.doRedirect:
    /**
     * Redirect to the desired URL. wag: erstmal nur ein test wie man ein redirect macht will ich dann als tag
     * implementieren siehe
     * http://forum.java.sun.com/thread.jspa?threadID=530461&messageID=4014422
     *
     * @param url page to redirect to
     */
public static void doRedirect(String url) {
        //TODO make sure this is an absolute URL
        final FacesContext facesContext = FacesContext.getCurrentInstance();
        final ExternalContext externalContext = facesContext
                .getExternalContext();
        try {
            externalContext.redirect(externalContext.getRequestContextPath()
                    + url);
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
    }

in the backing bean
    public String getRedirectSelectProfile() {
        BackingBeanHelper.doRedirect("/java_server_faces_faq_page.jsf");
        return "";
    }

in the jsf code
            <!-- redirect to start_profile.xhtml -->
            <h:outputText value="#{userSettings_backing.redirectSelectProfile}"/>

OR
<h:form>
            <h:inputHidden
                value="#{userSettings_backing.redirectSelectProfile}" />
        </h:form>
I do not know if either one of them got an advantge over the other one

Since both above seem to be evaluated before the ui:fragment rendered condition is evaluated, i settled for
            <script>
                location.href="java_server_faces_faq_page.jsf";
            </script>


More info on redirects
http://forum.java.sun.com/thread.jspa?threadID=530461&messageID=4014422

UI:repeat

contains a bug (or actually, the jdk until java 1.6 contains some bug that ui:repeat suffers from), the effect is that sometimes not all entries is displayed.
Hence, ui:repeat should never be used, instead t:dataList should be used (has the same arguments etc)
t:dataList needs some jars though:
standard-1.0.2.jar, jstl-1.0.2.jar

Radio buttons

to be able to place the buttons anywhere you want, use the tomahawk:

<h:panelGrid columns="1" >
<%/* t:selectOneRadio is an extension of h:selectOneRadio
if layout="spread" selectitems wont get rendered
instead t:radio components are getting rendered
*/%>
<t:selectOneRadio id="discount2" value="#{carconf.discount2}" layout="spread" styleClass="selectOneRadio">
<f:selectItem itemValue="0" itemLabel="#{example_messages['discount_2_0']}" />
<f:selectItem itemValue="1" itemLabel="#{example_messages['discount_2_1']}" />
<f:selectItem itemValue="2" itemLabel="#{example_messages['discount_2_2']}" />
</t:selectOneRadio>
<h:panelGroup>
<t:radio for="discount2" index="2" /><f:verbatim> </f:verbatim>
<h:inputText value="#{carconf.bandName}" />
</h:panelGroup>
<%/* t:radio is a myfaces extension. renders the selectItem at the
given index (starting with 0). the for attribute must be the id
of the corresponding t:selectOneRadio */%>
<t:radio for="discount2" index="0" />
<t:radio for="discount2" index="1" />

</h:panelGrid>
http://www.irian.at/myfaces/selectbox.jsp.source

Errors

javax.servlet.ServletException: org.apache.myfaces.renderkit.html.util.DummyFormUtils.isWriteDummyForm(Ljavax/faces/context/FacesContext;)Z

Solution:
????????? Probably several implementations of myfaces are included, check the component file in eclipse

Code snippets

Ajax links

<form jsfc="h:form">
        <span class="relevanceback">
             <ui:fragment rendered="#{searchCriteria_backing.relevance}">
             <a jsfc="h:commandLink" value="Reset relevance" >
                    <a4j:support event="onclick"
                        action="#{searchHandler_Backing.resetRelevance}" reRender="content">
                    </a4j:support>
            </a>
                <span class="unsichtbar">. </span>
            </ui:fragment>
        </span>
        </form>

<form jsfc="h:form">
        <a jsfc="a4j:commandLink" reRender="content" action="#{searchHandler_Backing.searchForOneMediaType}" value="${linktext}" class="${class}" title="${title}">
            <f:param name="argument" value="${mediaTypeId}"/>
        </a>
 </form>

Ajax Checkbox

    <form jsfc="h:form"><h:selectBooleanCheckbox
         value="#{searchHandler_Backing.mediaType}">
        <f:param name="argument" value="${argument}"/>
        <a4j:support event="onclick" reRender="content"
            action="#{searchHandler_Backing.toggleMediaType}" ignoreDupResponses="true">
            <f:param name="argument" value="${argument}"/>
        </a4j:support>
    </h:selectBooleanCheckbox>
    <a4j:outputPanel ajaxRendered="true">
      <h:messages />
    </a4j:outputPanel>
   </form>

When there are javascript problem "f is not defined Log" or something along those lines, it apparently is a Mozilla/Firefox bug, and one can try to use
a4j:commandLink instead of normal links (note: still withing h:form, no need to do a4j:form)



/**
  * Delete input values for specific components
*
* @param clientId Die JSF clientId der zu bereinigenden Komponente.
*/
public static void clearComponent(String clientId) {
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot uiViewRoot = facesContext.getViewRoot();

Object component = uiViewRoot.getAttributes().get(clientId);
if (component instanceof UIInput) {
((UIInput)component).setSubmittedValue(null);
}
}




More programming related pages

Workflow: Release process
Workflow: Bug tracking
Teambox (Redbooth) - Mantis connector
Design Patterns
Git & Github
Go / Golang
CVS
CVS backup script
Distribution process
Installation script
Java Server Faces
Facelets
jibx
jBoss
jBpm
Perl tips
Perl links
PostgreSQL
Python / py2app
Shell scripts
Xslt
Node.js
Facebook / Opengraph
PHP developer notes
Redbooth API through php
Website optimization
jqTableKit demo
Javascript / html / css links