I am using this tutorial
I see delete functionality for each row:
7.6 Dynamic fields
Thanks to the advanced form-field binding capabilities in Spring MVC,
we can use complex Spring EL expressions to bind dynamic form fields
to our form-backing bean. This will allow us to create new Row objects
in our SeedStarter bean, and to add those rows’ fields to our form at
user request.
In order to do this, we will need a couple of new mapped methods in
our controller, which will add or remove a row from our SeedStarter
depending on the existence of specific request parameters:
#RequestMapping(value="/seedstartermng", params={"addRow"})
public String addRow(final SeedStarter seedStarter, final BindingResult bindingResult) {
seedStarter.getRows().add(new Row());
return "seedstartermng";
}
#RequestMapping(value="/seedstartermng", params={"removeRow"})
public String removeRow(
final SeedStarter seedStarter, final BindingResult bindingResult,
final HttpServletRequest req) {
final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
seedStarter.getRows().remove(rowId.intValue());
return "seedstartermng";
}
And now we can add a dynamic table to our form:
<table>
<thead>
<tr>
<th th:text="#{seedstarter.rows.head.rownum}">Row</th>
<th th:text="#{seedstarter.rows.head.variety}">Variety</th>
<th th:text="#{seedstarter.rows.head.seedsPerCell}">Seeds per cell</th>
<th>
<button type="submit" name="addRow" th:text="#{seedstarter.row.add}">Add row</button>
</th>
</tr>
</thead>
<tbody>
<tr th:each="row,rowStat : *{rows}">
<td th:text="${rowStat.count}">1</td>
<td>
<select th:field="*{rows[__${rowStat.index}__].variety}">
<option th:each="var : ${allVarieties}"
th:value="${var.id}"
th:text="${var.name}">Thymus Thymi</option>
</select>
</td>
<td>
<input type="text" th:field="*{rows[__${rowStat.index}__].seedsPerCell}" />
</td>
<td>
<button type="submit" name="removeRow"
th:value="${rowStat.index}" th:text="#{seedstarter.row.remove}">Remove row</button>
</td>
</tr>
</tbody>
</table>
Quite a lot of things to see here, but not much we should not
understand by now… except for one strange thing:
Now I won't implement edit functionality.
Ideas?
Related
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/
I have created simple jsp servlet project on that when i submitting jsp form it insert data to specified table but after that when i refersh same jsp form get submitted and same data inserted to table..
ItemUnit.jsp
<form method="POST" action='ItemUnitHandler' name="frmAddUser">
<input type="hidden" name="action" value="insert" />
<table style="width:95%;margin-top:70px;" align="center">
<tr>
<td style="width:10%"> </td>
<td style="width:30%" align="right">Item Unit :</td>
<td style="width:2%"> </td>
<td style="width:40%" align="left"><input type="text" name="itemUnitName" style="width:200px;" /></td>
<td style="width:18%"> </td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr>
<td colspan="5" align="center">
<input type="submit" class="button-2" value="Insert"></input>
<input type="reset" class="button-2" value="Reset"></input>
</td>
</tr>
</table>
</form>
Servlet post method code..
String action = request.getParameter("action");
System.out.println("action :action action : "+action);
if(action == null)
{
redirect = "ItemUnit.jsp";
}
else if(action.equalsIgnoreCase("insert"))
{
ItemUnit objItemUnit = new ItemUnit();
// System.out.println("request.getParameter : "+request.getParameter("itemUnitName"));
objItemUnit.setItemUnit(request.getParameter("itemUnitName"));
dao.addItemUnit(objItemUnit);
redirect = "ItemUnit.jsp";
}
please help me out from this problem...
Ideally, you should land on a static result page, instead of displaying the input form again. The idea is to force the browser to switch to a new URL so that a refresh will not re-trigger another data update. This is the simplest fix.
But if you prefer to use the same screen, you should use a hidden token. You must record some state since you want to differentiate 2 requests from one another: first and second request (the refresh).
Within your logic, plant a token the first time you display the input form. On any incoming request from the same session, check if you have used the token already and use it to determine if this is a second-time request. The diagram below will illustrate it further.
Setup
Processing
I don't know if the titles says what i'm looking for.
Basically i got a table that shows current logged-in sessions. I'd like to be able to change their status and log them out. This is the JSTL code:
<div id="users_table_div">
<table id="box-table" style="width: 690px">
<thead>
<tr>
<th>IP</th>
<th>USER NAME</th>
<th>LAST ACTIVITY</th>
<th>LOGIN STATUS</th>
</tr>
</thead>
<tbody>
<c:forEach var="userDetail" items="${userSessionData}">
<tr>
<td><c:out value="${userDetail.ip}"/></td>
<td><c:out value="${userDetail.username}"/></td>
<td><c:out value="${userDetail.lastActivity}"/></td>
<td><img src="images/sign-out.png" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
Currently i've set a javascript function in the sessionId, but there might be another way to log out a selected user by its sessionId using spring security MVC.
So from the controller, i get different session data from all the users.
IP, Username, LastActivity and their SessionId (to log them out)
Is there a nice way to manage this?
This has been already resolved.
Just in case other is under the same issue:
I just made another #RequestMapping in the Controller with the sessionId passed from the view as the following:
<a href="<c:url value='/endsession?sessionId=${userDetail.sessionId}' />">
Then in the Controller the code was very simple:
#RequestMapping(value="/endsession", method = RequestMethod.GET)
public String endSession(String sessionId, Principal principal) {
logger.info("Removing sessionID = " + sessionId);
sessionRegistry.getSessionInformation(sessionId).expireNow();
return "activeusers";
}
And taraam :D
Hope it helps to others as well.
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)