0

I have a string in a Managed Bean that I am returning to JSF as output. The returned string is set to output each String component that it consists of on a new line. However, when I pass it to JSF the string prints all on one line. Is there a way to get around this without having to create an individual method (and returned string) for each of the strings that I have combined in the String 'output'?

    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 Answer 1

1

First of all I think you should rethink your design (separate JSF Managed Beans from your business and DAO layers).
To display the String like you want it, just use <br/> instead of \n. And in your xhtml page:

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

Where aPrpoerty is a String attribute of your managed bean that you set value in the welcomeDisplay method and have a getter for it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.