0

我正在尝试将 JSF 与 spring 和 hibernate 集成。

以下是我的代码:

LeaveBean.java

   package com.nagra.bean;

    import java.io.Serializable;
       import java.sql.SQLException;
     import java.text.DateFormat;
     import java.text.SimpleDateFormat;
     import java.util.ArrayList;
    import java.util.Date;
      import java.util.List;
  import java.util.Properties;

   import javax.annotation.Resource;
    import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
   import javax.mail.Message;
      import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
     import javax.mail.internet.MimeMessage;

  import org.hibernate.HibernateException;

     import com.nagra.leaveapp.BO.LeaveInfoBo;
   import com.nagra.leaveapp.model.LeaveInfo;


  @ManagedBean(name="customer")
       @SessionScoped
     public class LeaveBean implements Serializable{

/**
 * 
 */
//private LeaveAppDo leaveAppDo=new LeaveAppDo();
LeaveInfoBo leaveinfoBo;

public LeaveInfoBo getLeaveinfoBo() {
    return leaveinfoBo;
}
public void setLeaveinfoBo(LeaveInfoBo leaveinfoBo) {
    this.leaveinfoBo = leaveinfoBo;
}

private String total_sick_leave;
private String total_paidoff_leave;
private String start_date;
private String end_date;
private String reason;
private String status;
private Date secondDate;

public Date getSecondDate() {
    return secondDate;
}
public void setSecondDate(Date secondDate) {
    this.secondDate = secondDate;
}
public String getTotal_sick_leave() {
    return total_sick_leave;
}
public void setTotal_sick_leave(String total_sick_leave) {
    this.total_sick_leave = total_sick_leave;
}
public String getTotal_paidoff_leave() {
    return total_paidoff_leave;
}
public void setTotal_paidoff_leave(String total_paidoff_leave) {
    this.total_paidoff_leave = total_paidoff_leave;
}
public String getStart_date() {
    return start_date;
}
public void setStart_date(String start_date) {
    this.start_date = start_date;
}
public String getEnd_date() {
    return end_date;
}
public void setEnd_date(String end_date) {
    this.end_date = end_date;
}
public String getReason() {
    return reason;
}
public void setReason(String reason) {
    this.reason = reason;
}
public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

private static final long serialVersionUID = 1L;
//resource injection
@Resource(name="jdbc/leaveapp")
//private DataSource ds;

//if resource inject is not support, you still can get it manually.
/*public CustomerBean(){
    try {
        Context ctx = new InitialContext();
        ds = (DataSource)ctx.lookup("jdbc:mysql://localhost:3306/leaveapp");
    } catch (NamingException e) {
        e.printStackTrace();
    }

}*/


public List<LeaveInfo> getCustomerList() throws SQLException{
    System.out.println("Getting In " );

    return null;

    //  return leaveinfoBo.retrieveData();

}

public String addCustomer() throws HibernateException 
{
    /* String url = "jdbc:mysql://localhost:3306/leaveapp"; 
    Connection con = DriverManager.getConnection(url,"root","root");

    System.out.println(con);

    if(con==null)
        throw new SQLException("Can't get database connection");
    String sql="insert into leaveinformation(start_date,end_date,reason,status)values(?,?,?,?)";
    java.util.Date utilDate = new Date();
    // Convert it to java.sql.Date
    java.sql.Date date = new java.sql.Date(utilDate.getTime());
     java.sql.Timestamp  sqlDate = new java.sql.Timestamp(new java.util.Date().getTime());*/
     DateFormat formatter ; 
      try
      {
         System.out.println("Start date " + getStart_date());
      formatter = new SimpleDateFormat("yyyy-MM-dd");
      Date startDate = formatter.parse(LeaveBean.this.getStart_date());
      System.out.println(startDate);

      Date endDate = formatter.parse(LeaveBean.this.getEnd_date());

//    Days d = Days.daysBetween(startDate,endDate);
    //  int days = d.getDays();
      @SuppressWarnings("deprecation")
    java.sql.Date startdate=new java.sql.Date(startDate.getDate());
      @SuppressWarnings("deprecation")
    java.sql.Date enddate=new java.sql.Date(endDate.getDate());
    /* System.out.println("Today is " +date );
     PreparedStatement ps 
        = con.prepareStatement(sql);
    ps.setDate(1,startdate,Calendar.getInstance(Locale.ENGLISH));
    ps.setDate(2,enddate,Calendar.getInstance(Locale.ENGLISH));
    ps.setString(3,CustomerBean.this.reason);
    ps.setString(4,"Waiting");*/
      LeaveInfo leave = new LeaveInfo();
      System.out.println("Entering addCustomer()  ");
      leave.setEnd_date(enddate);
      leave.setStart_date(startdate);
      leave.setReason(reason);
      leave.setStatus("Waiting");
     System.out.println(leave.toString());
     leaveinfoBo.save(leave);
     clearall();
    // Insert the row
  /*  ps.executeUpdate();
    con.close();*/

     sendmail();

      }
      catch(Exception e)
      {
          System.out.println("There is an error " + e.getStackTrace().toString());
          e.printStackTrace();
      }
      return "";
}

 private void clearall() {
    // TODO Auto-generated method stub
    setEnd_date("");
    setStart_date("");  
    setReason("");
    setStatus("");
}
public String sendmail()
 {
        // Recipient's email ID needs to be mentioned.
     String to = "nsbharathi88@gmail.com";

     // Sender's email ID needs to be mentioned
     String from = "web@gmail.com";

     // Assuming you are sending email from localhost
     String host = "localhost";

     // Get system properties
     Properties properties = System.getProperties();

     // Setup mail server
     properties.setProperty("mail.smtp.host", host);

     // Get the default Session object.
     Session session = Session.getDefaultInstance(properties);

     try{
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");

        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
     }catch (MessagingException mex) {
        mex.printStackTrace();
     }
     return "";
  }

 }

应用程序上下文.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-2.5.xsd">

<!-- Database Configuration -->
<import resource="classes/spring/database/DataSource.xml"/>
<import resource="classes/spring/database/Hibernate.xml"/>

<!-- Beans Declaration -->
<import resource="classes/spring/bean/LeaveApp.xml"/>

  </beans>

当我在 tomcat 服务器上运行它时出现以下错误:

            Start date 2012-02-03
            java.lang.NullPointerException
       at com.nagra.bean.LeaveBean.addCustomer(LeaveBean.java:167)
            Fri Feb 03 00:00:00 IST 2012
         Entering addCustomer()  
     LeaveInfo [id=0, total_sick_leave=15, total_paidoff_leave=15, start_date=1970-01- 01, end_date=1970-01-01, reason=cfklsn, status=Waiting]
    There is an error [Ljava.lang.StackTraceElement;@fc644a
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at  com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
4

2 回答 2

0

我认为,您的 startdate 和 enddate 是 String 类型,将其更改为 java.util.Date 并尝试

于 2012-06-25T07:08:33.070 回答
0

开始日期=1970-01-01

您的开始日期中有一个空格。它不会解析。

于 2012-06-25T09:02:22.590 回答