citress
citress

Reputation: 909

Using JSF2 @ManagedProperty in Spring managed backing bean

I have a JSF backing bean that is Spring managed but I would like to be able to make use of the @ManagedProperty from JSF. The following does not work:

@Component
@Scope(Scopes.REQUEST)
public class MyRequestBean {

    @ManagedProperty(value="#{param.bcIndex}")
    private int bcIndex;

    public int getBcIndex() {
        return bcIndex;
    }

    public void setBcIndex(int bcIndex) {
        this.bcIndex = bcIndex;
    }
}

Suggestions?

Upvotes: 1

Views: 1376

Answers (1)

skuntsel
skuntsel

Reputation: 11742

Actually it is quite simple. I know of three ways to do your injection:

  1. Use Spring's @Value annotation together with implicit El #{param} object:

    @Value("#{param.bcIndex}")
    private int bcIndex;
    
  2. Make use of ExternalContext#getRequestParameterMap in a @PostConstruct / preRenderView listener:

    //@PostConstruct
    public void init() {
        bcIndex = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("bcIndex");
    }
    
  3. Make a binding in your view utilizing <f:viewParam>:

    <f:metadata>
        <f:viewParam name="index" value="#{myRequestBean.bcIndex}" />
    </f:metadata>
    

Upvotes: 2

Related Questions