2016-07-27 9 views
-1

Was ich habe, ist eine einfache Hallo Welt-Anwendung mit JSF und Spring mit Maven. Immer wenn ich mein BO (Geschäftsobjekt) aus meiner Managed Bean heraus anrufe, ist das BO immer Null. Ich bin mir nicht sicher, was ich vermisse oder nicht verstehe.Mit JSF und Spring 3 ist das BO immer Null (wenn Sie eine Schnittstelle auf Ihrem BO verwenden)

HelloWorldMB.java

package com.project.web; 

import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext; 

import tech.calvanodesign.business.HelloWorldBo; 

import java.io.Serializable; 

@ManagedBean 
@SessionScoped 
public class HelloWorldMB implements Serializable { 

    public HelloWorldBo helloWorldBo; 

    private static final long serialVersionUID = 1L; 

    private String name; 

    public void init() { 
     System.out.println("HelloWorldMB.init()"); 
     if (helloWorldBo != null) 
      return; 
     System.out.println("helloWorldBo is null"); 
    } 

    public String springTest() { 
     // Call the business object to register the user 
     helloWorldBo.springTest(name); 
     return ""; 
    } 

    // Set the registrationBo attribute used by Spring 
    public void setHelloWorldBo(HelloWorldBo helloWorldBo) { 
     this.helloWorldBo = helloWorldBo; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

welcome.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"> 

<h:head> 
    <title>JSF 2.0 Hello World</title> 
    <h:outputStylesheet library="css" name="style.css" /> 
</h:head> 
<h:body> 
    <h:form> 
     <p:growl id="growl" showDetail="true" sticky="true" /> 
      <p:inputText id="intxtSpringTest" value="#{helloWorldMB.name}"/> 
      <p:commandButton id="cmdbtnSpringTest" value="Test Spring 3 with JSF" action="#{helloWorldMB.springTest}" ajax="false"/> 
     </p:panel> 
    </h:form> 
</h:body> 

HelloWorldBo.java

package com.project.business; 

public interface HelloWorldBo { 
    /** 
    * springTest method 
    * @param name 
    */ 
    public void springTest(String name); 
} 

HelloWorldBoImpl

package com.project.business; 

public class HelloWorldBoImpl implements HelloWorldBo { 

    /** 
    * Tests the spring and jsf implementation 
    */ 
    @Override 
    public void springTest(String name) { 
     System.out.println("HelloWorldBoImpl:: springTest : " + name); 
    } 
} 

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <faces-config 
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" 
     version="2.2"> 

    <application> 
     <el-resolver> 
       org.springframework.web.jsf.el.SpringBeanFacesELResolver 
     </el-resolver> 
    </application> 

</faces-config> 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
     http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>tech.calvanodesign</groupId> 
<artifactId>calvanodesignsource</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>calvanodesignsource Maven Webapp</name> 
<url>http://maven.apache.org</url> 
<dependencies> 

<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-api</artifactId> 
    <version>2.1.7</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-impl</artifactId> 
    <version>2.1.7</version> 
</dependency> 

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>2.5</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>javax.servlet.jsp</groupId> 
    <artifactId>jsp-api</artifactId> 
    <version>2.1</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging</artifactId> 
    <version>1.1.1</version> 
</dependency> 

<dependency> 
    <groupId>commons-lang</groupId> 
    <artifactId>commons-lang</artifactId> 
    <version>2.6</version> 
</dependency> 

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>3.8.1</version> 
    <scope>test</scope> 
</dependency> 

<dependency> 
    <groupId>org.primefaces</groupId> 
    <artifactId>primefaces</artifactId> 
    <version>6.0</version> 
</dependency> 

     <!-- spring-context which provides core functionality --> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>4.2.5.RELEASE</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>4.2.5.RELEASE</version> 
</dependency> 
    </dependencies> 
    <build> 
     <finalName>calvanodesignsource</finalName> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.1</version> 
        <configuration> 
         <source>1.8</source> 
         <target>1.8</target> 
        </configuration> 
       </plugin> 
      </plugins> 
     </build> 
    </project> 

https://github.com/Epooch/CalvanoDesignSource

Die Quelle für diejenigen, die die gesamte Anwendung sehen möchten, wie es auf meiner Maschine ist.

Die Codeblöcke bleiben so wie sie waren für diejenigen, die auf die Probleme stoßen. Ich werde darunter die Änderungen, die ich gemacht habe, um es funktionieren zu lassen.

Arbeitslösung

HelloWorldMB

package com.project.web; 

import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext; 

import com.project.business.HelloWorldBo; 

import java.io.Serializable; 

@ManagedBean 
@SessionScoped 
public class HelloWorldMB implements Serializable { 

    @ManagedProperty(value = "#{helloWorldBo}") 
    private HelloWorldBo helloWorldBo; 

    private static final long serialVersionUID = 1L; 

    private String name; 

    public void init() { 
     System.out.println("HelloWorldMB.init()"); 
     if (helloWorldBo != null) 
      return; 
     System.out.println("helloWorldBo is null"); 
    } 

    public void springTest(ActionEvent e) { 
     // Call the business object to register the user 
     helloWorldBo.springTest(name); 
    } 

    // Set the registrationBo attribute used by Spring 
    public void setHelloWorldBo(HelloWorldBo helloWorldBo) { 
     this.helloWorldBo = helloWorldBo; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

HelloWorldBo

package com.project.business; 

public interface HelloWorldBo { 
    /** 
    * springTest method 
    * @param name 
    */ 
    public void springTest(String name); 
} 

HelloWorldBoImpl

package com.project.business; 
import javax.inject.Named; 

@Named("helloWorldBo") 
public class HelloWorldBoImpl implements HelloWorldBo { 

    /** 
    * Tests the spring and jsf implementation 
    */ 
    @Override 
    public void springTest(String name) { 
     System.out.println("HelloWorldBoImpl:: springTest : " + name); 
    } 
} 

addierten folgende Abhängigkeit der pom.xml

<dependency> 
    <groupId>javax.inject</groupId> 
    <artifactId>javax.inject</artifactId> 
    <version>1</version> 
</dependency> 
+0

Wie Sie Ihren Code bitte – Jens

+1

Bitte lesen [mcve] und http://www.stackoverflow.com/tags/jsf/info – Kukeltje

+0

Ich hatte versehentlich vor dem Anzeigen von Code geschrieben. Das war mein Fehler. – Eric

Antwort

1

Wenn Sie Frühling erwarten, dass die Business-Objekt zu injizieren, müssen Sie JSF zu schaffen, mit irgendeiner Weise die Bohne Verweise aufzulösen. Ihre verwaltete Bean muss das Geschäftsobjekt irgendwo in einer Methode initialisieren, die während des JSF-Lebenszyklus aufgerufen wird.

Zum Beispiel sind hier die relevanten Abschnitte aus einem einfachen Beispiel.

Zuerst müssen Sie den Frühlings-Setup in der Web-Anwendung Deskriptoren:

/WEB-INF/web.xml

<web-app> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/app-service-config.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
    <listener> 
     <listener-class> 
      org.springframework.web.context.request.RequestContextListener 
     </listener-class> 
    </listener> 

/WEB-INF/app-Service-config. xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- Spring 3.1 annotation support --> 
    <context:component-scan base-package="com.rtt.simple.service" /> 

Dann brauchen Sie JSF mit einem Resolver einrichten, die es Spring-beans in seiner ma injizieren lässt naged Bohnen.

DummyBackingBean.java

@ManagedBean 
@ViewScoped 
public class DummyBackingBean implements Serializable { 

@ManagedProperty(value = "#{dummyService}") 
private DummyService dummyService; 

private List<DummyDataItem> dataItems; 

@PostConstruct 
public void postConstruct() { 
    LOG.trace("postConstruct()"); 

    dataItems = dummyService.listAll(); 
} 

public DummyService getDummyService() { 
    return dummyService; 
} 

public void setDummyService(DummyService dummyService) { 
    this.dummyService = dummyService; 
} 

DummyService:

faces-config.xml

<application> 
    <!-- Spring Framework support --> 
    <el-resolver> 
     org.springframework.web.jsf.el.SpringBeanFacesELResolver 
    </el-resolver> 

Dann können Sie eine Spring-Bean in einer verwalteten Bohne injizieren. Java

Beachten Sie, dass ich die @ Named-Annotation von javax.inject verwendet habe, um die Bean in der Spring-Konfiguration zu deklarieren, aber diese Technik funktioniert mit jeder Spring-Injection-Annotation.

+0

Also habe ich versucht, das Geschäftsobjekt zu initialisieren, aber der Frühling erlaubte mir nicht, es zu initialisieren, wie ich es erwartet hätte. Ich habe versucht, die HelloWorldBo HalloWorldBo = new HelloWorldBo(); Das hat nicht funktioniert. – Eric

+0

Ich hatte diese Zeile bereits in meine faces-config.xml aufgenommen und funktioniert immer noch nicht. Ich hatte ursprünglich ein Post-Konstrukt, aber auf meiner Initialisierungsmethode, aber es entfernt, um zu versuchen, mein Problem zu isolieren. – Eric

+0

Ich habe gerade meine Antwort bearbeitet, um die komplette Konfigurationskette für ein einfaches Beispiel zu zeigen. Ich hoffe es hilft! –