如何在 JSF 中填充数据
到现在为止,我一直都是使用JSP来显示页面。当用户请求诸如“添加项目”之类的页面时,我将加载数组列表中的所有项目类别并将它们显示为选择框中的选项,如下所示:
<select name="category>
<%
ArrayList<Category> categories = (ArrayList<Category>) request.getAttribute("categories");
for (Category c : data) {
%>
<option value="<%= c.getId() %>"><%= c.getName() %></option>
<%
}
%>
</select>
从“JavaServer Faces 2.0,完整参考”一书中,我了解到“JSF 通过禁止在标记页面中包含 Java 代码来强制执行干净的模型-视图-控制器分离”。因此,如果有人能够向我展示如何使用 JSF 处理上述任务,我将非常感激,因为我无法像以前那样使用 Java 代码。
最好的问候,
詹姆斯·特兰
Up until now, I have always been using JSP to display pages. When a user request for a page such as "Add Item", I will load all Item Category in an Array List and display them as options in select box like this:
<select name="category>
<%
ArrayList<Category> categories = (ArrayList<Category>) request.getAttribute("categories");
for (Category c : data) {
%>
<option value="<%= c.getId() %>"><%= c.getName() %></option>
<%
}
%>
</select>
From the book "JavaServer Faces 2.0, The Complete Reference", I learnt that: "JSF enforces clean Model-View-Controller separation by disallowing the inclusion of Java code in markup pages". Hence, I'd be very grateful if someone could show me how I can handle the above task using JSF since I cannot use Java code as I have always done anymore.
Best regards,
James Tran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSF 2.0 使用 Facelets 作为模板方法,简而言之,它是带有一些附加元素的 XHTML。
虽然从技术上讲,您可以从 Facelets 执行方法调用,但一般来说,其想法是使用适当的 geter/setter 方法访问 JavaBean 来执行数据移动。您可以按照以下代码段所示来完成此操作:
在 bean 方面,您希望使用 faces-config(这在很大程度上是不鼓励的)或诸如 CDI 或托管 Bean 基础结构之类的机制向 JSF 公开 bean。如果您选择 CDI 路线,我强烈建议您考虑使用 SEAM,因为它将统一(目前非常奇怪的不同)托管 Bean 和 CDI 框架,因此您可以在 CDI 中使用 JSF 作用域,并在 JSF 作用域中使用 CDI bean。
您还可以让 getter 对您的值进行延迟初始化(例如,从数据库中提取 CategoryList),并使用其他一些 JSF 注释来执行各种初始化任务。
您还可以对操作方法进行编码,该方法返回一个表示返回后要执行的 JSF 操作的字符串(这会被编码到您的 faces-context.xml 文件中)。支持 bean 上的阶段侦听器还可以在页面呈现、验证和提交的各个阶段调用,从而为您提供非常细粒度的控制。
上面例子中的categoryList当然不限于基本类型,还有。还有一些用于写出所选项目的文本版本的语法,因此您可以创建一些相当复杂的表达式来以友好的方式显示每个项目。
JSF 2.0 uses Facelets as the templating method, which in a nutshell is XHTML with some additional elements.
While technically you can perform method calls from Facelets, in general the idea is to access a JavaBean with proper geter/setter methods to perform your data moving. You can accomplish this as the below segment of code shows:
On the bean side of things, you want to expose a bean to JSF using either faces-config (which is largely discouraged) or a mechanism such as CDI or the Managed Bean infrastructure. I highly recommend you look into using SEAM if you go the CDI route, as it will unify the (currently really strangely disparate) Managed Bean and CDI frameworks, so you can use JSF scopes in CDI, and have CDI beans available in JSF scopes.
You can also make the getters do lazy initialization of your values (for pulling categoryList from a database for example), and use some other JSF annotations to do various initialization tasks.
You can also code action methods which return a String representing the JSF action (this gets coded into your faces-context.xml file) to take after returning. Phase listeners on the backing bean can also be called at various stages of page rendering, validation and submission, getting you very fine grained control.
categoryList in the above example is not limited to basic types of course, and <f:selectItems> also has some syntax for writing out the textual version of your select items, so you can make some quite complex expressions to display each item in a friendly way.
创建一个 bean 并使用例如
@Named
使其为人所知,以便您可以从 JSF 脚本中引用它。然后为该 bean 提供一个返回您想要显示的数据的方法,并在需要该数据的位置(例如循环构造)从 JSF 脚本中调用该方法。Create a bean and make it known with e.g.
@Named
so you can refer to it from your JSF script. Then give that bean a method returning the data you want to show, and invoke that method from your JSF script in a location where that data is expected e.g. a loop construct.将要显示的数据存储在 Java 列表中,并将该列表公开为支持 bean 的属性。使用适当的 JSF 标记来显示该属性。
Store the data you want to display in a Java list, and expose that list as a property of a backing bean. The use the appropriate JSF tag to display that property.
在 JSF 2.0 中,您可以包含标签 h:selectOneMenu,您可以在其中获取存储所选项目值的值。 f:selectItems 中的值可以是任何对象的集合,大多数情况下,此对象中的 SelectItem 您声明值对象和要显示的标签。
如果您需要另一个对象的值和标签,则必须声明
In JSF 2.0 you can include the tag h:selectOneMenu in which you get the value where you store the item value selected. The value in f:selectItems could be a collection of any object the most of times SelectItem in this object your declare value object and the label to display.
if you required values and labels of another object in you must declare