0

I've created an address bean and I want to use it twice - once for street address and once for mailing address. I can achieve this using faces config as per the below, but I'm wondering if I can do this via annotations.

e.g. put @ManagedBean(name="StreetAddress") and @ManagedBean(name="MailingAddress") on the same class? I feel like I am missing something obvious here but I'm not sure what.

<managed-bean> <managed-bean-name>MailingAddress</managed-bean-name> <managed-bean-class>com.leetb.jsf_ex1.model.AddressBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <map-entries/> </managed-bean> <managed-bean> <managed-bean-name>StreetAddress</managed-bean-name> <managed-bean-class>com.leetb.jsf_ex1.model.AddressBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <map-entries/> </managed-bean> public class AddressBean { private String line_one; private String line_two; private String suburb; private String state; private String postcode; /* getters and setters snipped */ } 

1 Answer 1

1

You've there a design mistake. Those look more like model classes than backing bean classes. Model classes shouldn't be managed beans at all. Make them a property of a backing bean class and manage it instead.

E.g.

@ManagedBean @ViewScoped public class AddressBacking { private AddressBean mailingAddress; private AddressBean streetAddress; @PostConstruct public void init() { // Prepare them here yourself. For example, when it's for a new entry. mailingAddress = new AddressBean(); streetAddress = new AddressBean(); } public void save() { // ... } // ... } 

(I'd rename AddressBean to Address by the way)

and use it as follows

<h:inputText value="#{addressBacking.mailingAddress.line_one}" /> ... <h:inputText value="#{addressBacking.streetAddress.line_one}" /> ... 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - Ive been mixing up my class model and object model. Need to start thinking differently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.