angel
angel

Reputation: 41

How to get request parameter form JSP page in Facelets page?

I have a login.jsp page:

<form method="post" action="url:8081/login.xhtml">
    Username : <input type="text" name="txtUsername"/>
    Password : <input type="text" name="txtPassword"/>
    <input type="submit" value="submit"/>
</form>

When I submit it, how can I get the txtUsername and txtPassword parameters in login.xhtml?

Upvotes: 3

Views: 7406

Answers (1)

BalusC
BalusC

Reputation: 1108632

All request parameters are in EL available by the #{param} map. Thus, this should do:

<p>Username: #{param.txtUsername}</p>
<p>Password: #{param.txtPassword}</p>

If you need to preprocess them by Java code, better put them as managed properties or view parameters in the backing bean class associated with login.xhtml.

Managed property example:

@ManagedBean
@RequestScoped
public class Login {

    @ManagedProperty("#{param.txtUsername}")
    private String username;

    @ManagedProperty("#{param.txtPassword}")
    private String password;

    @PostConstruct
    public void init() {
        // Do here your thing with those parameters.
        System.out.println(username + ", " + password);
    }

    // ...
}

View parameter example:

<f:metadata>
    <f:viewParam name="txtUsername" value="#{login.username}" required="true" />
    <f:viewParam name="txtPassword" value="#{login.password}" required="true" />
    <f:event type="preRenderView" listener="#{login.init}" />
</f:metadata>

with

@ManagedBean
@RequestScoped
public class Login {

    private String username;
    private String password;

    public void init() {
        // Do here your thing with those parameters.
        System.out.println(username + ", " + password);
    }

    // ...
}

See also:

Upvotes: 5

Related Questions