delete row composite key in table jsp - spring-mvc

How can I delete row of table with dinamic delete button it works for single id but in this case that is for a compositekey how can i pass the value of both ids to component
<table border="1" cellpadding="5" class="table">
<tr>
<th>Fecha</th>
<th>Actividad</th>
</tr>
<c:forEach items="${listFechaActividad}" var="fechaActividad">
<tr>
<td>${fechaActividad.fecha}</td>
<td>${fechaActividad.actividad}</td>
<td>
<!-- composite key -->
Delete
</td>
</tr>
</c:forEach>
</table>
#RequestMapping("/delete")
public String deleteForm(#RequestParam String id) {
...
return "redirect:/";
}

I fanilly made it work adding te other id in the string
Delete

Related

getting value in JSP by using field name

I have requirement something similar like below in our application,
I would like to show the below details in the form of table.
Country Name Population CapitalCity Aria
US XX XX XX XX
IN YY YY YY YY
User can choose in the configuration page which columns he is interested to see.
In the backend I set model attributes as below(using spring MVC),
model.addAttribute("selColumns", "column keys");
model.addAttribute("countryDetails", "List of country details");
In the CountryDetail class, field names and selColumns key names are same.
class CountryDetails {
private String country,
population,
CapitalCity,
Aria;
}
In the UI I am trying with the following code to achieve the same.
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<c:forEach items="${selColumns}" var="item">
<th><spring:message code="${item}" /></th>
</c:forEach>
</tr>
</thead>
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
//Below code is not working
<c:out value="${item.det}" /></td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
Table header is working fine. But I am struggling to show row information only for the configured columns.
Code wouldn't work because it tries to find getDet() at the java side.
Could some one please help is there any way in JSP, if I give property(field) name the corresponding value would return?
Something like this
Thanks in advance,
kitty
Try this one.
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
<c:out value="${det[item]}" />
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
Note:
selColumns = ["Country", "Population", "CapitalCity", "Aria"]
CountryDetails properties = Country,Population,CapitalCity,Aria

Cannot properly passing the value of textbox in a table row from view to controller in MVC5

I am trying to send 3 values from a view to another action, using actionlink. I have a table . Each row has 3 columns , with id, name and bid money which is a textbox. The problem is the action link only passing the value of the textbox of the first row, suppose if I press the actionlink of the second row, it is passing, the name and id of the second row but the bid money which is in textbox, is passing of the first row.
my view code:
#model List<UdemyMVC.Models.Movies>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="danger">
<th>
Id
</th>
<th>
Name
</th>
<th>
Bid Money
</th>
</tr>
</thead>
<tbody>
#foreach (var movie in Model)
{
<tr>
<td>
#movie.Id
</td>
<td>
#movie.MovieName
</td>
<td>
<input type="text" id="bidMoney" name="bidMoney"/>
</td>
<td>
#Html.ActionLink("Details", "Details", "Test", new { Id =movie.Id, MovieName = movie.MovieName }, new { onclick = "this.href += '&biddingMoney=' + document.getElementById('bidMoney').value;", #class = "btn btn-primary" })
</td>
</tr>
}
</tbody>
</table>
I think you should add an index to the id of bidMony and use that index as a reference to the one you want. My razor is totally rusty right now
but declare an index
#{ int i = 0}
#foreach (var movie in Model)
{
<tr>
<td>
#movie.Id
</td>
<td>
#movie.MovieName
</td>
<td>
<input type="text" id="bidMoney#(i)" name="bidMoney" />
</td>
<td>
#Html.ActionLink("Details", "Details", "Test", new { Id =movie.Id, MovieName = movie.MovieName }, new { onclick = string.Format("alert(document.getElementById('bidMoney{0}').value);",i), #class = "btn btn-primary" })
</td>
</tr>
i++;
}
I'm unable to test this but I think this is what you need to do. The problem is that you're getelementbyid see's a bunch of bidMoney elements but can only return one so it gives you the first one.

Imported data not display

Imported data not displays in repeated table. It's only shows demo data. below is my template.
#{
<table class="table table-striped table-bordered">
<tbody>
#foreach (var Content in AsDynamic(Data["Default"]).OrderBy(m => m.wcName))
{
<tr>
<td width="30%">#Content.wcName </td>
<td>#Html.Raw(#Content.wcPosition)
#if (DotNetNuke.Common.Globals.IsEditMode())
{
#Content.Toolbar
}
</td>
</tr>
}
</tbody>
</table>
}
Your template only shows items assigned to this page/module, not "all data" - for that you would have to replace Data["Default"] with App.Data["your-content-type-name"].
You probably want to read more about this here: http://2sxc.org/en/blog/post/12-differences-when-templating-data-instead-of-content

Spring Form hidden value is lost on submit

