JSF 2.1 Spring 3.0集成

时间:2022-05-27 20:03:32

I'm trying to make very simple Spring 3 + JSF2.1 integration according to examples I googled in the web.

我正在尝试根据我在网络上搜索的示例进行非常简单的Spring 3 + JSF2.1集成。

So here is my code:

所以这是我的代码:

My HTML submitted to actionController.actionSubmitted() method:

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

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

now the Action Controller itself:

现在是Action Controller本身:

@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";
    }
}

Now my bean Spring:

现在我的豆春:

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." );

    }
}

This is my web.xml

这是我的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>

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

my faces-config.xml is the following:

我的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>

My Problem is that I'm getting NullPointerExeption because my mailService Spring bean is null.

我的问题是我得到NullPointerExeption,因为我的mailService Spring bean为null。

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

1 个解决方案

#1


0  

I added getter for mail servie and the problem was solved. I do't know why this getter is required, but it works.

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

#1


0  

I added getter for mail servie and the problem was solved. I do't know why this getter is required, but it works.

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