我有一个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
中获取txtPassword
和login.xhtml
参数?
答案 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);
}
// ...
}