Thursday, September 9, 2010

Passing parameters through faces-config.xml.


View Page:


Code Snippet-1



<f:view>
<h1>Welcome to Account Activation Page</h1>
<h:form>
<h:outputText value="Please click on the link below to activate your account."/>
<h:commandLink value="http://localhost:8080/IdeaPoke/jsp/activation-page.jsf" action="#{ActivateUser.activate}">
<f:param name="key" value="#{ActivateUser.queryString}"/>
</h:commandLink>
</h:form>
</f:view>



Explanation:


The above code(CS -1) introduces an attribute <f:param> within the <h:commandLink> which suggests that it will be holding on to a value named queryString and pass it to the faces-config.xml file whenever the link is clicked. The value to the attribute queryString is attached with a reference name i.e “key” which practically acts as the carrier.

Managed Bean (ActivateUser):

Code Snippet-2





public class ActivateUser
{
private String key;
private String queryString;

// Setter & Getter Methods started here //
public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getQueryString() throws DataAccessException,SQLException{

// Database connection established here

HibernateSpringDAOdao=(HibernateSpringDAO)ServiceFinder.findBean("SpringHibernateDao");
userLogin=dao.getLastRecordId();
// Value retrieved and assigned to queryString
setQueryString(userLogin.getVerificationKey());
System.out.println(queryString);
return queryString;
}

public void setQueryString(String queryString) {
this.queryString = queryString;
}
// Setter & Getter Methods ended here //


public String activate() throws Exception {
String status = "failure";
String key_value = (FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get("key"));
return status;
}

}







Explanation:

At the time of page load (view page), it invokes getQueryString() method which retrieves the persisted value from the database and is available in the view page. The “SpringHibernateDao” is the bean which we will be interacting while making any database transactions. Since this is not our concern right now, so we will leave that for now.
The event starts when the user clicks on the link. The activate() method is triggered which retrieves the parameter value based on the object “key” through the getRequestParameterMap() method.


Faces-config.xml:


Code Snippet-3




<managed-bean>
<managed-bean-name>ActivateUser</managed-bean-name>
<managed-bean-class>com.login.action.ActivateUser</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>key</property-name>
<value>#{param.key}</value>
</managed-property>
</managed-bean>


<navigation-rule>
<from-view-id>/jsp/activation-page.jsp</from-view-id>
<navigation-case>
<from-action>#{ActivateUser.activate}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/jsp/login.jsp</to-view-id>
</navigation-case>
</navigation-rule>




Explanation:

I would like to bring to your notice the addition of an attribute <managed-property> which encloses two new parameters <property-name> and <value> in CS -3 . This attribute is actually responsible for transmission of value from one page to another.
The rest of the code is normal which defines a page navigation rule, hence after clicking the link the user will carried forward to login.jsp.

Inputs from your end will be highly appreciated.
Looking forward to implement log4j inside a web application.



** CS means Code Snippet

No comments:

Post a Comment