0

I want to pass some IDs in a URL that are later going to be read by a backing bean. For each ID a particular operation will follow.

F.g. URL: localhost:8585/monit/accepted.jsf?acceptMsg=948&acceptMsg=764

The bean:

@ManagedBean
@RequestScoped
public class AcceptedBean {
    @EJB
    private MessageService messageService;

    @ManagedProperty("#{paramValues.acceptMsg}")
    private String[] acceptMsg;

    public String[] getAcceptMsg() {
        return acceptMsg;
    }

    public List<Message> getMessages() {
        return messageService.getAcceptedMessages();
    }

    public void setAcceptMsg(String[] acceptMsgs) {
        this.acceptMsg = acceptMsgs;

        if(acceptMsg != null) {
            try {
                for (String accept: acceptMsg) {
                    final Message o = messageService.find(Integer.parseInt(accept));

                    messageService.accept(o);

                    FacesContext.getCurrentInstance().
                            addMessage(null,
                                    new FacesMessage(FacesMessage.SEVERITY_INFO,
                                            "Message was accepted:", o.getSubject()));
                }

            } catch (Exception e) {
                FacesContext.getCurrentInstance().
                        addMessage(null,
                                new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                        "Error:", e.getMessage()));
            }
        }

    }
}

acceped.xhtml:

....
<f:metadata>
    <f:viewParam name="acceptMsg" value="#{acceptedBean.acceptMsg}" />
</f:metadata>
....

When I tried replacing value with binding in the .xhtml I got

Cannot convert javax.faces.component.UIViewParameter@ee2549 of type class javax.faces.component.UIViewParameter to class [Ljava.lang.String;

Any ideas on how to solve the conversion error would be appreciated!

zaxme
  • 1,065
  • 11
  • 29
  • Shouldn't it be `localhost:8585/monit/accepted.jsf?acceptMsg[]=948&acceptMsg[]=764` and in ManagedBean `@ManagedProperty("#{param.acceptMsg}")`? – pepuch Mar 27 '13 at 09:37
  • Thanks for the idea, but it doesn't help. – zaxme Mar 27 '13 at 10:17

1 Answers1

0

The <f:viewParam> doesn't support multiple parameter values. Remove it. You don't need it in this particular case. You already have a @ManagedProperty("#{paramValues.acceptMsg}") which already sets them.

See also:


Unrelated to the concrete problem, I'd only replace the business job in the setter by a @PostConstruct method. The getters/setters should in principle not be doing anything else than getting or setting a property, that would indicate bad design as they are merely access points and can be invoked multiple times on a per-request basis.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555