Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters via URL to a bean class using JSF?

Tags:

java

jsf

I have created a login page for my JSF application. I want to pass the username and password as parameters via the URL to later receive them as fields in a bean class. How can I do this?

like image 830
smk Avatar asked Apr 08 '13 03:04

smk


People also ask

What is f param in JSF?

f:param tag provides the options to pass parameters to a component or pass request parameters.


2 Answers

You should pass it as POST parameter which is what JSF does by default , You can google for a quick example of a Login page with JSF , however if you want to read the request parameters from URL then you can do like

        <a href="name.jsf?id=#{testBean.id}" />

You need something like this in your bean

@ManagedBean
@RequestScoped
public class TestBean {

  @ManagedProperty(value = "#{param.id}")
  private String id;

  .....
}

You can also do this in your xhtml to get the same outcome, this will work with JSF 2.x as viewParam is not available in JSF 1.2

<f:metadata>
    <f:viewParam name="id" value="#{testBean.id}" />
</f:metadata>

Above line will set the parameter id in bean from the request parameter id when your bean is created.

like image 171
Avinash Singh Avatar answered Oct 26 '22 06:10

Avinash Singh


Well first of all, if you are thinking of appending the username and password as part of query string. THEN DO NOT DO IT, you are making your system vulnerable.

Regarding the answer to your question:

<h:commandLink action="#{ttt.goToViewPage()}" value="View">
    <!-- To access via f:param tag, this does not maps directly to bean. Hence using context fetch the request parameter from request map. -->
    <!-- <f:param name="selectedProfileToView" value="#{profile.id}" /> -->

    <!-- Using this to replace the f:param tag to avoid getting the request object -->
    <f:setPropertyActionListener target="#{ttt.selectedStudentProfile}" value="#{profile.id}" />

</h:commandLink>

f:param (as mentioned in comment), this will not map directly to the bean attribute, but you will have to use context to get the request object from which you can reference the value from the requestparametermap.

FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> requestMap = context.getExternalContext().getRequestParameterMap();

f:setPropertyActionListener, this is the other attribute, which will map directly to the attribute of the managed bean.

<h:commandLink action="#{ttt.goToEditPage(profile.id)}" value="Edit">

If you look here I have mentioned the argument in the function. A method with similar signature should be present in the managed bean class, the value will be mapped directly to the function argument.

like image 30
Himanshu Bhardwaj Avatar answered Oct 26 '22 05:10

Himanshu Bhardwaj