0

If I need to inject Spring dependency in JSF ManagedBean (creating through Annotations), I am using spring 2.0 it does not support @Autowire.

Is there any way out to inject dependencies if I keep @ManagedBean annotation as it is?

I am using Myfaces 2.1.6 running it with Jetty through eclipse.

Please suggest.

faces-config.xml

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <navigation-rule>
        <description>Navigation from the hello page.</description>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/wsListing.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <description>Navigation from the hello page.</description>
        <from-view-id>/wsListing.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>abcService</from-outcome>
            <to-view-id>/abcinput.xhtml</to-view-id>
            <redirect />
        </navigation-case>
        <navigation-case>
            <from-outcome>filterAbc</from-outcome>
            <to-view-id>/filterabcinput.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>


    <navigation-rule>
        <description>Navigation from the hello page.</description>
        <from-view-id>/abcinput.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/abcoutput.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>
    <application>
        <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>es</supported-locale>
        </locale-config>
        <message-bundle>messages</message-bundle>
    </application>
</faces-config>

WsListingBean.java

package com.example.ws.ui;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean(name="wsListingBean")
@RequestScoped
public class WsListingBean {  
    //want to inject some property here through spring injection

    private String webServiceId; 

    public String getWebServiceId() { 
        return webServiceId; 
    }

    public void setWebServiceId(String webServiceId) {
        this.webServiceId = webServiceId;
    }

    public String action()
    {
        return this.webServiceId;
    }

}

wsListing.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" xmlns:lang="en">



<h:head>
<meta charset="utf-8"/>
<title>Web Service Listing</title>
<link href="../resources/css/abc-style.css"  rel="stylesheet" type="text/css" media="screen" />
</h:head>

<h:body class="login-body">
<!-- start main container -->
<div id="nb-login-container"> 
  <!-- start header -->
  <div id="content-header">
    <div class="logo"><a href="#"><img src="../resources/images/abc_logo.png"  alt="Logo abc" /></a></div>
  </div>
  <!-- end header --> 



  <!-- start content container -->
  <!-- start content main -->
  <div id="content-login">
    <div class="content-main-body">


    <div id="table-container">
        <h2>Web Services</h2>
        <h:form>
          <table width="100%" class="list-table">
            <tbody><tr>
              <th width="20%">Name</th>
              <th width="80">Description</th>
              </tr>
            <tr>
              <td><h:commandLink value="First Web Service"  action="#{wsListingBean.action}"><f:setPropertyActionListener target="#{wsListingBean.webServiceId}" value="abcService" /></h:commandLink></td>
              <td>
              Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae.
              </td>
              </tr>
            <tr class="odd-row">
              <td><a href="#">Web Services 2</a></td>
              <td>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</td>
              </tr>
            <tr>
              <td><a href="#">Web Services 3</a></td>
              <td>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident. similique sunt in culpa qui officia deserunt.</td>
              </tr>
            <tr class="odd-row">
              <td><a href="#">Web Services 4</a></td>
              <td>Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.</td>
              </tr>
            <tr>
              <td><a href="#">Web Services 5</a></td>
              <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae.</td>
              </tr>
            <tr class="odd-row">
              <td><a href="#">Web Services 6</a></td>
              <td> Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</td>
              </tr>
            <tr>
              <td><a href="#">Web Services 7</a></td>
              <td>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident. similique sunt in culpa qui officia deserunt.</td>
              </tr>
            <tr class="odd-row">
              <td><a href="#">Web Services 8</a></td>
              <td>Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.</td>
              </tr>
            <tr>
              <td><a href="#">Web Services 9</a></td>
              <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae.</td>
              </tr>
          </tbody></table>
          </h:form>
        </div>


    </div>
  </div>
  <!-- end content main  --> 
  <!-- end content container --> 
</div>
<!-- end main container -->
</h:body>

</html>
Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80

2 Answers2

0

If I can see right thing here then
if u can read your error carefully, it says that identifier wslistingBean resolved to null
Here you wanted to access bean named wsListingBean but somewhere you have typed by mistake wslistingBean(L is small letter here)
So check out where you are doing this typing mistake.
and for managedBean there is no need to put the bean name if you are just changing first letter to small like -

@ManagedBean(name="wsListingBean")
@RequestScoped
public class WsListingBean {}

For this class It will automatically take wsListingBean as bean name.
So u can write it like as -

@ManagedBean
@RequestScoped
public class WsListingBean {}

That is one and the same.

Hope it clarifies your doubt......

Addicted
  • 1,694
  • 1
  • 16
  • 24
0

This should be one work-around to load the bean property in @PostConstruct

@Postconstruct
private void initTheBean(){
webServiceId=resolveValue("#{springbeanname}",String.class);
}

public static <T> T resolveValue(String inputValueExpression,
                Class<T> outputClass) {
            FacesContext context = FacesContext.getCurrentInstance();

            ELContext elContext = context.getELContext();

            ValueExpression expression = context
                    .getApplication()
                    .getExpressionFactory()
                    .createValueExpression(elContext, inputValueExpression,
                            outputClass);

            return (T) expression.getValue(elContext);
        }
Ahamed Mustafa M
  • 3,069
  • 1
  • 24
  • 34