我在Managed Bean中有一个字符串,我将它作为输出返回给JSF。返回的字符串被设置为在新行上输出它所包含的每个string组件。但是,当我将它传递给JSF时,字符串将全部打印在一行上。有没有一种方法可以绕过这个问题,而不必为我在字符串"输出"中组合的每个字符串创建一个单独的方法(和返回的字符串)?
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>
首先,我认为您应该重新考虑您的设计(将JSF Managed Beans与您的业务层和DAO层分开)。
要像您想要的那样显示字符串,只需使用<br/>
而不是n
。在xhtml页面中:
<h:outputText value="#{yourManagedBean.aPrpoerty}" escape="false"/>
其中aPrpoerty
是您的托管bean的字符串属性,您在welcomeDisplay
方法中设置值并为其设置getter。