如何正确显示JSF中字符串的输出?

时间:2014-03-11 12:10:59

标签: jsf

我在Managed Bean中有一个字符串,我将其作为输出返回给JSF。返回的字符串设置为输出由新行组成的每个String组件。但是,当我将它传递给JSF时,字符串会在一行上打印出来。有没有办法解决这个问题而不必为我在String'输出中组合的每个字符串创建一个单独的方法(并返回字符串)?

    public String welcomeDisplay() {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();   


    if(true){
        em.getTransaction().begin();

        String sessionEmail=Util.getEmail();
        Query myQuery = em.createQuery("SELECT u FROM BusinessAccount u WHERE u.email=:email");
        myQuery.setParameter("email", sessionEmail);
        List<BusinessAccount> userList=myQuery.getResultList();
        em.getTransaction().commit();
        em.close();

        String bName=userList.get(0).getBusinessName();
        String bAddress1=userList.get(0).getAddress1();
        String bAddress2=userList.get(0).getAddress2();
        String bTown=userList.get(0).getTown();
        String bPhone=userList.get(0).getTelephoneNum();
        String bEmail=userList.get(0).getEmail();

        String output=(bName+"\n"+bAddress1+"\n"+bAddress2+"\n"+bTown+"\n"+bPhone+"\n"+bEmail);

        return output;
    } 


    else {
        addMessage(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "User Registration Failed!", null));
        return "failure";
    }
}

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Greeting Page</title>
</head>
<body>
<h:form id="greetingForm">
    <H2>Greeting Page</H2>
    <H4>
        <h:outputText value="#{registerBean.welcomeDisplay()}"/>
        ! You have been successfully authenticated.
    </H4>
    <table>
        <tr>
        <td><h:outputLabel for="companyDescription">
                    <h:outputText id="descriptionLabel" value="Company Description" />
                </h:outputLabel></td>
        <td><h:inputTextarea rows="10" id="companyDescription" value="#{registerBean.businessAccount.description}"
                    size="20" /></td>
                    </tr>
                    <tr>
            <td></td>
            <td><h:commandButton id="createAccounts"
                    action="#{registerBean.update}" value="Update Profile">
                </h:commandButton></td>
        </tr>
                    <tr>
    <td><h:commandButton id="logout" action="#{loginBean.logout}">
        <h:outputText value="Logout" />
    </h:commandButton></td>
    </tr>
    </table>
</h:form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

首先,我认为您应该重新考虑您的设计(将JSF Managed Beans与您的业务和DAO层分开)。
要显示您想要的字符串,只需使用<br/>而不是\n。在您的xhtml页面中:

<h:outputText value="#{yourManagedBean.aPrpoerty}" escape="false"/>

其中aPrpoerty是托管bean的String属性,您可以在welcomeDisplay方法中设置值并为其设置getter。