使用单例类和应用程序范围的托管bean来保存应用程序数据有区别吗?
我需要查找某些JNDI资源,例如远程bean接口,因此我自己写了一个单例来缓存我的引用,只允许单引用.(服务定位)
我在两个不同的浏览器中打开了我的网站,而且这个单例只被初始化了一次.所以我假设它的应用范围?
应用程序范围托管bean的任何其他好处,然后能够访问其在jsf中的属性?
当在CMT(EJB3)中使用以下编程事务和会话习惯用法并且Hibernate Core设置为使用CMT时会发生什么?
假设当前CMT事务是必需的并且使用默认值启动@TransactionAttribute(REQUIRED)
beginTransaction()?commit()尝试立即提交hibernate事务还是等到当前的CMT提交?B.行为是否取决于当前会话是否与CMT绑定使用getCurrentSession()?
// A: openSession()
// B: getCurrentSession();
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
// do some work
tx.commit();
}
catch (final RuntimeException e)
{
try
{
tx.rollback();
}
catch (final RuntimeException e)
{
// log error
}
throw e;
}
finally
{
session.close();
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我目前正在使用单个数据库,并且使用Hibernate的程序化JDBC事务工作正常.现在,该应用程序还使用JMS-Queue for Mail消息传递,并希望将其合并到全局CMT事务中.
编辑:
目前我根本不在应用程序中使用EntityManager,并且还希望将代码可移植到非托管环境中.
Hibernate配置hibernate.cfg.xml启用CMT:
Hibernate 4.2.6和Glassfish 3.1.2
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.autocommit">false</property>
<property …Run Code Online (Sandbox Code Playgroud) 是否有关于如何覆盖模板定义的规则<ui:define>用<ui:insert>.
模板A:
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets">
template A content<br/>
<ui:insert name="content"/>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
模板B:
<ui:composition template="/resources/templates/A.xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:define name="content">
template B content<br/>
<ui:insert name="content"/>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
网站1:
<ui:composition template="/resources/templates/B.xhtml">
Site 1<br/>
<ui:define name="content">
site content<br/>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
输出:
Site 1
site content
Run Code Online (Sandbox Code Playgroud)
该内容<ui:define>取自站点1,不会呈现模板的内容.
网站2:
<ui:composition template="/resources/templates/B.xhtml">
Site 2<br/>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
输出:
Site 2
template B content
template A content
Run Code Online (Sandbox Code Playgroud)
其内容<ui:define>取自模板B和模板A,其中奇怪的是模板B内容在模板A的内容之前呈现.
是否可以使用相同的名称覆盖<ui:define>新的<ui:insert>?
为嵌套创建新名称<ui:insert>是一种可能性,但很难跟踪层次结构以及插件的使用位置.
JAX-RS需要网络模块WAR还是我做错了什么?
每个教程都声明要配置其余服务web.xml.但是在ejb-module中没有web.xml.我必须WAR为休息服务创建一个公正吗?
在我的ejb模块中,我想将EJB公开为休息服务,但无法使其工作.调用"localhost:8080/EjbModule/rest/test/method"会导致404
项目结构
- ear
- EjbModule.jar
Run Code Online (Sandbox Code Playgroud)
码
将Bean公开为JAX-WS Web服务并在浏览器中进行测试是没有问题的.
@ApplicationPath("rest")
public class RestApplication extends Application
{
@Override
public Set<Class<?>> getClasses()
{
final Set<Class<?>> classes = new HashSet<>(1);
classes.add(TestService.class);
return classes;
}
}
@Stateless
@Path("/test")
public class TestService
{
@Path("/method")
@GET
@Produces(MediaType.TEXT_HTML)
public String test()
{
return new Date().toString();
}
}
Run Code Online (Sandbox Code Playgroud)
环境:Glassfish 4.0
编辑:
创建单独WAR的休息服务按预期工作.
如何在类级别验证器上返回不同的消息?
接口
public @interface ValidFoo
{
public String message() default "{package.ValidFoo.message}";
}
Run Code Online (Sandbox Code Playgroud)
履行
@Override
public boolean isValid(final Foo foo, final ConstraintValidatorContext context)
{
// check constraint 1
// --> return message "package.ValidFoo.message_constraint1"
// check constraint 2
// --> return message "package.ValidFoo.message_constraint2"
}
Run Code Online (Sandbox Code Playgroud)
ValidationMessages.properties
package.ValidFoo.message=Validation error.
package.ValidFoo.message_constraint1=Validation error 1.
package.ValidFoo.message_constraint2=Validation error 2.
Run Code Online (Sandbox Code Playgroud) 使用bean验证,特定的hibernate验证器实现是否可以定义某些组以自动用于某些crud操作,如创建或更新?
或者是否有一些内部检查这些操作的hibernate组?
实体Foo:
public int Foo { get; set; }
EF 查询:
int? barX = FOOs.GetQuery().Where(f => f.x == x).Select(f => f.bar).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
如果没有行f.x == x但我想返回,则返回整数的默认值 0 null。
如何实现这一目标?
0 没有明确指示结果是否为空或列值确实为 0!
我想使用 JSF 本机文件上传组件。如果可能,使用 AJAX。但我无法让它工作。
上传文件
<h:form enctype="multipart/form-data">
<h:inputFile value="#{bean.file}" required="true"/>
<h:commandButton action="#{bean.importFile()}"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
我认为使用 JSF 2.2 不应该出现以下错误。
Request.getParts在没有多部分配置的情况下调用。将 a 添加@MultipartConfig到 servlet,或将 multipart-config 元素添加到 web.xml
环境
我知道我可以通过它获得旧的价值UIInput#getValue().
但是在很多情况下,字段绑定到bean值,我想获得默认值,如果输入等于默认值,我不需要验证.
如果某个字段具有唯一约束并且您具有编辑表单,则这非常有用.
验证将始终失败,因为在检查约束方法中它始终会找到自己的值,从而验证为false.
一种方法是使用<f:attribute>并在验证器内部检查将该默认值作为属性传递.但是有更简单的内置方式吗?
我给自己写了一个自定义的NavigationHandler非常类似于跟随一个但只是使用堆栈来保存历史记录:
public class HistoryNavigationHandler extends NavigationHandler
{
private final NavigationHandler navigationHandler;
private final Stack<String> outcomes;
public HistoryNavigationHandler(final NavigationHandler navigationHandler)
{
this.navigationHandler = navigationHandler;
this.outcomes = new Stack<String>();
}
@Override
public void handleNavigation(final FacesContext context, final String fromAction, final String outcome)
{
if (outcome != null)
{
if (outcome.equals("back"))
{
final String lastViewId = this.outcomes.pop();
final ViewHandler viewHandler = context.getApplication().getViewHandler();
final UIViewRoot viewRoot = viewHandler.createView(context, lastViewId);
context.setViewRoot(viewRoot);
context.renderResponse();
return;
}
else
{
this.outcomes.push(context.getViewRoot().getViewId());
}
}
this.navigationHandler.handleNavigation(context, fromAction, outcome);
}
} …Run Code Online (Sandbox Code Playgroud) jsf ×5
java-ee ×3
ejb ×2
hibernate ×2
crud ×1
ear ×1
facelets ×1
file-upload ×1
java ×1
jax-rs ×1
jpa ×1
jsf-2.2 ×1
lambda ×1
linq ×1
managed-bean ×1
navigation ×1
singleton ×1
templates ×1
transactions ×1
validation ×1