1

I need to solve this business requirements but I'm experiencing some unwanted behaviour and I need the assistance of the experts. I'm using JSF 2.2.13, Prime Faces 5.3

I have a UserBean like this

@ManagedBean(name = "userBean")
@RequestScoped
public class UserBean implements Serializable {
  private Long id;
  private String firstName;
  private String lastName;
  @ManagedProperty(value = "#{countryBean}")
  private CountryBean phoneCode1;
  private String phoneNumber1;
  @ManagedProperty(value = "#{countryBean}")
  private CountryBean phoneCode2;
  private String phoneNumber2;
...
...getter/setter

and a CountryBean like this

@ManagedBean(name = "countryBean")
@RequestScoped
public class CountryBean implements Serializable{
  private Long id;
  private String isoCode;
  private String phoneCode;
  ...
  ...getter/setter

The problem(as you probably already know) is that inside the UserBean we have more than 1 field(phoneCode1, phoneCode2)of the same managed property(countryBean).

The strange behavior is that inside the database(MySQL), my application save the same value for all of those fields(phoneCode1, phoneCode2) even if, in the front-end we select different values.

In the front-end I've this piece of code

<h:selectOneMenu value="#{userController.userBean.phoneCode1.id}" class="form-control">
    <f:selectItem itemLabel="#{msg['seleziona']}" itemValue="" noSelectionOption="true" />
    <f:selectItems value="#{applicationScopedBean.countries}" var="ac" itemValue="#{ac.id}" itemLabel="#{ac.phoneCode}"/>
 </h:selectOneMenu>
<h:selectOneMenu value="#{userController.userBean.phoneCode2.id}" class="form-control">
    <f:selectItem itemLabel="#{msg['seleziona']}" itemValue="" noSelectionOption="true" />
    <f:selectItems value="#{applicationScopedBean.countries}" var="ac" itemValue="#{ac.id}" itemLabel="#{ac.phoneCode}"/>
 </h:selectOneMenu>

So, in which way we can solve this business requirement?

I've saw similar questions but I don't understand if I'm not using very well JSF, or if it's a JSF limitation. From the point of view of a database it's similar to ask "In which way I can create a table named "A" with some FK linked to the table "B"?

Thank you!

Gavi
  • 1,300
  • 1
  • 19
  • 39
  • 1
    Is there a particular reason why your CountryBean is a bean and a managed property? I'm still learning myself, but I don't think you need to treat them differently from a string. – klog Apr 07 '16 at 04:40
  • Country is a db lookup table and I need the id and the name – Gavi Apr 07 '16 at 21:20
  • I don't understabnd why UserBean and CountryBean are JSF ManagedBean,.. They are just simple data structures – Raphael Roth Apr 08 '16 at 11:06

3 Answers3

1

I had a very similar situation as yours but a bit more complex as I had several managed properties of the same bean in a "Parent" bean and also in the beans used as managed properties in is Parent bean I had other managed properties to other beans. This because all this "child" beans are tied to front end elements and need to be reusable (basically is a 3 dropdown lists element used to ask the user for the address of his house, his job and other extra location). And the problem was that when the user selects the address for a location the values repeat on the other 3 locations.

So my solution to this problem was to set all the "child" beans (the reusable ones) as @NoneScoped and the "parent" bean as @ViewScoped

And this worked perfectly, the addresses for each location no longer interfere with each other. And all of this with still having several managed properties of the same bean in the "parent" bean.

Hope this works for someone with a similar problem.

S.Mora
  • 23
  • 4
0

In place of @RequestScoped use @NoneScoped for CountryBean.

None Scope

Community
  • 1
  • 1
Ravi
  • 391
  • 2
  • 18
0

In fact the error was that in that case there were no reason to use the annotation @ManagedProperty.

When there're more than 1 instance in the same scope, is useless to use the @ManagedProperty.

The purpose of the @ManagedProperty is to identify the only one available instance in the used scope.

Gavi
  • 1,300
  • 1
  • 19
  • 39