1

我创建了一个使用 JSF 和托管 bean 的简单应用程序。我试图从托管 bean 实例化一个将数据存储到数据库的 spring bean。但是“ @Autowired ”注释似乎不起作用,因为我得到了一个nullPointerExcpetion

javax.faces.el.E​​valuationException:java.lang.NullPointerException

有调用authenticationComponent.authenticate()方法时出现问题

rmq : 我测试了 ClientService 的方法,一切都很好

托管豆:

@ManagedBean(name="authenticationComponent")
@RequestScoped
public class AuthenticationComponent implements Serializable {

    private static final long serialVersionUID = -5085727127290746426L;
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    @Autowired
    ClientService clientService;
    Client client;
    String clientName;
    String clientPass;
    public String authenticate() {

        client = clientService.authenticate(clientName, clientPass);
        if (client != null) {
            return SUCCESS;
        }
        return ERROR;

    }

认证.xhtml

              <p:inputText id="userName"  required="true"
                  label="User Name" title="Enter Your Name !" value="#{authenticationComponent.clientName}"/>
              <p:tooltip for="userName" styleClass="tooltip" showEvent="focus"
                  hideEvent="blur" />

              <h:panelGroup>
                  <h:outputText value="User Password : " />
                  <h:outputText style="color:red" value="* " />
              </h:panelGroup>

              <p:inputText id="userPass"  required="true"
                  label="User Password" title="Enter Your Password !" value="#{authenticationComponent.clientPass}" />
                  <!-- 
                          label option is needed for messages component
                   -->
              <p:tooltip for="userPass" styleClass="tooltip" showEvent="focus"
                  hideEvent="blur" />

              <p:commandButton id="submitLoginButton"
                  update="loginPanelGrid,messages" value="Sign In"
                  icon="ui-icon-check" action="#{authenticationComponent.authenticate()}" />
              <p:commandButton id="newUserButton"
                  update="loginPanelGrid,messages" value="Sign Up"
                  icon="ui-icon-plus"  />

应用上下文:

    <bean id="clientService" class="com.webapp.service.ClientServiceImpl">
            <property name="clientDao" ref="clientDao" />
        </bean>
 <bean id="genericDao" class="com.webapp.dao.GenericDaoImpl" abstract="true" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao">
        <constructor-arg ref="client"  />
   </bean>

  <bean id="client" class="com.webapp.model.Client"/>
4

1 回答 1

1

首先,您的faces-config.xml文件中需要一个自定义 el-resolver:

 <application>
        <!-- For DI of beans from Spring WebApplicationContext -->
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

然后你需要@ManagedProperty像这样使用注释:

@ManagedProperty(value = "#{clientService}")
ClientService clientService;

所以spring el-resolver可以通过名字注入你的服务bean。

于 2013-07-01T01:25:42.367 回答