0

JSF ManagedBean に Spring 依存関係を注入する必要がある場合 (アノテーションを使用して作成)、@Autowire をサポートしていない spring 2.0 を使用しています。

@ManagedBean アノテーションをそのままにしておくと、依存関係を注入する方法はありますか?

私は Myfaces 2.1.6 を使用して、Jetty で Eclipse を実行しています。

提案してください。

顔-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

これは、@PostConstruct で Bean プロパティをロードするための回避策の 1 つです。

@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 に答える
0

ここで正しいことがわかり
、エラーを注意深く読むことができれば、その識別子が表示されます。ここでは、 wsListingBeanwslistingBean resolved to null
という名前の 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 に答える