0

如果我需要在 JSF ManagedBean 中注入 Spring 依赖项(通过 Annotations 创建),我使用的是 spring 2.0,它不支持 @Autowire。

如果我保持 @ManagedBean 注释原样,有没有办法注入依赖项?

我正在使用 Myfaces 2.1.6 通过 Eclipse 与 Jetty 一起运行它。

请建议。

面孔-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>
4

2 回答 2

0

如果我能在这里看到正确的东西,那么
如果你能仔细阅读你的错误,它会说标识符wslistingBean resolved to null
在这里你想访问名为wsListingBean的 bean,但在某个地方你错误地输入了wslistingBean(L 在这里是小写字母)
所以看看你在做什么这个打字错误。
对于managedBean,如果您只是将第一个字母更改为小字母,则无需输入bean名称 -

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

对于此类,它会自动将wsListingBean作为 bean 名称。
所以你可以把它写成 -

@ManagedBean
@RequestScoped
public class WsListingBean {}

那是一回事。

希望能解开你的疑惑......

于 2012-05-15T04:48:42.510 回答
0

这应该是在 @PostConstruct 中加载 bean 属性的一种解决方法

@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);
        }
于 2012-05-15T05:34:08.687 回答