小编ber*_*tie的帖子

@Past或今天的Bean验证?

这是一个简单的来源,声明此日期必须使用Bean验证过去:

@Past
private Date transactionDate;
Run Code Online (Sandbox Code Playgroud)

是否有BV注释用于验证日期必须是今天还是前一天?

或许我需要扩展约束并提供我自己的约束?

谢谢 !

java bean-validation

8
推荐指数
2
解决办法
8379
查看次数

如何使用f:viewParam对GET请求执行操作?

我目前正在尝试将记录的ID从一个页面发送到另一个页面.

所以在第1页中,我有这样的事情:

<p:column>
    <h:link value="#{rpb.map['transNum']}" outcome="TInput.xhtml">
        <f:param name="id" value="#{rpb.map['id']}" />
    </h:link>
</p:column>
Run Code Online (Sandbox Code Playgroud)

在目标页面(TInput.xhtml)中,我有类似的东西来捕获id:

....
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">

<f:metadata>
    <f:viewParam name="id" value="#{tInputBean.id}"></f:viewParam>
</f:metadata>

<h:head>
....
Run Code Online (Sandbox Code Playgroud)

现在,单击链接,转到第2页,第2页由一个视图范围的jsf bean处理.从我的调试来看,这是发生的顺序:

  1. 执行@PostConstruct方法
  2. 使用从viewParam捕获的id更新模型(在appy请求+验证之后)

我想要实现的是: 在更新模型之后,我想对该记录id执行查询,获取它的bean以及它来自Business Service的详细信息列表.

我想知道我应该在哪里放置我的查询代码:

  1. @PostConstruct方法内部是不可能的,因为在@PostConstruct方法完成,从viewParam捕获的id被设置为模型
  2. 在模型更新后使用阶段监听器?
  3. 使用系统事件?虽然我似乎无法找到适合这种情况的人

请赐教:)

jsf jsf-2

8
推荐指数
1
解决办法
8759
查看次数

需要帮助在命令按钮中显示图标图像

只是一个基本需求,但无法让它工作.我想有一个primefaces按钮来显示我的图像,没有任何文字.

但我得到的是一个只包含一个^字符而不显示图像的按钮,其尺寸仅为16x16.


所以,这是primefaces按钮:

<p:commandButton image="ui-icon-csv" title="CSV Document" ajax="false">
    <p:dataExporter type="csv" target="gridRPBDetails" 
        fileName="#{tInputBean.exportFilename}" />
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)

这是css文件:

.ui-icon-csv {
    background-image: url(images/csv_small.png);
}
Run Code Online (Sandbox Code Playgroud)

这是按钮生成的html:

<button type="submit" onclick=";" 
    name="gridRPBDetails:j_idt82" id="gridRPBDetails:j_idt82" 
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" 
    role="button" aria-disabled="false">
        <span class="ui-button-icon-primary ui-icon ui-icon-csv"></span><span class="ui-button-text">CSV Document</span>
</button>
Run Code Online (Sandbox Code Playgroud)

为了证明图像可以访问,我尝试这个URL,实际上它显示了图片:

http://albert:8080/mywebapp/faces/javax.faces.resource/images/csv_small.png
Run Code Online (Sandbox Code Playgroud)

我使用tomcat 7,这些是我的依赖:

<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

出了什么问题?

谢谢 !

html css jsf primefaces jsf-2

8
推荐指数
1
解决办法
3万
查看次数

bean验证默认参数的顺序?

我目前正在尝试使用bean验证提供自定义验证消息.

目前使用spring mvc 3.1.1 + apache bean验证.

在我的bean中,我指定:

@Size(min=1, max=50)
private String title;
Run Code Online (Sandbox Code Playgroud)

在我的messages.properties中:

Size.addForm.title=The title must not be empty and must not exceed {1} characters.
Run Code Online (Sandbox Code Playgroud)

从实验中我发现:

  • {0}指'标题'
  • {1}指的是最大值,即50
  • {2}指的是min,即1

