最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to pass multiple parameter from view to controller using @PathVariable? - Stack Overflow

programmeradmin2浏览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.

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.

Share edited Mar 5 at 16:13 Doug Breaux 5,1051 gold badge27 silver badges68 bronze badges asked Mar 4 at 8:28 R1f4tR1f4t 11 bronze badge 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? – Marius 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? – Doug Breaux 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 :) – R1f4t Commented Mar 11 at 8:15
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论