On my 'update user' page I have a hidden field to hold the ID of the current user being edited. I've checked the HTML source and the value populates correctly. However when I submit the form, the userID value ALWAYS comes through as 0 while every other field comes through correctly.
-I've tried setting the type as form:input and submitting it, still 0 on the controller side.
-There are no bindingresult errors.
-Page URL: /admin/update-user.html?uid=3 (I've tried changing the URL variable from uid to userID, no difference.)
So what the heck is going on?! Am I missing something obvious??
JSTL
<form:form commandName="user" method="POST">
<form:hidden path="userID"/>
<table width="400" cellpadding="3" cellspacing="0" border="0" class="datatable">
<tr>
<td>User ID #</td>
<td>${user.userID}</td>
</tr>
<tr>
<td valign="top">Password</td>
<td>****<br />Change Password</td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td>Gender: </td>
<td>
<form:select path="gender">
<form:option value="Male" label="Male" />
<form:option value="Female" label="Female" />
</form:select>
</td>
</tr>
<tr>
<td>Birthday: </td>
<td><form:input path="birthday" id="datepickerpast" readonly="true" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Update" /> <input type="reset"></td>
</tr>
</table>
</form:form>
Controller
#RequestMapping(value = "admin/update-user", method = RequestMethod.POST)
public String processUpdateUser(ModelMap model, #ModelAttribute("user") User user, BindingResult result) throws SQLException {
if (result.hasErrors()) {
model.addAttribute("user", user);
model.addAttribute("pagetitle", "Admin - Update User Details");
return "admin/users/update-user";
} else {
userDao.updateUser(user);
return "redirect:/admin/user-management.html";
}
}
Object Properties with appropriate getters/setters
private int userID;
private String firstName;
private String lastName;
private String username;
private String email;
private Date birthday;
private String gender;
Realized my getter was type Integer but my setter and property was type int. Made all of them Integer and it worked. If someone can explain why there was no error on compile or why it didn't work the way it was, that'd be greatly appreciated.
Sigh, there goes 3 hours of my life.
You got a compiler error because of autoboxing, which allows for the automatic conversion of primitives to their wrapper class alternatives. As to why it didn't work when the getter was using Integer, I can't help you there.

How to do the Hibernate validation on the nested list objects?

I need to validate the objects which are stored in the list on my form bean object.
Below is my form bean object.
public class Role implements java.io.Serializable {
// Fields
private int roleId;
#NotBlank
private String roleName;
private boolean active;
#Valid
private List<Module> modules;
// getters anfd setters
}
and below is my object which is present in the list of my main form bean object
public class Module implements Serializable {
private int id;
#NotBlank
private String moduleName;
// other properties and getters and setters
}
Below is my properties file
# -- Role form --
NotBlank.role.roleName=Role Name can not be blank.
NotBlank.module.moduleName=Module Name can not be blank.
Below is My JSP page, the form consists of a role name and modules which can be added to the role.
<table border="0" class="section_tbl2">
<tr>
<td width="150px" valign="top">
<spring:message code="dmx.role.form.label.name"/>
</td>
<td width="10px">:</td>
<td>
<form:input class="txtinput" id="roleName" path="roleName" maxlength="50"/> <form:errors path="roleName" cssClass="error"/>
</td>
</tr>
<tr><td colspan="3" height="8px"></td></tr>
<tr>
<td width="150px" vAlign="top">
Modules
</td>
<td width="10px" vAlign="top">:</td>
<td>
<table>
<tr>
<td>
<input type="button" value="<spring:message code="dmx.role.form.button.addModule.label"/>" onclick="return addModuleRow();"></input>
</td>
</tr>
<tr><td> </td></tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" class="tblstyle1" id="moduleTable">
<thead>
<tr>
<th class="fst" width="200px">
<spring:message code="dmx.role.form.label.moduleName"/>
</th>
<th width="50px"><spring:message code="dmx.role.form.label.create"/></th>
<th width="50px"><spring:message code="dmx.role.form.label.update"/></th>
<th width="50px"><spring:message code="dmx.role.form.label.delete"/></th>
<th width="30px"></th>
</tr>
</thead>
<tbody id="moduleTBody">
<c:forEach items="${role.modules}" var="module" varStatus="status" >
<c:set var="moduleCounter" value="${status.index}"/>
<tr id="moduleRowId_${moduleCounter}">
<td class="fst txt-center">
<form:select onchange="checkIfThisModuleAlreadySelected(this);" class="seloption" id="selectedModule_${moduleCounter}" path="modules[${moduleCounter}].id">
<form:option value="" label="-- Select Module --"/>
<form:options items="${moduleList}" itemLabel="moduleName" itemValue="id" />
</form:select>
</td>
<td class="txt-center">
<form:checkbox id="create_${moduleCounter}" path="modules[${moduleCounter}].create"/>
</td>
<td class="txt-center">
<form:checkbox id="update_${moduleCounter}" path="modules[${moduleCounter}].update"/>
</td>
<td class="txt-center">
<form:checkbox id="delete_${moduleCounter}" path="modules[${moduleCounter}].delete"/>
<td class="txt-center">
<input class="delbtn" id="moduleDelBtn_${moduleCounter}" name="moduleDelBtn[${moduleCounter}]" type="button" onclick="delModuleRow(${moduleCounter});">
</td>
</tr>
</c:forEach>
</tbody>
</table>
</td>
</tr>
<tr><td colspan="3" height="3px"></td></tr>
</table>
I can successfully validate the role name i.e. when role name is blank I get an error message but when module is not selected i do not get any error message.
Please help
Adding #NotNull and #Size constraints to your module list should help:
#Valid
#NotNull
#Size(min = 1)
private List<Module> modules;
The #Valid annotation causes the elements of the annotated collection to be validated but it doesn't validate wether that collection is not null or contains any elements.

Resources