如果我需要在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>
答案 0 :(得分: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 {}
这是同一个。
希望它澄清你的怀疑......
答案 1 :(得分: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);
}