0

我有一个使用速度生成的 .jspa 页面,其中包含用户填写的表单,我需要从表单中获取数据并在 Java 中使用它进行计算,但我不知道如何去做。我可以使用任何 Java、HTML、Velocity 和 Javascript,但仅此而已。以下是与我的问题相关的代码段:

速度/HTML:

<form name="dates">
  <table cellspacing= "3">
    <td>Start Date: <input type="text" name="startdate" size="10" maxlength="10" value = $action.convertDateToString($action.getStartDate())></td>
    <td>Work Days: <input type="text" name="workdays" size="6" maxlength="4" value = $action.getWorkDays()></td>
    <td>End Date: <input type="text" name="enddate" size="10" maxlength="10" value = $action.convertDateToString($action.getEndDate())></td>
  </table>
</form>

爪哇:

//converts a Date variable to a String format to be used for display
public String convertDateToString(Date d){
    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
    return formatter.format(d);
}

public int getWorkDays(){
    //needs to get the user entered value for number of works days to use when calculating the end date
    return 10;
}

public Date getEndDate(){
    //calculates the end date based on the number of work days given and the start date
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(getStartDate());
    int duration = getWorkDays();

    for (int i = 1; i < duration; i++) {
        startCal.add(Calendar.DAY_OF_MONTH, 1);
        //loop through by number of work days, skipping Saturday and Sunday
        while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
          startCal.add(Calendar.DAY_OF_MONTH, 1);
      }

      return startCal.getTime();
}

public Date getStartDate(){
    setCurrProj();
    Date startDate = version.getReleaseDate();
    return startDate;
}

所以我有一个表格,其中开始日期在 Java 中计算,工作日数由用户输入,然后将该值发送回 Java 以计算结束日期。我不确定如何从速度中获取数据并将其发送回 Java。最好在输入工作日数后立即(即自动)完成计算,但如果需要“计算”按钮,它也可以工作。我对 Java 比较熟悉,之前没有用 Velocity 或 JSP 做过很多 Web 开发。

4

1 回答 1

0

Java 和 Velocity 页面在服务器端执行。当用户在表单中输入内容时,它发生在浏览器中,在 Java/Velocity 代码完成生成 HTML 页面很久之后。

因此,您需要在 JavaScript 中完全计算结束日期,或者使用 AJAX 请求将 startDate 和天数发送到服务器,在 Java 中计算结束日期,将其发送到响应,从响应中获取此日期AJAX 响应处理程序,并在页面中更新它。

于 2012-07-27T19:30:27.473 回答