并且它将显示为The title must not be empty and must not exceed 50 characters.正确的.

但所有这些都来自实验.我想知道是否有文件说明默认约束的参数顺序

我希望使用Size.addForm.title=The title must not be empty and must not exceed {max} characters.基于默认ValidationMessages.properties的方法,但最终会使用NumberFormatException {max}.我认为它与插值有关?


更新

因此,每个都在NumberFormatException上独立失败{max}:

  • messages.properties:Size.addForm.title =标题不能为空,且不得超过{max}个字符.
  • messages.properties:Size =标题不能为空,且不得超过{max}个字符.
  • messages.properties:javax.validation.constraints.Size.message =标题不能为空,且不得超过{max}个字符.
  • ValidationMessages.properties:Size.addForm.title …

java spring spring-mvc internationalization bean-validation

8
推荐指数
1
解决办法
4301
查看次数

JSF 2:使用SelectItemGroup + POJO进行选择分组

我尝试使用以下类似的方法处理分组选择:

<h:selectOneMenu value="#{selectionLabBean.oneSelectMenuGroup}"
    id="SelectOneMenuGroup" >
        <f:selectItems value="#{selectionLabBean.heroGroupList}" />
</h:selectOneMenu>
<p:message for="SelectOneMenuGroup" />
Run Code Online (Sandbox Code Playgroud)

heroGroupList是这样的:

SelectItem[] heroArr = new SelectItem[] {
    new SelectItem("Paladin"),
    ...
};
heroListWithGrouping.add(
    new SelectItemGroup("Human", 
        "A collection of human race Heroes", 
        false,
        heroArr
    )
);
.....
Run Code Online (Sandbox Code Playgroud)

而且我想知道我是否可以使用POJO而不是SelectItem对象进行这种分组?

如果我无法实现这一点,我想我必须以某种方式将我的域对象或我的查询结果转换为SelectItem数组以使其工作.

有任何想法吗 ?

jsf jsf-2

7
推荐指数
1
解决办法
4539
查看次数

JSF2:从Spring向managedbean注入服务对象?

我已经测试了这个,试图将服务对象注入@ManagedBean,但它失败了nullpointerexception,因为userService为null.

我目前正在使用Tomcat 7,JSF 2,这里有一些我的pom.xml

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.0.3.RELEASE</org.springframework-version>
    <org.hibernate-version>3.6.0.Final</org.hibernate-version>
    ....
</properties>
Run Code Online (Sandbox Code Playgroud)

这是异常跟踪:

javax.faces.el.EvaluationException: java.lang.NullPointerException
    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:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    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:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.NullPointerException
    at org.albertkam.ui.LoginBean.login(LoginBean.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at …
Run Code Online (Sandbox Code Playgroud)

java jsf spring java-ee jsf-2

7
推荐指数
1
解决办法
1万
查看次数

JPA:与惰性初始化集合合并的行为是什么?

以下是导致问题的序列:

  1. 我有一个团队记录,数据库中有3个玩家记录.Team实体有一个使用FetchType.LAZY,CascadeType.ALL的List
  2. 单击webui上的搜索按钮
  3. 调用服务器端使用JPA查询的查询,查找所有Team记录,在这种情况下,只查询从查询​​返回的团队实体的1条记录(其中包含播放器实体列表的代理)
  4. 将此teamEntity映射到DTO,并将此DTO返回到webui,跳过播放器实体列表的映射
  5. Webui以html格式呈现DTO,准备接收用户的修改
  6. 用户修改团队的属性,例如它成立的日期
  7. 单击webui上的保存按钮
  8. 将DTO转换为团队实体,用于更新现有的团队记录
  9. 但在这种情况下,如果我要使用em.merge(teamEntity),团队记录将会更新,但是玩家列表会发生什么?因为从DTO转换到团队实体时,teamEntity有一个空的玩家实体列表.合并后,我注意到teamEntity的大小为0.但在刷新该实体em.refresh(teamEntity)后,它将返回3个细节大小.

我很困惑:

  1. 合并后为什么大小为0?这就像不再代表记录了
  2. 在进行测试之前,我认为细节将被移除,因为我将teamEntity与空细节合并.

请赐教:)

