0

There is a field on webpage

<form:input id="startDate" onkeydown="return false;" readonly="readonly" path = "startDate"/>

And there is an anchor tag

<a id="excelDownload" target ="reportExcel" href ="downloadFIReport.do?startDate='start_date_goes_here'">Download As Excel</a>

How can i get "start_date_goes_here".

This date must be the date while page load ,not the one changed after page load.

Do i have to add this date using model.addAttribute("") ? Or there is some another way ?

3

2 Answers 2

1

Use can use CustomDateEditor. First, the CustomDateEditor is created, that converts a String to java.util.Date. The format that will be used to parse the String to Date is also defined inside the CustomDateEditor bean definition.

<bean id="dateEditor"
    class="org.springframework.beans.propertyeditors.CustomDateEditor">

    <constructor-arg>
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg value="yyyy-MM-dd-HH:mm:ss;z" />
        </bean>
    </constructor-arg>
    <constructor-arg value="true" />

</bean>

Then the CustomEditorConfigurer is created to register the CustomDateEditor. It has a property named customEditors, that consists of a map with a java.util.Date key and a value that is a reference to the CustomDateEditor bean.

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date">
                    <ref local="dateEditor" />
                </entry>
            </map>
        </property>
    </bean>

Now in initBinder method use it

binder.registerCustomEditor(Date.class,dateEditor);
super.initBinder(request, binder);

UPDATE: Or if you just hate that configuration in xml file, you can directly register CustomDateEditor like this in InitBinder. This method should be written in controller.:

@InitBinder
public void initBinder( WebDataBinder binder){

     SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM/dd/yyyy");
            simpleDateFormat.setLenient(false);
            binder.registerCustomEditor( Date.class, new CustomDateEditor( simpleDateFormat,false));     

}
Sign up to request clarification or add additional context in comments.

4 Comments

Where exactly i am supposed to write initBinder ?
Which Spring version you are using?
I am using Spring MVC 3.0
check the updated answer. It will work if Controller expects date to come.
1

In spring MVC the model is where the controller puts anything that will be used by the view.

As you want to use the date :

  • in a link
  • with its initial value

the simpler way it to put in in the model, and directly use it in the form.

Controller

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

@RequestMapping(...)
public String show_the_view(Model model) {
    // get and format the date
    model.addAttribute("startdate", dateFormat.format(new Date());
    ...
    return "view_name";
}

View :

<a id="excelDownload" target ="reportExcel"
      href ="downloadFIReport.do?startDate='${startdate}'">Download As Excel</a>

EDIT

With the code you gave in comment (and that you should have given directly in question ...), provided fiReportSearchInput.getStartDate() is a java.util.Date , it would give :

@RequestMapping(value = "downloadFIReport.do", method = RequestMethod.GET)
public void downloadFIBill(ModelMap model,
        @ModelAttribute("SpringWeb") FIReportSearchInput fiReportSearchInput,
        HttpServletResponse response) {
    System.out.println(fiReportSearchInput.getStartDate() + " "
        + fiReportSearchInput.getEndDate());
    ModelMap.addAttribute("startdate",
        dateFormat.format(fiReportSearchInput.getStartDate());
    ...
}

1 Comment

Hi this value is then sent to a controller where startDate is expected to be a java.util.Date. Not sure if it will work in that case. Here is the controllers Code. @RequestMapping(value = "downloadFIReport.do", method = RequestMethod.GET) public void downloadFIBill(ModelMap model,@ModelAttribute("SpringWeb") FIReportSearchInput fiReportSearchInput,HttpServletResponse response) { System.out.println(fiReportSearchInput.getStartDate() + " " + fiReportSearchInput.getEndDate());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.