I am new to jsp and spring MVC.
I had a jsp which display students list to take attendance with a checkbox in the form.
The Students jsp contains student details from student table and checkbox is from attendance table and my jsp page is like this
Jsp Code
<tr>
<th width="80">Student No</th>
<th width="120">Student Name</th>
<th width="120">Father Name</th>
<th width="60">Mobile No</th>
<th width="60">Present</th>
</tr>
<c:if test="${!empty studentsList}">
<c:forEach items="${studentsList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.studentName}</td>
<td>${student.fatherName}</td>
<td>${student.mobileNo}</td>
<td><form:checkbox path="attendance.presentFlag" /></td>
</c:forEach>
</c:if>
</table>
<div align="center">
<tr>
<td></td>
<td><input type="submit" value="Present" style="margin-top: 12px;"/></td>
</tr>
</div>
</form:form>
Above jsp will display list of students with a checkbox.
Now I want to submit the modified list in jsp to spring MVC controller to store in database based on the checkbox checked flag, can any one please suggest me how to overcome jsp form submission to spring mvc, I am unable to get what to do. I am able to call a method of controller what I have do for list submission.
Create a button instead of submit and call a java script function and then submit the form after applying your logic.
You need few things inside form tag:
method="GET/POST"
action="${request.contextPath}/requestMapping"
modelAttribute="attendance"
<form:checkbox path="presentFlag" /> -> will autobind value if attendance object have array of booleans "presentFlag"
Submit name and value determine request parameter with name and value respectively.
Your Spring controller should have request mapping same as in action, as well as request parameter and model attribute "attendance".
https://www.mkyong.com/spring-mvc/spring-mvc-checkbox-and-checkboxes-example/
Related
There are 2 tables here:
The table below is using purely container to display populate the tables from database whereas the one above is using datatable.
However i wish to duplicate the last column of displaying a icon menu for edit and delete actions which is contained inside actions.jsp inside the table above.
This is my partial code in view.jsp displaying the datatables.
<%
List<Guestbook> guestbookList = GuestbookLocalServiceUtil.getGuestbooks(scopeGroupId);
request.setAttribute("guestbookList", guestbookList);
%>
<table id="gbdb" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${guestbookList}" var="guestbook">
<tr>
<td>${guestbook.name}</td>
<td>${guestbook.status}</td>
<td><jsp:include page="${actions.jsp}" flush="true" /></td>
</tr>
</c:forEach>
</tbody>
</table>
However as you can see my function of diplaying actions.jsp is not similar to the method for using container where i can just referenced the path using
<td> <liferay-ui:search-container-column-jsp
align="right"
path="/guestbookadminportlet/guestbook_actions.jsp" /></td>
Please help me with the correct way of displaying by referencing to actions.jsp
You should not include whole jsp as an action column if using jQuery data-table,
Let's assume you need to add Delete book action so your td should be replace by following code.
<td>
<portlet:actionURL name="<%=DELETE_BOOK%>" var="deleteBookUrl">
<portlet:param name="bookId" value="${book.id}"/>
</portlet:actionURL>
<input type="button" onclick="location.href='${deleteBookUrl}'" value="Delete"/>
</td>
Hope this will help you.
so basically to lay this out I have
AffidavitController - Controller
Affidavit.jsp - GET view page
summary.jsp - POST view page
so basically I have a form that a user fills out and then they hit the submit button and it posts it.
This method is then called in the controller
#RequestMapping(value = "/affidavit", method = RequestMethod.POST)
public String post(AffidavitDetailDto affidavitDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
now within this method I catch the information that was passed over in the response object.
an example of this is I have a name field on the GET view page and the name of this field is 'custName'
so I catch it like so
String contactName = request.getParameter("custName");
model.addAttribute("contactName", contactName);
and then I can display it on my POST view page
<p>Name: ${contactName}</p>
but now I am trying to figure out how I can do the same with a table and treat it like an array
the following is how it is added on the GET view page
<table id="certEmail" name="certEmail">
<tr>
<td>${user.email}</td>
</tr>
</table>
the following is what I have attempted
String[] certEmail = request.getParameterValues("certEmail");
model.addAttribute("certEmail", certEmail);
and this is what I have on the view page to display it
<div id="Emails">
<c:forEach items="${certEmail}" var="email">
<p>${email}</p>
</c:forEach>
</div>
this does not currently work. I know I am probably just doing it wrong so can someone please at least point me in right direction. I just haven't figured out a good google query yet to get an answer.
Table is not a form element (https://www.w3schools.com/html/html_form_elements.asp). Only form element values on form are submitted to server. So instead of
<table id="certEmail" name="certEmail">
<tr>
<td>${user.email}</td>
</tr>
</table>
You should have something like
<table id="certEmail">
<tr>
<td>
<input type="text" name="certEmail" value="${user.email}" />
</td>
</tr>
</table>
if you want your data to be editable, or
<table id="certEmail">
<tr>
<td>
${user.email}
<input type="hidden" name="certEmail" value="${user.email}" />
</td>
</tr>
</table>
if you want it to be hidden and just resent to the server.
While I should denote that second variant generally should be avoided. I would recommend you to pass the user id and use it in back-end to load user and read his email.
Please learn how to use HTML forms: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Your_first_HTML_form
i have three servlet. In first servlet i have Request Dispatcher so it is redirect to my Dynamic List.jsp file. The jsp file contains
<div class='feedItem'> <c:out value="${feed.tagRuleType}"> </c:out> </div>
<div class='feedItem'><input type="submit" value="Update" onClick="updateRows(${feed.configId})"></div>
<div class='feedItem'><input type="submit" value="Delete" onClick="deleteRows(${feed.feedId})"></div>
When i click on the update button i can able to call the servlet through javascript. Inside servlet i am using Request Dispatcher:
RequestDispatcher dispatcher = request.getRequestDispatcher("/FeedUpdate.jsp");
dispatcher.forward(request, response);
But it is not redirecting to FeedUpdate.jsp file
It is iterating the list object. Inside the tag i added update button. so when i click update button corresponding row id i will get for updating a record.
When i click the button i can able to call servlet and the get the id. when i redirect to the jsp page using Request Dispatcher inside the servlet it is not redirecting to jsp page.
Can anyone the give the suggestion why it is not redirecting to jsp file.
if you have used Jquery Ajax call to call your servlet, your dispatcher forward won't work. You may want do the redirect in the success method of Ajax:
jQuery.ajax({
type:"POST",
url : "servlet",
data : param,
success : function(data) {
windows.location.href='/myservlet?id='+data;
});
So basically you want to call other method in another JSP and takes id as the parameter, if yes, then you can do it this way :
<h3>Sections List</h3>
<c:if test="${!empty listSections}">
<table class="tg">
<tr>
<th width="80">Section ID</th>
<th width="120">Section name</th>
<th width="120">Section size</th>
<th width="120">Section position</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listSections}" var="section">
<tr>
<td>${section.sectionid}</td>
<td>${section.sectionname}</td>
<td>${section.sectionsize}</td>
<td>${section.sectionposition}</td>
<td><a href="<c:url value='/editsection/${section.sectionid}' />" >Edit</a></td>
<td><a href="<c:url value='/removesection/${section.sectionid}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
The method editSetion and RemoveSection can be in different JSP files as well, depends how you have mapped it. Is this what you are looking for? I cannot understand exactly what you want, as you have not provided much information, no code too.. Please add more code and properly explain if this is not what you expected.
I am working on a small Struts 1.2.4 Phonebook app.
The app contains forms in a single JSP to add , search , and edit/delete the data. My problem lies in displaying the ResultSet which is Obtained on performing a wildcard search. Here's the flow of the program :
User enters a search string.
Request is passed to a DispatchAction class's search() method , which in turn calls a searchContact(String name , HttpServletRequest request) method of a 'model' class.
The method executes SELECT query on the DB using a PreparedStatement, which returns a resultset, the values of which are put in an ArrayList< PhoneBook >. The class PhoneBook is a JavaBean with two fields, 'c_name' and 'p_num' , with getters and setters.
logic:bean and logic:iterate tags are used to print the contents of the ArrayList< PhoneBook > in the JSP.
In the JSP :
<html:form action="/PhoneBookAction.do" method="post" >
<table>
<tr>
<td>Name</td>
<td><html:text property="name"/></td>
</tr>
</table>
<br>
<html:submit property="method" value="Search"/>
<br><br><br>
<table align="center" border="2" bordercolor="royal blue">
<thead>
<tr>
<td><b> Select </b></td>
<td><b> Name </b></td>
<td><b> Phone Number </b></td>
</tr>
</thead>
<logic:iterate id="search_resId" name="phonebookform" property="search_res">
<tr>
<td> <bean:write name="search_resId" property="c_name" /> </td>
<td> <bean:write name="search_resId" property="p_num" /> </td>
</tr>
</logic:iterate>
</table>
</html:form>
This usually gives a "Bean not found in any scope" error, or prints nothing. I tried the fixes suggested in some similar threads, But none of them seem to work. Looking forward to a solution. Thank you.
I have a JSP that presents a list of customers (ArrayList searchResults). I want to be able to pick one of those, and submit it to a Spring MVC controller. However, it appears that I cannot pass the selected object, only a property of it, such as customerId. I really need to pass the entire object.
Is there a standard way to do this in Spring 3.x?
<c:forEach items="${searchResults}" var="searchResult">
<tr>
<td><c:out value="${searchResult.customerId}" /></td>
<td><c:out value="${searchResult.firstName}" /></td>
<td><c:out value="${searchResult.lastName}" /></td>
<td>
<form method="POST" ACTION="./customercare">
<input type="SUBMIT" value="Select This Customer"/>
<input type="hidden" name ="searchResult" value="${searchResult}"/>
</form>
</td>
</tr>
</c:forEach>
You can use Spring's form taglib instead of plain <form> to post back to a Spring MVC Controller and then it will Bind the values back to the model you specify.
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
...
#RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(#ModelAttribute("contact")
Contact contact, BindingResult result) {
See this Post: http://viralpatel.net/blogs/spring-3-mvc-handling-forms/
You might want to consider giving id to each and nested tags to differentiate between row that you want to POST
<c:forEach items="${searchResults}" var="searchResult">
<tr>
....
<form:form id="${searchResults.key}-form" method="POST" ACTION="./customercare">
<form:input id="${searchResults.key}-btn" type="SUBMIT" value="Select This Customer"/>
<form:input id="${searchResults.key}-hidden" type="hidden" name ="${searchResults.key}" value="searchResult['${searchResults.key}']"/>
</form:form>
</tr>
On the backend side you will have to write the controller as suggested by #PatBurke
You could have 1 form per customer with a lot of hidden inputs in. When that customer is selected you can POST that form. Spring can then bind all the hidden inputs to your customer object.
(Generally, I would just send the id only and load the customer info, as an entity, from a Database. However, I assume you must have a good reason for not wanting to do this)