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

3

我有一个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>

当我提交时,如何在login.xhtml中获取txtUsername和txtPassword参数?
1个回答

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

    // ...
}

See also:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接