0

I would like to pass 4 parameter from view. It looks like this-

<button>
    <a href="/generateDetails/${fromDate}/${toDate}/${shopList.shopId}/${billStatus}" target="_blank" title="Download">Download</a>
</button>

In controller

@RequestMapping(value = "generateDetails/{what to do here}", method = RequestMethod.GET)
private void generateDetails(
    HttpServletRequest request, HttpServletResponse response, 
    @PathVariable("what to do here") String something) {
    //Codes for generate pdf report
}

Please provide me some details about corrections in View and Controller end.

I have tried:

In View End-

<button>
    <a href="/generateDetails/${fromDate}/${toDate}/${shopList.shopId}/${billStatus}" target="_blank" title="Download">Download</a>
</button>

In Controller-

@RequestMapping(value = "generateDetails/{fromDate}/{toDate}/{shopId}/{billStatus}", method = RequestMethod.GET)
private void generateDetails(
    HttpServletRequest request, HttpServletResponse response,
    @PathVariable("fromDate") String fDate, 
    @PathVariable("toDate") String tDate, 
    @PathVariable("shopId") String shopId, 
    @PathVariable("billStatus") String bStatus) {
    //Codes for generate pdf report
}

In view end, after clicking the button it is not working. If i pass single parameter then the button works and controller gets the value.

3
  • 1
    Your button does not work because the default value of field required of object PathVariable is true, hence all of them are required to be non-empty. You can set it up by @PathVariable(required = false). Moreover Have you considered making a RequestBody instead of 4 Path Variables? Would the fromDate and toDate not be better as LocalDate objects instead Strings? Commented Mar 4 at 8:37
  • What error do you see when you click the button? During application startup, have you enabled Spring debug logging and seen what paths are being registered with it? Commented Mar 5 at 16:21
  • Thanks everyone. I have solved the issue. I have used <input> tag within the <form> tag where i used formaction attribute to redirect to the URL in View end. And in Controller end i used @ModelAttribute annotation to get each of the inputted form data. It's working just fine :) Commented Mar 11 at 8:15

1 Answer 1

0

In view

<button onclick="downloadReport()">Download</button>

<script>
function downloadReport() {
    let fromDate = encodeURIComponent('${fromDate}');
    let toDate = encodeURIComponent('${toDate}');
    let shopId = encodeURIComponent('${shopList.shopId}');
    let billStatus = encodeURIComponent('${billStatus}');

    window.open(`/generateDetails/${fromDate}/${toDate}/${shopId}/${billStatus}`, '_blank');
}
</script>

in Controller

@RequestMapping(value = "/generateDetails/{fromDate}/{toDate}/{shopId}/{billStatus}", method = RequestMethod.GET)
    private void generateDetails(
        HttpServletRequest request,
        HttpServletResponse response,
        @PathVariable("fromDate") String fromDate,
        @PathVariable("toDate") String toDate,
        @PathVariable("shopId") String shopId,
        @PathVariable("billStatus") String billStatus
    ) {
        //Codes for generate pdf report
    }

for more security and prevent some issues it's good to use Query Parameters instead path variables

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

1 Comment

Explain what you changed here and why.

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.