2011-03-12 55 views
0

我试图根据我在网络上搜索的例子做出非常简单的Spring 3 + JSF2.1集成。JSF 2.1 Spring 3.0集成

因此,这里是我的代码:

我提交actionController.actionSubmitted HTML()方法:

<h:form> 
<h:message for="textPanel" style="color:red;" /> 
    <h:panelGrid columns="3" rows="5" id="textPanel"> 
     //all my bean prperties mapped to HTML code. 
    </h:panelGrid> 
    <h:commandButton value="Submit" action="#{actionController.actionSubmitted}" /> 


</h:form> 

现在的动作控制器本身:

@ManagedBean(name="actionController") 
@SessionScoped 
public class ActionController implements Serializable{ 

    @ManagedProperty(value="#{user}") 
    User user; 

    @ManagedProperty(value="#{mailService}") 
    MailService mailService; 

    public void setMailService(MailService mailService) { 
     this.mailService = mailService; 
    } 
    public void setUser(User user) { 
     this.user = user; 
    } 
    private static final long serialVersionUID = 1L; 
    public ActionController() {} 

    public String actionSubmitted(){ 
     System.out.println(user.getEmail()); 
    mailService.sendUserMail(user); 
     return "success"; 
    } 
} 

现在我的豆春季:

public interface MailService { 
    void sendUserMail(User user); 
} 

public class MailServiceImpl implements MailService{ 

    @Override 
    public void sendUserMail(User user) { 
     System.out.println("Mail to "+user.getEmail()+" sent."); 

    } 
} 

这是我的web.xml

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
    </listener> 
    <listener> 
    <listener-class> 
     org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
    </listener> 

    <!-- Welcome page --> 
    <welcome-file-list> 
    <welcome-file>index.xhtml</welcome-file> 
    </welcome-file-list> 

    <!-- JSF mapping --> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

我的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
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-3.0.xsd"> 

    <bean id="mailService" class="com.vanilla.jsf.services.MailServiceImpl"> 
    </bean> 

</beans> 

我faces-config.xml中如下:

<application> 
      <el-resolver> 
       org.springframework.web.jsf.el.SpringBeanFacesELResolver 
       </el-resolver> 
     <message-bundle> 
     com.vanilla.jsf.validators.MyMessages 
     </message-bundle> 
    </application> 
     <managed-bean> 
     <managed-bean-name>actionController</managed-bean-name> 
     <managed-bean-class>com.vanilla.jsf.controllers.ActionController</managed-bean-class> 
     <managed-bean-scope>session</managed-bean-scope> 
     <managed-property> 
      <property-name>mailService</property-name> 
      <value>#{mailService}</value> 
     </managed-property> 
    </managed-bean> 

    <navigation-rule> 
    <from-view-id>index.xhtml</from-view-id> 
    <navigation-case> 
     <from-action>#{actionController.actionSubmitted}</from-action> 
     <from-outcome>success</from-outcome> 
     <to-view-id>submitted.xhtml</to-view-id> 
     <redirect /> 
    </navigation-case> 
    </navigation-rule> 

我的问题是,我获取NullPointerExeption是因为我的mailService Spring bean是null。

public String actionSubmitted(){ 
    System.out.println(user.getEmail()); 
//mailService is null Getting NullPointerException 
mailService.sendUserMail(user); 
    return "success"; 
} 

回答

0

我为邮件服务添加了getter,问题解决了。我不知道为什么这个吸气剂是必需的,但它工作。

+0

你好,请你给我样品链接,我想做类似的整合。 –

+0

@Jsword,没问题:http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/ –

+0

如果getter为abscent,则VariableResolver找不到'属性'。 –