使用getParameterValues将日期从jsp传递到servlet

时间:2016-04-14 03:27:46

标签: java jsp date datetime servlets

我在servlet中有错误从JSP传递值..

JSP代码:

<table>                 
<tr>
    <td>Day</td>
    <td>Start</td>
    <td>End</td>
    <td>Date</td>
    <td>&nbsp;</td>                                                          
</tr>               
<tr>
   <td><select name="availableDay">  
       <!--Listing days-->                                                                                         
       </select></td>
   <td><input type="time"   name="availableStart"/></td>
   <td><input type="time"   name="availableEnd"/></td>
   <td><input type="date"   name="availableDate" /></td>
   <td><input type="button" class="add" name="action" value="Add More"</td>
</tr>
</table>

我想将availableDate传递给servlet。 Fyi,JSP中的行是动态生成的。因此,我使用[]传递。我的servlet代码:

  public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {

  String[] presentationID = request.getParameterValues("selectavailability");
  String[] availableDay =  request.getParameterValues("availableDay"); 
  String[] availableStart =  request.getParameterValues("availableStart");
  String[] availableEnd =  request.getParameterValues("availableEnd");
  String[] availableDate = request.getParameterValues("availableDate");  

  SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
  Date chosenDate = availDate.parse(availableDate);// THIS IS WHERE I AM GETTING ERROR

  try {

        if(availableDay != null && availableStart != null && availableEnd != null)
        {
          for (int i = 0; i < availableDay.length; i++) 
          {

            AvailabilityBean available = new  AvailabilityBean(); 
            available.setLecturerID(request.getParameter("lecturerID"));
            available.setAvailableDay(availableDay[i]);
            available.setAvailableStart(availableStart[i]);
            available.setAvailableEnd(availableEnd[i]);
            available.setAvailableDate(availableDate[i]); //EFFECTED THIS LINE TOO

            available = AddAvailableDAO.addavailable(available);


           }
        }

     }
                   response.sendRedirect("addAvailability.jsp");                
 }
catch (Throwable theException) {
            System.out.println("hhhhhhh"+theException);
        }
    }

对于额外的,AvailabilityBean:

    private String availableID;
    private String lecturerID;
    private String availableDay;
    private String availableStart;
    private String availableEnd;
    private Date availableDate;
    private String presentationID;

错误:incompatible types: String[] cannot be converted to String.

你能告诉我我的错误在哪里吗?我该如何解决这个问题?谢谢

2 个答案:

答案 0 :(得分:1)

以下是您的availableDate变量,它是String的数组。

 String[] availableDate = request.getParameterValues("availableDate");

现在,您正在使用SimpleDateFormat类将String数据格式化为Date

此处parse(String text)将采用String参数而不是String[](数组)的类型。

 SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
  Date chosenDate = availDate.parse(availableDate);// availableDate is a String Array not a String.

因此,它会引发compile-time错误incompatible types: String[] cannot be converted to String.

你应该尝试这个

 Date chosenDate = availDate.parse(availableDate[0]); //preferred index you may pass as per your requirement.
  

注意: - availableDate[0]将返回索引为String的{​​{1}}对象。

答案 1 :(得分:0)

这是您的JSP代码示例..

<td><select name="availableDay">  //only one value will get selected so. use getParameterValue() for this field too.
       <!--Listing days-->                                                                                         
       </select></td>

   //this will return only a String not a String[] . So use getParameterValue();
   <td><input type="time"   name="availableStart"/></td> 

    //this will also return only a String not a String[] . So use getParameterValue();
   <td><input type="time"   name="availableEnd"/></td> 

    //this too will return only a String not a String[] . So use getParameterValue();
   <td><input type="date"   name="availableDate" /></td> //this too will return a String with getParameterValue();

所以,试试吧..代码。

  String availableDay =  request.getParameter("availableDay"); 
  String availableStart =  request.getParameter("availableStart");
  String availableEnd =  request.getParameter("availableEnd");
  String availableDate = request.getParameter("availableDate");  

而不是这一个。

 String[] availableDay =  request.getParameterValues("availableDay"); 
  String[] availableStart =  request.getParameterValues("availableStart");
  String[] availableEnd =  request.getParameterValues("availableEnd");
  String[] availableDate = request.getParameterValues("availableDate");