谢谢 !

jpa jpa-2.0

7
推荐指数
1
解决办法
5699
查看次数

合并并获得没有刷新的待更新版本?

是否可以更改版本化的实体实例,并在不使用flush的情况下获取待增加的版本?因为从我读到的内容来看,即时冲洗并不是一种好的做法,因为它对性能甚至数据损坏都有不良影响?我不确定:D


这是一个简单的代码,输出也是注释:

/*
    Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, ... too long
    the version before modification : 16
    the version after modification : 16
    after merge the modification, the version is : 16
    Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, usermodify_id=?,  ... too long
    after flushing the modification, the version is finally : 17
*/
public void modifyHeaderAndGetUpdatedVersion() {
    String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
    ReceivingGood receivingGood = em.find(ReceivingGood.class, id);
    System.out.println("the version before modification : " + receivingGood.getVersion());

    receivingGood.setTransactionNumber("NUM001xyz");
    System.out.println("the …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa jpa-2.0

7
推荐指数
1
解决办法
9078
查看次数

Spring JPA:使用@Transactional和@PersistenceContext的应用程序管理持久化上下文

目前我正在尝试应用程序管理的持久化上下文,通过手动创建实体管理器并将它们存储起来以启用跨越JSE应用程序中的多个请求调用(可能类似于扩展持久性上下文)的事务.

但是,我想知道我是否可以通过使用spring的@PersistenceContext注入来避免在整个服务和DAO方法中发送entityManager对象作为附加参数,并使用@Transactional注释标记方法以使用与该实体管理器手动启动的事务.

我想我可以通过使用ThreadLocal来管理这个功能,但我会更高兴能够将它附加到spring框架.

这是我想到的一个例子:


UI动作方法:

在这里我们可以看到事务是由ui逻辑启动的,因为后端没有外观/命令方法将这些调用分组到业务逻辑:

Long transactionid = tool.beginTransaction();

// calling business methods
tool.callBusinessLogic("purchase", "receiveGoods", 
                        paramObject1, transactionid);

tool.callBusinessLogic("inventory", "updateInventory", 
                        paramObject2, transactionid);

tool.commitTransaction(transactionid);
Run Code Online (Sandbox Code Playgroud)

在工具里面:

public Long beginTransaction() {
  // create the entity --> for the @PersistentContext
  Entitymanager entityManager = createEntityManagerFromFactory();
  long id = System.currentTimeMillis();
  entityManagerMap.put(id, entitymanager);

  // start the transaction --> for the @Transactional ?
  entityManager.getTransaction().begin();

  return id;
}

public void commitTransaction(Long transactionId) {
  EntityManager entityManager = entityManagerMap.get(transactionId);

  entityManager.getTransaction().commit();
}

public Object callBusinessLogic(String module, String function, 
                        Object paramObject, Long transactionid) …
Run Code Online (Sandbox Code Playgroud)

spring jpa jpa-2.0

6
推荐指数
1
解决办法
1万
查看次数

Spring:@PersistenceContext和@Autowired线程安全?

基于这个例子:

@Service
public class Purchase {
  @PersistenceContext
  private EntityManager em;

  @Autowired
  private PurchaseDAO dao;

  private String normalField;

  .... // methods, operations, etc
}
Run Code Online (Sandbox Code Playgroud)

如果我错了,请帮助纠正我:

  1. 服务类PurchasePurchaseDAO是由spring管理的单例
  2. 服务类的字段normalField不是线程安全的,因为singleton是许多人共享的单个对象
  3. 让我们假设@ Repository-annotated-PurchaseDAO没有任何字段,这意味着它的线程安全,将由spring自动注入
  4. EntityManager的实例也是一个线程安全的属性,因为@PersistenceContext将确保当前事务的实体管理器将被使用.

谢谢 !

spring jpa jpa-2.0

6
推荐指数
1
解决办法
7990
查看次数