我正在创建一个Web应用程序,您必须从DB中读取对象/实体列表并在JSF中填充它<h:selectOneMenu>.我无法对此进行编码.有人可以告诉我该怎么做吗?
我知道如何List<User>从数据库中获取数据.我需要知道的是,如何填充此列表<h:selectOneMenu>.
<h:selectOneMenu value="#{bean.name}">
...?
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud) 有没有办法在加载页面时执行JSF托管bean操作?
如果那是相关的,我现在正在使用JSF 1.2.
是否可以在EL表达式中定义变量并重新使用该变量?
例如 :
<h:inputText
value="#{myBean.data.something.very.long}"
rendered="#{myBean.data.something.very.long.showing}"
/>
Run Code Online (Sandbox Code Playgroud)
我的想法是这样的:
<!--
somehow define a variable here like :
myVar = #{myBean.data.something.very.long}
-->
<h:inputText
value="#{myVar}"
rendered="#{myVar.showing}"
/>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?谢谢 !
我想显示的保存在数据库中的图像字节StreamedContent中<p:graphicImage>,如下所示:
<p:graphicImage value="#{item.imageF}" width="50" id="grpImage" height="80"/>
Run Code Online (Sandbox Code Playgroud)
private StreamedContent content; // getter and setter
public StreamedContent getImageF() {
if (student.getImage() != null) {
InputStream is = new ByteArrayInputStream(student.getImage());
System.out.println("Byte :"+student.getImage());
content = new DefaultStreamedContent(is, "", student.getStuID());
System.out.println("ddd ------------------------------- " + content);
return content;
}
return content;
}
Run Code Online (Sandbox Code Playgroud)
这将返回一个空白图像.这是怎么造成的,我该如何解决?
标准输出打印以下内容:
INFO: Byte :[B@a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent@b0887b
INFO: Byte :[B@a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent@1d06a92
INFO: Byte :[B@d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent@39a60
INFO: Byte :[B@d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent@8c3daa …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?控制器感觉它正在做一些服务工作.
<ui:repeat />标签是否支持迭代java.util.Set?我已经尝试迭代Set中包含的JPA域实体对象,但是收到错误.有什么我想念的吗?是否需要存在额外的旗帜?
我在JSF页面中遇到了ajax请求的问题.当我点击按钮时,我得到以下异常:
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: CDATA tags may not nest
at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.startCDATA(HtmlResponseWriter.java:630)
at javax.faces.context.ResponseWriterWrapper.startCDATA(ResponseWriterWrapper.java:172)
at javax.faces.context.PartialResponseWriter.startError(PartialResponseWriter.java:342)
at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:210)
at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)
at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:123)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
Run Code Online (Sandbox Code Playgroud)
我认为这是String对象的一些问题,因为当我对网站上显示的JPA实体属性进行硬编码时,一切都还可以.但是,当从数据库(PostgreSQL)中检索实体时,它会抛出上述异常.
JSF代码:
<p:column>
<f:facet name="header">
Akcja
</f:facet>
<h:commandButton actionListener="#{mBDocumentMigration.actionEdit(object)}" value="Edytuj" rendered="#{mBDocumentMigration.editingObject == null}" >
<f:ajax render="@form" execute="@form" />
</h:commandButton>
<h:commandButton action="#{mBDocumentMigration.actionZapisz}" value="Zapisz" rendered="#{mBDocumentMigration.editingObject != null}" >
<f:ajax render="@form" execute="@this" />
</h:commandButton>
</p:column>
Run Code Online (Sandbox Code Playgroud) 与前面的示例相关,我试图在服务器上监视我的get/set方法(当它们被调用时,以及多久).所以,我的实际看起来像这样:
@ManagedBean(name="selector")
@RequestScoped
public class Selector {
@ManagedProperty(value="#{param.profilePage}")
private String profilePage;
public String getProfilePage() {
if(profilePage==null || profilePage.trim().isEmpty()) {
this.profilePage="main";
}
System.out.println("GET "+profilePage);
return profilePage;
}
public void setProfilePage(String profilePage) {
this.profilePage=profilePage;
System.out.println("SET "+profilePage);
}
}
Run Code Online (Sandbox Code Playgroud)
并且唯一可以调用此方法的页面(它只调用渲染时的get方法)是:
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
// nothing at the moment
</h:panelGroup>
</h:panelGroup>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
当我看到服务器日志时,我的昏迷,我看到:
SET null
GET main
GET main
GET main
GET main
GET main
GET main
GET main
Run Code Online (Sandbox Code Playgroud)
什么?它调用七次getProfilePage()方法?(也是1次setProfilePage())我想知道为什么这个行为:)
谢谢
添加了一个例子 …
我有一个数据表如下:
<h:dataTable value="#{bean.items}" var="item">
Run Code Online (Sandbox Code Playgroud)
我想用从服务方法获得的数据库中的集合填充它,以便在初始(GET)请求期间打开页面时立即显示它.我什么时候应该打电话给服务方法?为什么?
我有一个关于POJO中List的初始化的问题,因为它遵循下一个代码:
public class Person {
//other fields...
private List<String> friends=new ArrayList<>();
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
}
Run Code Online (Sandbox Code Playgroud)
或者它是这样的更好,并在其他类中具有初始化(例如Bean(JSF))
public class Person {
//other fields...
private List<String> friends;
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是什么方法更好?