简体   繁体   中英

How to generate a download request from html page to controller of spring mvc

 <html> <head> <title>Student Registration</title> </head> <body> <h3 align="center">New Trainee Registration</h3> <table align="center" cellpadding="10"> <!-- First Name --> <tr> <td>First Name</td> <td>${traineeData.fName}</td> </tr> <!-- Last Name --> <tr> <td>Last Name</td> <td>${traineeData.lName}</td> </tr> <!-- Father's Name --> <tr> <td>Father's Name</td> <td>${traineeData.fatherName}</td> </tr> <!-- Mobile Number --> <tr> <td>Mobile No.</td> <td>${traineeData.mobile}</td> </tr> <tr> <td>Joining Date</td> <td>${traineeData.joiningDate}></td> </tr> <tr> <td>Grade</td> <td>${traineeData.grade}</td> </tr> <tr> <td>Gender</td> <td>${traineeData.gender}</td> </tr> <!-- Course --> <tr> <td>COURSES<br />APPLIED FOR </td> <td>${traineeData.course}</td> </tr> <tr> <td>Joining Date</td> <td>${traineeData.joiningDate}</td> </tr> <!-- Submit and Reset --> <tr> <td colspan="2" align="center"> <button type="button" >Generate Certificate</button> </td> </tr> </table> </body> </html> 

Let's say, I have a download button on my HTML page. when clicking on this button a request goes through the controller of the spring MVC framework along with "enrollmentNo".

On Controller class, I will use this enrollmentNo.

I have a class which will generate a certificate for the student by fetching data using the student's enrollmentNo.

and response goes back and a file will download(which a pdf file generated by my pre-created class)

I have tried with many ways but not get what I want.

@RequestMapping(value = "/generateCertificate")
public void generateCertificate(@RequestParam("traineeData.enrollmentNo") int enrollmentNo) {

    System.out.println("you in /generateCertificate");

    // I don't know what should I return... 
    // please change return type accordingly...
}

I want a button on my HTML page named "Generate certificate". The code should be easy and normal, Ajax can be used here.

Your button isn't actually doing anything, useful.

Try this code around your button...

<tr>
    <td colspan="2" align="center">
        <form action="/generateCertificate" method="GET">
            <input type="hidden" name="enrollmentNo" value="${traineeData.enrollmentNo}"
            <button type="submit">Generate Certificate</button>
        </form>
    </td>
</tr>

Then your controller will need to changed slightly...

@RequestMapping(value = "/generateCertificate")
public void generateCertificate(@RequestParam("enrollmentNo") int enrollmentNo) {

    // The @RequestParam was changed to the name of the param from the form and not the object/value.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM