【发布时间】:2014-09-04 20:27:50
【问题描述】:
我有一些托管 bean (ViewScoped),当前使用会话中的数据进行了初始化。我想用 URL GET 参数初始化它们,这样我就可以为 URL 提供我想在视图中显示的实体 ID。类似于displayClient.xhtml?entityId=123。
现在我在视图主实体的 getter 中考虑这样的事情:
public clientModel getclientM() {
if (this.clientM == null) {
// TODO: Check for empty, non-integer or garbage parameters...
// Anything exists to "sanitize" URL parameters?
int entityId = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("entityId"));
// I guess I should check here if the logged user is authorized to
// load client entity with this entityId... anything else to check?
this.clientM = this.clientS.find(entityId);
}
return this.clientM;
}
任何关于最佳实践的提示或建议将不胜感激。
【问题讨论】:
-
但是为什么呢? bean 在 http 会话中管理,而不是在 URL 中
-
你在谈论视图参数吗? stackoverflow.com/questions/6377798/…
-
@Jaqen H´ghar 看起来你完全找到了我想要的东西。我不知道 JSF 支持这样的参数。我需要进一步清理 f:viewparams 还是可以通过这种方式信任它?
-
我会说除非它们被绑定到一个字符串,否则它们已经通过了转换器,因此是完全安全的。您还可以选择使用验证器。如果绑定到字符串,您可能应该考虑它的确切用途。您甚至可以将其直接绑定到 bean.clientM 并使用转换器 - 然后此转换器应将 String 转换为 Integer 并 find() 实体,因此如果参数无效,它将抛出 NumberFormatException