ui重复以显示更多值,并且方法未在JSF2.2中的托管Bean中调用 [英] ui repeat to show one more value and method is not invoking in managed Bean in JSF2.2

查看:47
本文介绍了ui重复以显示更多值,并且方法未在JSF2.2中的托管Bean中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ui:repeat value="#{cc.attrs.bean.foo.foo1}" var="test" varStatus="test1">
    <h:outputText value="#{test.prime}" title="#{test.primeNumber}" />
    <h:outputText value="," rendered="#{!test1.last}" />
</ui:repeat>

我得到一个值example1,example2

现在添加新行后:

<ui:repeat value="#{cc.attrs.bean.foo.foo1}" var="test" varStatus="test1">
    <h:outputText value="#{cc.attrs.bean.testNo(test)}" rendered="#{test1.first}" />
    <h:outputText value="#{test.prime}" title="#{test.primeNumber}" />
    <h:outputText value="," rendered="#{!test1.last}" />
</ui:repeat>

我希望我的输出是这样的Hello- example1,example2......

I want my output something like this Hello- example1,example2......

但是我无法获得此输出.实际上,不会调用testNo(test)方法.这到底是怎么回事.预先谢谢你

But I am not able to get this output. In fact testNo(test) method is not invoked. What exactly is getting wrong over here. Thank you in advance

Manage bean method
 private String testNo(Test test) {
        List<Test11> type = Lists.newArrayList();
        String some = someService.findTestNumber(test.getSomeNumber());
        return some;

    }

推荐答案

您需要制作一个基本的MVC Web应用程序.有很多关于此的教程,但是示例总是有帮助:您的模型很简单,一个CompletedTest的两个实体:

You need to make a basic MVC webapp. There are many tutorials about this, but example always helps: Your model is simple, two entities for a CompletedTest:

@Entity
public class CompletedTest {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    @OneToMany(fetch=FetchType.EAGER)
    private List<TestResult> results;
    // getters and setters
}

TestResult:

@Entity
public class TestResult {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String result;
    // constructors, getters, setters
}

通过service layer

@Stateless
public class TestService {
    @Inject
    private EntityManager em;

    public CompletedTest findTestResultByName(String name) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<CompletedTest> q = cb.createQuery(CompletedTest.class);
        Root<CompletedTest> tr = q.from(CompletedTest.class);
        q.select(tr).where(cb.equal(tr.get("name"), name));
        List<CompletedTest> testResults = em.createQuery(q).getResultList();
        if ( testResults.size() > 0 ) return testResults.get(0);
        else return null;
    }

创建一个controller以连接到您的view:

The create a controller to interface to your view:

@Model
public class TestResultController {
    @Inject
    private TestService testResultService;

    private CompletedTest testResult;

    @PostConstruct
    public void init() {
        testResult = testResultService.findTestResultByName("Test #1");
    }    

最后,您的视图就是您的JSF页面:

and finally your view is your JSF page:

<ui:repeat var="r" value="#{testResultController.testResult.results}" varStatus="tSt">
    <h:outputText value="#{testResultController.testResult.name}: " rendered="#{tSt.first}" />
    <h:outputText value="#{r.result}"/>
    <h:outputText value=", " rendered="#{!tSt.last}" />
</ui:repeat>

如您所见,JSF页面访问Controller bean.该bean中有一个PostConstruct批注,当JSF页面引用该bean时将执行该批注. PostConstruct执行初始化bean所需的任何代码,在这种情况下,调用service layer从数据库加载测试结果.当然,我没有包含一些代码,例如创建测试或显示CompletedTests列表,以便用户可以选择所需的代码.您可以使用表单和下拉列表在单个页面上完成所有操作.此外,每次加载JSF页面时调用数据库都会导致性能下降,因此存在用于在内存中缓存Entities的更复杂的机制,但这超出了这个简单的答案.

As you can see, the JSF page accesses the Controller bean. In that bean is a PostConstruct annotation, which gets executed when the JSF page references the bean. The PostConstruct executes any code you need to initialize the bean, in this case a call to the service layer which loads a test result from the database. Of course, there is a bit of code I didn't include, such as creating a test or showing a list of CompletedTests so the user can choose which one he or she wants. You can do all that on a single page with a form and a drop-down list. Further, calling to the database every time the JSF page is loaded will result in poor performance, so there are more sophisticated mechanisms for caching Entities in memory, but that is beyond this simple answer.

这篇关于ui重复以显示更多值,并且方法未在JSF2.2中的托管Bean中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