Issue
<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.
Solution
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.
}
Answered By - hooknc