簡體   English   中英

JSF 2.1 Spring 3.0集成

[英]JSF 2.1 Spring 3.0 Integration

根據我在網上搜索的示例,我試圖使Spring 3 + JSF2.1變得非常簡單。

所以這是我的代碼:

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

現在,動作控制器本身:

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

我的問題是我收到NullPointerException因為我的mailService Spring bean為null。

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

我為郵件服務添加了getter,此問題已解決。 我不知道為什么需要此吸氣劑,但是它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM