I'm making a Spring MVC project, and I have this form in my .jsp
JSP
<form:form action="/admin/assign/add" modelAttribute="cafeTable">
<table>
<tr>
<td>
<form:label path="tableNumber">
<spring:message text="Table's Number"/>
</form:label>
</td>
<td>
<form:select path="tableNumber" action = "select">
<form:options items="${tableNumbers}"></form:options>
</form:select>
</td>
</tr>
<tr>
<td>
<form:label path="${user.fullName}">
<spring:message text="Waiter's name"/>
</form:label>
</td>
<td>
<form:select path="${user.fullName}" action = "select">
<c:forEach items="${listUser}" var="user">
<option value="${user.fullName}">${user.fullName}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<br>
<input type="submit" name="assign"
value="<spring:message text="Assign"/>"/>
</form:form>
So I have User and CafeTable models, but my model attribute is CafeTable.
With the submit button I need to get both selected values in my controller:
#RequestMapping(value = "/admin/assign/add", params = "assign", method = RequestMethod.POST)
public String assign(#ModelAttribute("cafeTable") CafeTable cafeTable) {
//set cafeTable's UserID field matching selected fullName value
tableService.addTable(cafeTable);
return "admin";
}
How can I do this?
<form:form action="/admin/assign/add" modelAttribute="cafeTable">
<table>
<tr>
<td>
<form:label path="tableNumber">
<spring:message text="Table's Number"/>
</form:label>
</td>
<td>
<input type="hidden" value="${user.fullName}" name="fullname">
<form:select path="tableNumber" action = "select">
<form:options items="${tableNumbers}"></form:options>
</form:select>
</td>
</tr>
<tr>
<td>
<form:label path="${user.fullName}">
<spring:message text="Waiter's name"/>
</form:label>
</td>
<td>
<form:select path="${user.fullName}" action = "select">
<c:forEach items="${listUser}" var="user">
<option value="${user.fullName}">${user.fullName}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<br>
<input type="submit" name="assign"
value="<spring:message text="Assign"/>"/>
</form:form>
If you don't have fullname property in CafeTable try to add a hidden field inside form:form like
<input type="hidden" value="${user.fullName}" name="fullname">
and add a optional request parameter into your controller like below
#RequestMapping(value = "/admin/assign/add", method = RequestMethod.POST)
public String save(#RequestParam("fullname") String fullname,#Valid #ModelAttribute("cafeTable") CafeTable cafeTable) {
//now you can access full name from fullname variable here
....
}
I haven't tested this code.
Related
I am trying to edit a submitted form but I am facing difficulties in doing so the problem is when I am submitting the form after editing null is being sent.I have been trying to get rid of this by myself but alas I have not been able to so.Please help me out.
My controller(only significant part)
#RequestMapping(value = "/registerstudentProcess", method = RequestMethod.POST)
public ModelAndView addStudent(HttpServletRequest request, #ModelAttribute("add_student") Student user) {
userService.registerStudent(user, request.getParameter("password"));
ModelAndView mav = new ModelAndView("addedstudent");
mav.addObject("adminsession");
mav.addObject("user", user);
mav.addObject("success_msg", user.getId() + " has been successfully added as an Student");
return mav;
}
#RequestMapping(value = "/editstud",method=RequestMethod.GET)
public ModelAndView editstud(HttpServletRequest request) {
ModelAndView mav = new ModelAndView("editstudent");
// mav.addObject("upd", new Student());
mav.addObject("adminsession");
mav.addObject("olduser", userService.editStudent(Integer.parseInt(request.getParameter("nayastud"))));
mav.addObject("batches", userService.getAllBatches());
return mav;
}
#RequestMapping(value = "/updatestud", method = RequestMethod.POST)
public ModelAndView updatestud(HttpServletRequest request, #ModelAttribute("olduser") Student stup) {
ModelAndView mav = new ModelAndView("addedstudent");
mav.addObject("adminsession");
mav.addObject("user", userService.updateStudent(stup));
mav.addObject("success_msg", stup.getId() + " has been successfully added as an Student");
return mav;
}
Model:Student.java
package project.ams.model;
public class Student {
private Integer id;
private String email;
private String name;
private String batch;
private String session;
private String address;
private String contact;
private String status;
//with proper getters and setters
}
addedstudent.jsp
<table class="table table-bordered table-condensed" align="center">
<tr>
<th colspan="2" style="text-align: center;">DETAILS</th>
<tr>
<tr>
<td><label>Id</label></td>
<td>${user.id}</td>
</tr>
<tr>
<td><label>Name</label></td>
<td>${user.name}</td>
</tr>
<tr>
<td><label>Email</label></td>
<td>${user.email}</td>
</tr>
<tr>
<td><label>Batch</label></td>
<td>${user.batch}</td>
</tr>
<tr>
<td><label>Session</label></td>
<td>${user.session}</td>
</tr>
<tr>
<td><label>Address</label></td>
<td>${user.address}</td>
</tr>
<tr>
<td><label>Phone Number:</label></td>
<td>${user.contact}</td>
</tr>
</table>
<table class="table table-condensed" align="center">
<tr>
<td>
<form:form id="editForm" action="editstud" method="post">
<input type="hidden" name="nayastud" value="${user.id}"/>
<button name="register"
class="btn btn-default">Edit Details</button>
</form:form>
</td>
<td>
<button onclick="location.href='admhome'"
class="btn btn-default">Back To Home</button></td>
</tr>
</table>
editstudent.jsp
<form:form id="regForm" modelAttribute="olduser" action="updatestud"
method="post">
<div class="form-group">
<form:label path="id">Enrollment Number</form:label>
<form:input path="id" name="id" id="id" class="form-control"
value="${olduser.id}" disabled="true" />
</div>
<div class="form-group">
<form:label path="name">Name</form:label>
<form:input path="name" name="name" id="name" class="form-control"
value="${olduser.name}" />
</div>
<div class="form-group">
<form:label path="email">Email</form:label>
<form:input path="email" name="email" id="email"
class="form-control" value="${olduser.email}" />
</div>
<div class="form-group">
<form:label path="batch">Batch</form:label>
<form:select path="batch" name="batch" id="batch"
class="form-control">
<option value="${olduser.batch}" selected></option>
<c:forEach var="bat" items="${batches}">
<form:option path="batch" value="${bat}">${bat}</form:option>
</c:forEach>
</form:select>
</div>
<div class="form-group">
<form:label path="session">Session</form:label>
<form:input path="session" name="session" id="session"
class="form-control" value="${olduser.session}" />
</div>
<div class="form-group">
<form:label path="address">Address</form:label>
<form:input path="address" name="address" id="address"
class="form-control" value="${olduser.address}" />
</div>
<div class="form-group">
<form:label path="contact">Phone</form:label>
<form:input path="contact" name="contact" id="contact"
class="form-control" value="${olduser.contact}" />
</div>
<div class="form-group">
<input type="hidden" name="password" value="Hello#123" />
<form:input type="hidden" path="status" name="status" value="a" />
</div>
<button id="register" name="register" class="btn btn-default">Update</button>
</form:form>
Edit 1
Added Screenshots(Sorry not able to post more screenshots due to reputation issues.)
Here I am changing the address and trying to update its value in the db.
Editing Student Info
Error here.Getting null object back from the edit page
when you are sending get request to fetch edit_student then you are passing value through request param and you should access this value using #PathVariable
or #RequestParam ..
<form:form id="editForm" action="editstud" method="post">
Expects a post handler
#RequestMapping(value = "/editstud",method=RequestMethod.GET)
is defined to accept only GET requests.
Try changing edit student method to
#RequestMapping(value = "/editstud",method=RequestMethod.GET)
public ModelAndView editstud(#RequestParam String nayastud) {
ModelAndView mav = new ModelAndView("editstudent");
mav.addObject("adminsession");
mav.addObject("olduser", userService.editStudent(Integer.parseInt(request.getParameter("nayastud"))));
mav.addObject("batches", userService.getAllBatches());
return mav;
}
I`m trying to send post request from JSP view to Controller by Button "onClick" method but i getting 404 Error that the RequestMapping is not sign, why is that?
HomeController:
#RequestMapping(value = "/showSelectedRequest/{id}", method = RequestMethod.POST)
public String loadRequestProducts(#PathVariable("id") int id, Model model) {
logger.debug("HomeController.RequestIdSelected() - Start");
logger.debug("HomeController.RequestIdSelected: id: " + id);
model.addAttribute("RequestIdSelected", id);
logger.debug("HomeController.RequestIdSelected() - Done");
return "/home";
}
Home.jsp:
<form action="${contextPath}/requestlist" method="post">
<table class="table table-sm">
<thead class="thead-inverse">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Show request
</th>
</tr>
</thead>
<c:forEach items="${requestDTOList}" var="requestDTO">
<tr>
<td>
${requestDTO.getId()}
</td>
<td>
${requestDTO.getName()}
</td>
<td>
<button class="btn btn-info" onclick="post(/showSelectedRequest/${requestDTO.getId()})">Query</button>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</td>
</tr>
</c:forEach>
</table>
</form>
When you have a form, the action field is what will be executed when you will click on the input of type "submit".
As a solution you can modify your code as follow:
<form action="${contextPath}/showSelectedRequest/${requestDTO.getId()}" method="post">
// Form elements ...
<input type="submit" value="Query" />
</form>
I am having trouble with my .jsp code in Spring MVC where I am using a date picker. When I access the date time picker, the code works fine. But in case i try to leave that field empty, i.e not use the date picker, it takes null as default( which it should) and then gives the error :
Failed to convert property value of type java.lang.String to required type java.util.Date for property ticComDropoofDate; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "" from type java.lang.String to type java.util.Date; nested exception is java.lang.IllegalArgumentException
I am attaching my code with this and any help will be appreciated.
My controller:
public class TicketController extends MasterController{
#InitBinder
protected void initBinder(WebDataBinder binder){
if(binder.getTarget() instanceof TicketDetailsBean)
binder.setValidator(new TicketDetailsValidator());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
#RequestMapping(value="/TicketRegistration", method=RequestMethod.POST)
public ModelAndView createTicketRegistration(#Valid TicketDetailsBean ticket, BindingResult result, Model model) throws SQLException{
TicketManager dm = new TicketManager();
TechManager dm1 = new TechManager();
SimpleMailManager sm = new SimpleMailManager();
ModelAndView modelAndView = new ModelAndView();
if(!result.hasErrors()){
try{
String message = dm.saveTicketRegistration(ticket);
/*if(check)
{
TicketDetailsBean ticket1 = new TicketDetailsBean();
OwnerDetailsBean owner1 = new OwnerDetailsBean();
ticket1 = sm.fetchData(ticket);
owner1 = sm.fetchOwner(ticket1);
}*/
sm.sendMail();/*(owner1,ticket1);*/
List<TicketDetailsBean> ticket2 = dm.findRegisteredTicket();
if(message != null){
message = "Duplicate entry not allowed. Data with same name already in the database...";
modelAndView.addObject("Message", message);
}
modelAndView.setViewName("TicketDatabase");
modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean());
modelAndView.addObject("ticketList", ticket2);
modelAndView.addObject("techDetailsBean", new TechDetailsBean());
modelAndView.addObject("tech", globalBean);
return modelAndView;
}catch (Exception e) {
e.printStackTrace();
}
}
else{
List<TechDetailsBean> tech = dm1.findRegisteredTech();
modelAndView.addObject(result.getAllErrors());
modelAndView.setViewName("TicketRegistration");
modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean());
modelAndView.addObject("techDetailsBean", new TechDetailsBean());
modelAndView.addObject("tech", globalBean);
modelAndView.addObject("techList",tech);
return modelAndView;
}
/*List<TicketDetailsBean> ticket2 = dm.findRegisteredTicket();
modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean());
modelAndView.addObject("ticketList", ticket2);*/
return modelAndView;
}
My jsp :
<script>
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'yy-mm-dd',
showOn: "button",
buttonImage: "<%=request.getContextPath() %>/resources/images/calendar.gif",
buttonImageOnly: true
});
}).val('');
$(function() {
$( "#datepicker1" ).datepicker({ dateFormat: 'yy-mm-dd',
showOn: "button",
buttonImage: "<%=request.getContextPath() %>/resources/images/calendar.gif",
buttonImageOnly: true
});
}).val('');
</script>
<form:form modelAttribute="ticketDetailsBean" action="TicketRegistration" method="post">
<table>
<tr>
<td>
<h1>Ticket Registration Form</h1>
<h3>Please enter the information below</h3>
<h3>click "<b>Save</b>" when complete. </h3>
<h3> Fields marked with an asterisk '<b>*</b>' are required.</h3>
<h3> Click "<b>Reset</b>" to clear the fields</h3>
</td>
</tr>
<tr>
<td>
<fieldset title="info">
<legend><b>Ticket Information</b></legend>
<table>
<tr>
<td align="left"><form:label for="ticDate" path="ticDate" cssErrorClass="error">* Ticket Date :</form:label></td>
<td align="left"><form:input path="ticDate" id="datepicker1" size="35" maxlength="35"/> <form:errors path="ticDate" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticServicetag" path="ticServicetag" cssErrorClass="error">* Service Tag Number :</form:label></td>
<td align="left"><form:input name="ticServicetag" path="ticServicetag" id="ticSrvctag" size="35" maxlength="35" value = "${computerList.comServicetag}" /> <form:errors path="ticServicetag" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticOwnerSuUsername" path="ticOwnerSuUsername" cssErrorClass="error">* Owner Su_username :</form:label></td>
<td align="left"><form:input path="ticOwnerSuUsername" id="owsusenm" size="35" maxlength="35" value ="${computerList.comOwnerSUUsername }" /> <form:errors path="ticOwnerSuUsername" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticTechname" path="ticTechname" cssErrorClass="error">* Tech Name :</form:label></td>
<td align="left">
<select name="ticTechname" id = "ticTechname" id="slectboxid1">
<option value = ""> ---Select--- </option>
<c:forEach var="item" items="${techList}">
<option value = "${item.techName}"><c:out value= "${item.techName}"/></option>
</c:forEach>
</select>
<form:errors path="ticTechname" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticComProblem" path="ticComProblem" cssErrorClass="error">* Problem :</form:label></td>
<td align="left"><form:textarea path="ticComProblem" rows ="3" cols ="27"/> <form:errors path="ticComProblem" cssErrorClass="error" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticComments" path="ticComments" cssErrorClass="error"> Comments :</form:label></td>
<td align="left"><form:textarea path="ticComments" rows = "3" cols = "27"/> <form:errors path="ticComments" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticItemtoOrder" path="ticItemtoOrder" cssErrorClass="error"> Item To Order :</form:label></td>
<td align="left"><form:input path="ticItemtoOrder" size="35" maxlength="35" /> <form:errors path="ticItemtoOrder" /></td>
</tr>
<tr>
<td align="left"><form:label for="ticComDropoofDate" path="ticComDropoofDate" cssErrorClass="error"> Computer Drop Off Date:</form:label></td>
<td align="left"><form:input path="ticComDropoofDate" id="datepicker" size="35" maxlength="35" /> <form:errors path="ticComDropoofDate"/></td>
</tr>
<tr>
<td align="left"><form:label for="ticAssignedTech" path="ticAssignedTech" cssErrorClass="error">* Tech Assigned :</form:label></td>
<td align="left">
<select name="ticAssignedTech" id = "ticAssignedTech" id="slectboxid2">
<option value = ""> ---Select--- </option>
<c:forEach var="item" items="${techList}">
<option value = "${item.techName}"><c:out value= "${item.techName}"/></option>
</c:forEach>
</select>
<form:errors path="ticAssignedTech" />
</td>
</tr>
<tr>
<td align="left"><form:label for="ticStatus" path="ticStatus" cssErrorClass="error">* Ticket Status :</form:label></td>
<td align="left">
<select name="ticStatus" id = "ticStatus" id="slectboxid3">
<option value="">---Select--- </option>
<option value="Open"> Open </option>
<option value="InProgress">In Progress</option>
<option value="Closed">Closed</option>
</select>
<form:errors path="ticStatus" />
</td>
</tr>
<tr>
<td align="left"><form:label for="ticAddEquipments" path="ticAddEquipments" cssErrorClass="error"> Additional Equipments dropped with Computer :</form:label></td>
<td align="left"><form:textarea rows= "3" path="ticAddEquipments" cols ="27"/> <form:errors path="ticAddEquipments" /></td>
</tr>
</table>
</fieldset>
<fieldset>
<div align="center">
<form action="TicketDatabase" method="get">
<input type="submit" name="submit" value="Save"/>
<input type="reset" name="reset"/>
</form>
</div>
</fieldset>
</td>
</tr>
</table>
The exception message:
Unable to convert value "" from type java.lang.String to type java.util.Date
spring throws because you have not informed spring to convert empty string to null.
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
pass true if you want null when empty string like:
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
Spring CustomDateEditor constructor java docs has:
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)
Create a new CustomDateEditor instance, using the given DateFormat for parsing and
rendering. The allowEmpty parameter states if an empty String should
be allowed for parsing, i.e. get interpreted as null value. Otherwise,
an IllegalArgumentException gets thrown in that case.
Parameters:
dateFormat - DateFormat to use for parsing and rendering
allowEmpty - if empty strings should be allowed
I've a form that can edit/update data build in modal (bootstrap), I want to check the checkbox based on my row table...
this is my edit/update form :
<td><fieldset id="menu_e">
<input type="checkbox" name="menu" value="User" class="ceksmenu">User<br />
<fieldset id="sub_menu_user_e">
<input type="checkbox" name="sub_menu_user" value="User1" class="ceksmuser">User1
<input type="checkbox" name="sub_menu_user" value="User2" class="ceksmuser">User2
<input type="checkbox" name="sub_menu_user" value="User3" class="ceksmuser">User3<br />
</fieldset>
<input type="checkbox" name="menu" value="Monitoring" class="ceksmenu">Monitoring<br />
<fieldset id="sub_menu_monitoring_e">
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring1" class="ceksmmonit">Monitoring1
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring2" class="ceksmmonit">Monitoring2
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring3" class="ceksmmonit">Monitoring3<br />
</fieldset>
<input type="checkbox" name="menu" value="Parameter" class="ceksmenu">Parameter<br />
<fieldset id="sub_menu_parameter_e">
<input type="checkbox" name="sub_menu_parameter" value="Parameter1" class="ceksmparam">Parameter1
<input type="checkbox" name="sub_menu_parameter" value="Parameter2" class="ceksmparam">Parameter2
<input type="checkbox" name="sub_menu_parameter" value="Parameter3" class="ceksmparam">Parameter3</fieldset>
</fieldset>
</td>
this is my table code :
<tbody>
<c:forEach var="row" items="${requestScope.authorityuser}">
<tr>
<td>${row.id_authority}</td>
<td>${row.nama_authority}</td>
<td><c:forEach var="u" items="${row.menu}">|${u}| </c:forEach></td>
<td><c:forEach var="u" items="${row.sub_menu_user}">|${u}| </c:forEach></td>
<td><c:forEach var="u" items="${row.sub_menu_monitoring}">|${u}| </c:forEach></td>
<td><c:forEach var="u" items="${row.sub_menu_parameter}">|${u}| </c:forEach></td>
<input type="hidden" name="id_authority" value="${row.id_authority }">
<td><a href="#update" role="button" data-toggle="modal"
class="update" id_update="${row.id_authority}"
nama_authority="${row.nama_authority}" menu="${row.menu}"
sub_menu_user="${row.sub_menu_user}" sub_menu_monitoring="${row.sub_menu_monitoring}"
sub_menu_parameter="${row.sub_menu_parameter}"> <i class="icon-edit"></i> <spring:message code="edit" text="default text" />
</a><a href="#delete" role="button" data-toggle="modal"
class="delete" id_delete="${row.id_authority}">
<i class="icon-trash"></i> <spring:message code="delete" text="default text" />
</a></td>
</tr>
</c:forEach>
</tbody>
I tried like this and it's not work :(
<script type="text/javascript">
$(document).ready(function() {
$('.update').click(function() {
if($('input:checkbox[name=sub_menu_user]').is(':checked')){
$('input:checkbox[name=sub_menu_user]').prop('checked', true);
}else{
$('input:checkbox[name=sub_menu_user]').prop('checked', false);
}
var id_update = $(this).attr("id_update");
var nama_authority = $(this).attr("nama_authority");
var menu = $('input:checkbox[name=menu]').is(':checked');
var sub_menu_user = $('input:checkbox[name=sub_menu_user]').is(':checked');
var sub_menu_monitoring = $('input:checkbox[name=sub_menu_monitoring]').is(':checked');
var sub_menu_parameter = $('input:checkbox[name=sub_menu_parameter]').is(':checked');
$('#id_authority_e').val(id_update);
$('#id_authority_l').val(id_update);
$('#nama_authority_e').val(nama_authority);
$('#menu_e').val(menu);
$('#sub_menu_user_e').val(sub_menu_user);
$('#sub_menu_monitoring_e').val(sub_menu_monitoring);
$('#sub_menu_parameter_e').val(sub_menu_parameter);
});
$('.delete').click(function() {
var id_delete = $(this).attr("id_delete");
$('#id_authority_d').val(id_delete);
});
$('#example').dataTable({
});
});
</script>
this is the output of my table
how is it possible?? any help will be pleasure :)
this is my edit/update form
this is my table
Well i just looked at the javascript portion, here is my answer.
You would have to read the Text in the HTML and parse it, for that you need to be able to get a specific cell. With the row_id you can narrow your search down and over the classname for the cloumn you get the right cell.
I hope this short working demo on jsFiddle might help you with your "problem".
HTML:
<table class="data-table">
<tr id="row_1"><td class="authority">...</td>
<!-- id should be constructed something like id="row_${row.id_authority}" -->
<td class="...">...</td>
<td class="users">|user1|user2|user3|</td>
<td class="...">...</td>
<td> edit </td>
<!-- id_update should be as in your code id="${row.id_authority}" -->
</tr>
</table>
Javascript:
$(function(){
$(".update").click(function(){
var id_update = $(this).attr("id_update");
var users = $("#row_" + id_update).find(".users").text().split("|").slice(1,-1);
for(var user in users)
alert(users[user]);
});
});
// tested on Windows 7 with Chrome 33+
edit: added some "Code"
possible JSP Code:
<c:forEach var="row" items="${requestScope.authorityuser}">
<tr id="row_${row.id_authority}">
<td class="...">${row.id_authority}</td>
<td class="...">...</td>
<td class="users"><c:forEach var="u" items="${row.sub_menu_user}">|${u}| </c:forEach></td>
<td class="...">...</td>
<td>
<a href="#update" role="button" data-toggle="modal"
class="update" id_update="${row.id_authority}"
nama_authority="${row.nama_authority}" menu="${row.menu}"
sub_menu_user="${row.sub_menu_user}" sub_menu_monitoring="${row.sub_menu_monitoring}"
sub_menu_parameter="${row.sub_menu_parameter}"> <i class="icon-edit"></i> <spring:message code="edit" text="default text" />
</a> ...
</td>
</tr>
</c:forEach>
edit:
i updated the jsFiddle (this link is different to the one above).
the important part is here...
for(var user in users){
// it could be optimized, but like this it could work.
$("input[name='sub_menu_user'][value='" + users[user] + "']")[0].checked=true;
}
// tested on Windows 7 with Chrome 33+
I have a login page with form action of j_security_check. As of now this form just have two fields namely username
and password. I want to add a new dropdown to this form and collect the selected value in controller using
#RequestParam. For some reason I am not able to pass this dropdown value from JSP to my controller as its throwing the
exception: MissingServletRequestParameterException (Which occurs anytime a request param is missing).
In the code below I added the Visuals dropdown. Do I need to use Spring:Bind tag here?
Also on successful login, the control is directed to a controller with request mapping /controller1.html and this is where
I am trying to collect the dropdown value.
<form name="appLogin" action="j_security_check" method="POST">
<table width="100%">
<tr>
<td align="center">
<table>
<tr>
<td>Username: </td>
<td><input id="userName" name="j_username" value=""/></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="j_password" type="password" value="" /></td>
</tr>
<tr>
<td>Visual: </td>
<td><Select name="visuals" id="visuals"/>
<option value="S1">S1</option>
<option value="S2">S2</option>
<option value="S3">S3</option>
<option value="S4">S4</option>
</Select>
</td>
</tr>
</table>
<table>
<tr>
<td>
<button type="submit" name="submit" value="Sign In">Sign In</button>
<input type="submit"/>
</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</form>
Controller Code:
#RequestMapping( value = " /controller1.html", method = RequestMethod.GET )
public String setupForm( #RequestParam(value = "visuals", required=false) String visuals,
ModelMap model )
{
List<String> studentNames = new ArrayList<String>();
List<String> teacherNames = new ArrayList<String>();
model.addAttribute("someData", teacherNames);
model.addAttribute("anotherData", studentNames);
model.addAttribute("visuals", visuals);
log.info("Role from Dropdown: " + visuals);
return "school/classTen";
}
You need to create yyour own Filter by extending AbstractAuthenticationProcessingFilter
I don't have the entire code in front of my eyes, but the following article could help you:
http://mark.koli.ch/2010/07/spring-3-and-spring-security-setting-your-own-custom-j-spring-security-check-filter-processes-url.html