如何在Facelets页面中获取请求参数表单JSP页面?

时间:2012-06-04 04:49:37

标签: jsf-2 facelets

我有一个login.jsp页面:

<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>

当我提交时,如何在txtUsername中获取txtPasswordlogin.xhtml参数?

1 个答案:

答案 0 :(得分:5)

所有请求参数均由#{param}地图提供给EL。因此,这应该做:

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

如果需要通过Java代码对它们进行预处理,最好将它们作为托管属性或查看与login.xhtml关联的支持bean类中的参数。

托管属性示例:

@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);
    }

    // ...
}

查看参数示例:

<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>

@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);
    }

    // ...
}

另见: