希望听听有关从JSF UI编辑JPA实体的最佳实践的专家.
所以,关于这个问题的几句话.
想象一下,我有持久化对象MyEntity,我将其取出进行编辑.在DAO层我使用
return em.find(MyEntity.class, id);
Run Code Online (Sandbox Code Playgroud)
MyEntity在"父"实体上返回带有代理的实例 - 想象其中一个是MyParent.MyParent被提取为代理问候语@Access(AccessType.PROPERTY):
@Entity
public class MyParent {
@Id
@Access(AccessType.PROPERTY)
private Long id;
//...
}
Run Code Online (Sandbox Code Playgroud)
和MyEntity有它的参考:
@ManyToOne(fetch = FetchType.LAZY)
@LazyToOne(LazyToOneOption.PROXY)
private MyParent myParent;
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.在UI中,我只是直接使用获取的对象而不创建任何值对象,并使用选择列表中的父对象:
<h:selectOneMenu value="#{myEntity.myParent.id}" id="office">
<f:selectItems value="#{parents}"/>
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
一切都好,没有LazyInitializationException发生.但是当我保存对象时,我收到了
LazyInitializationException: could not initialize proxy - no Session
Run Code Online (Sandbox Code Playgroud)
在MyParent代理setId()方法.
如果我改变MyParent关系,我可以轻松解决问题EAGER
@ManyToOne(fetch = FetchType.EAGER)
private MyParent myParent;
Run Code Online (Sandbox Code Playgroud)
或使用获取对象left join fetch p.myParent(实际上我现在这样做).在这种情况下,保存操作正常,并且关系MyParent透明地更改为新对象.不需要执行其他操作(手动复制,手动参考设置).非常简单方便. …
我想测量JSF应用程序的渲染时间.由于我的权力原因,应用程序无法填充日志.
因此,我的问题是,在使用任何浏览器执行包含后端(服务器)调用的特定操作之后,有什么方法可以衡量应用程序的呈现时间?
到目前为止,在使用Chrome开发者工具后,我发现了以下内容.在"网络"选项卡上,每个请求都显示"时间".此外,在选择某个条目后,在"定时"选项卡上,将显示更详细的可视化.现在,我可以从中了解到,"Waiting"表示它在这里捕获到服务器的往返,但是实际的渲染时间呢.
假设整个请求耗时1秒,等待部分耗时500毫秒,我能否推断渲染时间是1秒-500毫秒?我不这么认为,这就是我问这个问题的原因.
简而言之,我需要从浏览器中了解服务器处理需要多长时间以及实际UI渲染时间.
任何提示将非常感谢.谢谢.
我在执行应用程序时遇到以下错误:
java.sql.SQLException:没有为参数1指定值
这是什么意思?
我UserGroup在我的名单中的列表:
public List<UsuariousGrupos> select(Integer var) {
List<UsuariousGrupos> ug= null;
try {
conn.Connection();
stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");
ResultSet rs = stmt.executeQuery();
ug = new ArrayList<UsuariousGrupos>();
while (rs.next()) {
ug.add(getUserGrupos(rs));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.Disconnected();
}
return ug;
}
public UsuariousGrupos getUserGrupos(ResultSet rs) {
try {
UsuariousGrupos ug = new UsuariousGrupos(rs.getInt("id_usuario"), rs.getInt("id_grupo"));
return ug;
} catch (SQLException e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 我已经进入了一个重新考虑JSF实现的项目.现有代码没有遵循正确的JSF标准.为了实现这一点,我正在学习JSF中的所有概念(我已经掌握了JSF的实验).具体来说,我想问一下我的想法.
请回答以上问题.如果我对答案很清楚,那么我会提出更多问题.
我知道Application-Scope持久存在于多个用户之间,因此我们应该确保所有ApplicationScoped ManagedBeans都是线程安全的.
我也明白,我们不需要关心RequestScoped ManagedBean的线程安全性.这是因为它仅针对一个HTTP请求而持续,并且如果被引用则针对每个请求进行新实例化.
但我不太清楚为什么我们应该担心SessionScoped ManangedBean的线程安全性.即使它在多个请求中持续存在,每个用户都可以获得自己的实例,对吧?
那么,为什么我们需要在SessionScoped ManagedBeand的情况下担心线程安全,这也适用于ViewScoped ManagedBean吗?ViewScope在同一视图的两个连续请求中持续存在,对吧?
我试图从一个小型facelet应用程序将一些值保留到MySql数据库,但不断收到此错误.我有一个JPS页面和一个servlet这个相同的应用程序,它与大致相同的逻辑工作得很好,这是我第一次尝试使用facelets所以它可能只是一些愚蠢但我会帮助你.
谢谢
错误
javax.faces.el.EvaluationException: javax.persistence.TransactionRequiredException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:163)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:145)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:263) …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作4个相关菜单.
当用户从第一个菜单中选择项目时,第二个菜单将显示相关数据,当用户从第二个菜单中选择项目时,第三个菜单将显示相关数据,依此类推.
用户将仅在第一个菜单上看到项目,而其他项目将为空白.如果他在第一个菜单上选择一个项目,则第二个菜单将显示数据,但第三个和第四个将保持空白,依此类推.用户最终必须从所有4个菜单中选择条目.
<h:selectOneMenu id="first" value="#{nodes.selectState"}>
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity"}>
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion"}>
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation"}>
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
节点Java类
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new …Run Code Online (Sandbox Code Playgroud) 当有数据在几个页面中重复时(参考示例)我可以加载到一个托管bean中,并在同一页面中使用多个托管bean.它的影响是什么?
这些天我正在努力学习JSF + Facelets.我有一个BackingBean和一个Facelet xHTML页面.当我请求facelet页面(只有一次)时,会多次调用backing-bean方法.
这可能是什么原因?
我看不到什么特别的东西.提前致谢.
这是facelet:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<ui:composition template="index.xhtml">
<ui:define name="content">
<h:form>Name: <h:inputText id="nameFilterPattern" value="#{kundenBackingBean.nameFilterPattern}" /><h:commandButton value="Suchen"/></h:form>
<h:dataTable var="kunde" value="#{kundenBackingBean.kunden}" rowClasses="rowHighlight, rowOrdinary">
<h:column>
<f:facet name="header">
<h:outputText value="Kundennr" />
</f:facet>
<h:outputText value="#{kunde.kundenNr}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{kunde.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Vorname" />
</f:facet>
<h:outputText value="#{kunde.vorname}"/>
</h:column> …Run Code Online (Sandbox Code Playgroud) 我在JSF页面中有以下内容:
<h:commandLink action="#{manager.removeEntity(row.id)}" value="Remove">
<f:ajax event="action" render=":form:table" />
</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
渲染工作完美,但在执行操作之前渲染组件.(我通过记录知道这一点)
在服务器上执行动作功能后,有什么方法可以渲染组件吗?
任何帮助将不胜感激
更新1
我删除了action属性并为标记添加了一个监听器,但不幸的是它似乎没有帮助,在渲染组件树之后仍然会调用该方法.