SpringMVC - Send nested form in jsp - java

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>

It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>

Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}

You can just make a ajax post call instead of a form submit

Related

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.
}

Different calls from the same JSP page

In a JSP page I use one form to get the entire DB and another form (and the related servlet request and the specific sort command). Once I sort a field, the form with the whole DB become empty, so the two Forms are not working one after another, as I expected.
For the moment I am not sure whether it's a HTML problem or something else, so I put just a second button to get a refresh. However, I assume there must be a smarter & simpler way using jQuery.
Thank you in advance!
<table cellpadding="10">
<form name="Refresh" action="AllPost">
<input type="submit" value="Refresh " /> ¦
</form>
<form name="F" action="SortedPost" id="formular" method="post">
<input type="submit" value="Sort: " />
<select name="Titl">
<c:forEach items="${AllPost}" var="p">
<option> ${p.Item} </option>
</c:forEach>
</select>
</form>
</table>
<table>
<c:forEach items="${SortedPost}" var="p">
<tr>
<td>ID: ${p.id}</td>
<td>bla bla : ${p.blabla}</td>
</tr>
</c:forEach>
</table>
<table>
<c:forEach items="${AllPost}" var="p">
<tr>
<td>${p.id}</td>
<td>bla bla : ${p.blabla}</td>
</tr>
</c:forEach>
</table>

values are not getting display in jsp

I started learning jsp and I have written a form.jsp which contains some fields, and I have another page called formResult.jsp.
I am trying to display the value of form.jsp to formResult.jsp using El(Expression Language).
I dont know what is wrong with my code, the value are not getting populated from form.jsp to formResult.jsp except fName(First Name).
This is very annoying and abnormal. I don't know what to do.
Please help me.
form.jsp
<form action="formResult.jsp">
<fieldset>
<legend>
<font face="Courier New" size="+1"
color="red">
Please enter your information
</font>
</legend>
</fieldset>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fName"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lName"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email">
</tr>
<tr>
<td>Address</td>
<td><textarea rows="3" cols="20" name="address"></textarea>
</tr>
<tr>
<td><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form>
With all the possible way I tried. the bellow three ways are not working.I am just getting the first value
formResult.jsp
<body>
First Name :${param.fName}<br>
Last Name :${param.lName}<br>
Email :${param.email}<br>
Address :${param.address}<br>
First Name :${param["fName"]}<br>
Last Name :${param["lName"]}<br>
Email :${param["email"]}<br>
Address :${param["address"]}<br>
First Name :${param[0]}<br>
Last Name :${param[1]}<br>
Email :${param[2]}<br>
Address :${param[3]}<br>
</body>
Input
Output

How to send values to a servlet from a jsp which has directly retrieved values from another jsp

my motive is data flow from modify.jsp to modifysummary.jsp then to servlet to update details but i dont want to use scriptlets in my jsp page.
modify.jsp
<form action="modifysummary.jsp">
<table border="1">
<tr>
<td><label>FirstName</label></td>
<td><input type="text" name="firstname" value="<%=bean.getFirstname() %>"></td>
</tr>
<tr>
<td><label>Surname</label></td>
<td><input type="text" name="surname" value="<%=bean.getSurname() %>" ></td>
</tr>
</form>
modifysummary.jsp
<table>
<tr >
<th>Employee Details</th>
</tr>
<tr>
<td>First Name</td>
<td><%=request.getParameter("firstname") %></td>
</tr>
<tr>
<td>Surname</td>
<td><%=request.getParameter("surname") %></td>
</tr>
<tr>
<td><input type="button" value="BACK" onclick="javascript:history.go(-1)"></td>
<td><input type="submit" value="Modify" name="Modify"></td>
</tr>
</table>
i have written this code inside a form,on clicking the modify button the data should go to servlet in a bean but how to do that.
i think this is not possible without using scriptlets in your modifysummary.jsp . you must set your username and password into request attributes
using request.setattribute();
and get them in servlet
using request.getattribute();
why you don't want to use scriptlets.
and if you want to use requestdispatcher you have to write all that in scriptlets

Linking HTML to JSP page

I'm have problems linking my HTML and JSP. Just trying to make a simple html page to take a "email" and "password" then have the jsp display on the next page. But when i click submit the parameters dont get passed i just get "null", any ideas where im going wrong?
HTML Page
<html>
<head>
<title>Enter your email and password</title>
</head>
<body>
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" email=email></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" password=password></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP Page
<html>
<body>
<%
// get info from request
String email = request.getParameter("email");
String password = request.getParameter("password");
%>
<p>Here is the info you provided:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Email:</td>
<td><%= email %></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><%= password %></td>
</tr>
</table>
<form action= "Next" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
You need to use the name attribute in the form fields .
<input type="text" name="email"></td>
The value of "email" can be retrieved in JSP as :
String email = request.getParameter("email");
OR
${param.email}
OR
<c:out value="${param.email}"/> <%--(Using JSTL)--%>
Here are the complete list of attributes.
name = string #
The name part of the name/value pair associated with this element for the purposes of form submission.
You should specify the input name attribute. Try this,
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
For more info you can try this link

Categories