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>

当我提交它时,我怎样才能得到txtUsernametxtPassword参数login.xhtml

4

1 回答 1

5

所有请求参数都在#{param}地图可用的 EL 中。因此,这应该这样做:

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

如果需要通过 Java 代码对它们进行预处理,最好将它们作为托管属性或视图参数放在与login.xhtml.

托管属性示例:

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

    // ...
}

也可以看看:

于 2012-06-04T13:54:42.887 回答