我注意到有不同的bean范围,如:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
Run Code Online (Sandbox Code Playgroud)
每个人的目的是什么?如何为我的bean选择合适的范围?
有时,使用时<h:commandLink>,<h:commandButton>或者<f:ajax>,在action,actionListener或listener与标签相关的方法根本不被调用.或者,bean属性不会使用提交的UIInput值进行更新.
有什么可能的原因和解决方案?
假设我像这样指定一个outputText组件:
<h:outputText value="#{ManagedBean.someProperty}"/>
Run Code Online (Sandbox Code Playgroud)
如果我在someProperty调用getter for时打印一条日志消息并加载页面,那么注意每个请求多次调用getter是很容易的(在我的情况下发生了两次或三次):
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
Run Code Online (Sandbox Code Playgroud)
如果someProperty计算的值很昂贵,这可能是一个问题.
我用Google搜索了一下,认为这是一个已知问题.一个解决方法是包括一个检查,看看它是否已经计算过:
private String someProperty;
public String getSomeProperty() {
if (this.someProperty == null) {
this.someProperty = this.calculatePropertyValue();
}
return this.someProperty;
}
Run Code Online (Sandbox Code Playgroud)
这个问题的主要问题是你得到大量的样板代码,更不用说你可能不需要的私有变量了.
这种方法有哪些替代方案?没有那么多不必要的代码,有没有办法实现这一目标?有没有办法阻止JSF以这种方式行事?
感谢您的输入!
任何人都可以澄清我们如何在一般情况下使用,或者在现实世界中使用这个代码片段?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
Run Code Online (Sandbox Code Playgroud) 我正在尝试习惯JSF如何处理数据(来自弹簧背景)
我正在创建一个维护用户列表的简单示例,我有类似的东西
<h:dataTable value="#{userListController.userList}" var="u">
<h:column>#{u.userId}</h:column>
<h:column>#{u.userName}</h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)
然后"控制器"有类似的东西
@Named(value = "userListController")
@SessionScoped
public class UserListController {
@EJB
private UserListService userListService;
private List<User> userList;
public List<User> getUserList() {
userList = userListService.getUsers();
return userList;
}
}
Run Code Online (Sandbox Code Playgroud)
而"服务"(虽然看起来更像是DAO)有
public class UserListService {
@PersistenceContext
private EntityManager em;
public List<User> getUsers() {
Query query = em.createQuery("SELECT u from User as u");
return query.getResultList();
}
}
Run Code Online (Sandbox Code Playgroud)
这是正确的做事方式吗?我的术语是对的吗?"服务"感觉更像是DAO?控制器感觉它正在做一些服务工作.
我已经开始学习JSF,但遗憾的是,大多数教程只提供登录或注册部分.
你能指点我一些更深入的例子吗?我感兴趣的一件事是一个展示产品清单的页面.我在页面回家,我按下页面产品,以便我可以看到添加的最新产品.每次访问该页面时,都会根据数据库中的最新条目创建产品列表.我怎么处理这个?
解决此问题的一种方法是创建一个会话范围的托管bean,在其中我将通过其他托管bean更新不同的实体.我在一些教程中发现了这种方法,但它看起来很困难而且很笨拙.
哪个是解决这类问题的最佳方法?在两页主从细节用户界面中,会话范围的正确用法是什么?
我是JSF的新手,在学习构建在线书店应用程序的过程中.
我有1个班级和1个豆子:Book.java和BookCatelogBean.java.Book类有3个属性:id,title和author它相应的getter和setter.该BookCatelogBean包含ArrayList<Book>在那里我用填充它Books(在将来,我将它连接到数据库).
我有两页:index.xhtml和book.xhtml.我想显示index.xhtml每个格式为REST链接的书名列表及其ID book.xhtml,如下所示:<h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" />
我知道如何使用BookCatelogBean显示1,book但我想显示所有这些?我有一个想法,调用一个方法来BookCatelogBean调用getAllBooks(),返回每个书籍标题,但我如何将它们中的每一个返回到index.xhtml作为JavaserverFace链接而不是字符串?
谢谢
这是我的代码:
Book.java
package bookshop;
import java.io.Serializable;
public class Book implements Serializable {
private int id;
private String title;
private String author;
public Book(int id, String title, String author){
this.title = title;
this.id = id;
this.author = author;
}
public …Run Code Online (Sandbox Code Playgroud) 我有表格中显示的页面列表.每个页面都有属性homePage,我想在数据表中有一列单选按钮要在此属性上绑定,用户只能检查一个值.如何在服务器端获得此值?
我看到了一些例子如下:http: //jforum.icesoft.org/JForum/posts/list/14157.page,但我想知道在这种情况下最佳做法是什么.
更新提供的实体的EJB方法(使用CMT):
@Override
@SuppressWarnings("unchecked")
public boolean update(Entity entity) throws OptimisticLockException {
// Code to merge the entity.
return true;
}
Run Code Online (Sandbox Code Playgroud)
javax.persistence.OptimisticLockException如果检测到并发更新,则将抛出,这将由调用者(托管bean)精确处理.
public void onRowEdit(RowEditEvent event) {
try {
service.update((Entity) event.getObject())
} catch(OptimisticLockException e) {
// Add a user-friendly faces message.
}
}
Run Code Online (Sandbox Code Playgroud)
但这样做会使javax.persistence表达层上的API 产生额外的依赖性,这是一种导致紧密耦合的设计气味.
应该包装哪个例外,以便完全省略紧耦合问题?或者是否有一种标准方法来处理此异常,这反过来又不会导致在表示层上强制执行任何服务层依赖性?
顺便说一句,我发现在EJB中(在服务层本身上)捕获此异常然后向客户端(JSF)返回一个标志值是笨拙的.
我正在尝试创建一个对话框,用于创建对象和更新对象.因此,如果我碰巧点击"新建"按钮,我将看到一个包含要填充的空字段的对话框,或者如果我单击某个条目的编辑按钮,该条目的数据将显示在对话框中以进行更新.
按照版本5.2的primefaces展示中的示例,我可以以只读的outputText形式呈现数据,但是当我将其更改为inputText时,该字段保持为空.以下代码是我的例子:
<h:form id="form">
<p:dataGrid id="guestList" var="guest" value="${guestList.guests}" columns="3" paginator="true" rows="20">
<f:facet name="header">
Guest List
</f:facet>
<p:panel>
<h:outputText value="${guest.name}" />
<br />
<h:outputText value="${guest.street}" />
<br />
<h:outputText rendered="#{guest.street2.length() gt 0}"
value="${guest.street2}" />
<h:panelGroup rendered="#{guest.street2.length() gt 0}">
<br />
</h:panelGroup>
<h:outputText value="${guest.city}, " />
<h:outputText value="${guest.state} " />
<h:outputText value="${guest.zipCode}" />
<p:commandButton update="@form:newGuestDetail" oncomplete="PF('newGuestDialog').show()" icon="ui-icon-edit" styleClass="ui-btn-inline">
<h:outputText styleClass="ui-icon ui-icon-edit" style="margin:0 auto;" />
<f:setPropertyActionListener value="#{guest}" target="#{guestList.selectedGuest}" />
</p:commandButton>
</p:panel>
</p:dataGrid>
<p:dialog header="#{guestList.hasSelected() ? 'Edit Guest' : 'New Guest'}" widgetVar="newGuestDialog" modal="true" showEffect="fade" …Run Code Online (Sandbox Code Playgroud) 我想知道如何List<T>在Facelet中显示如下所示:
public List<T> searchByString(String string) {
return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}
Run Code Online (Sandbox Code Playgroud)
会<h:dataTable>是一个合适的方式吗?
我在创建dataTable时遇到问题,其中每行都有一个inputText和一个commandLink.单击链接时,仅提交行的inputText数据.
像这样的东西?
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{bean.value}"/>
</h:column>
<h:column>
<h:commandLink action="#{bean.save}" value="save">
<f:setPropertyActionListener target="#{bean.item}" value="#{item}" />
</h:commandLink>
</h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)
豆:
@RequestScoped
public class Bean {
private Item item;
private String value;
Run Code Online (Sandbox Code Playgroud)
现在,就像它一样,它正在使用最后一行inputText填充value.我包裹了另一个h:form,但它破坏了其他东西,我已经知道嵌套h:form不是正确的方法来做它
这样做的正确方法是什么?
谢谢.
jsf ×12
jsf-2 ×5
datatable ×2
ejb ×2
java-ee ×2
scope ×2
action ×1
collections ×1
commandlink ×1
dao ×1
dialog ×1
el ×1
facelets ×1
getter ×1
input ×1
iteration ×1
jpa ×1
list ×1
loops ×1
managed-bean ×1
performance ×1
primefaces ×1
session ×1
viewaction ×1
viewparams ×1