this is what controller sends to me:
model.addAttribute("weather", weatherService.getWeatherByCity(id));
this is my JSP:
<form:form commandName="newWeather" method="post" action="edit">
<c:forEach items="${cities}" var="city">
<form:input path="temperature"></form:input>
<input type="submit" value="Submit">
</c:forEach>
</form:form>
Problem:
I get one object from database named weather. I want to edit that by changing temperature. So I must send back atleast id and field temperature. I know how to send back temperature as shown, but how can I send back my id.
I think I can get it from model by ${weather.id}, but how can I place it in form?
<input type="hidden" name="id" value="${weather.id}">
Related
I have a Spring MVC Form that I am building in a JSP, which will require entering an account number in a field. When I enter that number, I want to run a query to the database to pull back related information for that specific number. This data will then populate other fields on the form.
This is my Spring bind code in the JSP for the account number that will be entered. So, as soon as I enter this number, a DB query should trigger, to bring back the data for the other fields.
<spring:bind path="strExpenseAcctNum">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label">Expense Account Number :</label>
<div class="col-sm-10">
<form:input path="strExpenseAcctNum" type="string" class="form-control " id="strExpenseAcctNum" placeholder="strExpenseAcctNum" />
<form:errors path="strExpenseAcctNum" class="control-label" />
</div>
</div>
</spring:bind>
From my research, I am thinking a combination of Javascript and JQuery might be the best approach, but being new at this, I am not sure how to configure it.
Any help would be greatly appreciated.
Thanks,
Dave
#Dave
First implement your form using spring form tags, something like below.
<form:form method="POST" action="/spring-mvc-example" modelAttribute="example">
<table>
<tr>
<td><form:label path="number">Account Number</form:label></td>
<td><form:input path="number" id="number"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
Then use Jquery's focusout method to make an Ajax call in the background as soon as the user focus outs from account number field. Then in the success block, just parse it if needed and bind to your other fields using jquery's val method like below.
$("#number").focusout(function(){
$.ajax({
type:"POST",
url:"https://example.com/getDetails",
success: function(data) {
//Response from the controller comes here
var json = JSON.stringify(data);
//Bind it to fields like these
$("#firstname").val(json.firstname);
}
});
});
Hope this works for you, let me know if any questions.
I have a JSP page which reads data from HTML and has CSS,Jquery code in it .
Now my webpage in jsp has two text labels and a add button next to them.
User can enter any no of values in the text field.
Now my requirement is that every time user enters the alue in these fields and clicks on add then that data should be passed on to my servlet. Servlet will basically do some validation and return a boolean variable.
Based on the value of this boolean, I shall change the appearance of my text boxes.
This is required to be done for every time user clicks on Add button.
How can I achieve this ?
My HTML code :
<div id="id1" name="id1" style="display: none;">Add a node: </br>
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP"> <input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="button" value="Add" name="addnodebutton" id="addnodebutton"/>
</div>
The value in ipaddress and port shall be passed on to my servlet and depending on return parameter, their appearance should change.
Can anyone enlighten me how this is actually going to work ?
TIA :)
For passing data to and from a servlet, you have options.
Option 1- You can wrap your html in a form tag and set the action/method properties for your servlet/http method like below:
<form method="POST" action="servletname">
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP">
<input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="submit" value="Add" name="addnodebutton" id="addnodebutton"/>
</form>
The submit would send a request with the input to your servlet. You would then need to handle your request parameters in your servlet, set your values/flags in your response object and forward to the user or jsp/html page of your choice.
Option 2- You can make an ajax call from your jsp, process your input and return a response to your page asynchronously. Example below:
A Simple AJAX with JSP example
I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx
I have a question on how to update a particular record in a database using JSTL and java servlets.
Ok to call one record, I use a servlet to run a query to populate a jsp page using this type of format.
ResultSet results = getRecords.executeQuery(); //run database query
Result result = ResultSupport.toResult(results);
request.setAttribute("result", result);
RequestDispatcher rd = request.getRequestDispatcher("/showReport.jsp"); //redirect to this page with query info
rd.forward(request, response);
In the jsp page, I use this format to populate the page.
the row number is the corresponding to the index of the value on how the query is run.
<c:forEach var="row" items="${result.rowsByIndex}">
<form action = "/runthis/servlet?id=${row[0]}" method = "get" >
Employee Name<input type="text" value="<c:out value="${row[2]}"/>" />
Department<select>
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option></select>
<input type="submit" value="Save">
</form>
</c:forEach>
So for instance, if someone changes the Employee Name, but leaves the Department field alone, or vice versa, how would I go about this?
I think I should be able to kick this form action to a "update servlet", but I would rather not "update" every element in the record if I do not need to.
Also I'm still learning web dev, so if you can think of a better way to prepopulate stuff, via javascript or whatever, I'm willing to try that as well, if it's a possible solution.
Thanks for any help!
i think you need to do something like this:
<c:forEach var="row" items="${result.rowsByIndex}">
<form action="/runthis/servlet" method="post">
<input type="hidden" name="id" value="<c:out value="${row[1]}"/>" />
<input type="hidden" name="EmployeeName_orig" value="<c:out value="${row[2]}"/>" />
Employee Name<input type="text" name="EmployeeName" value="<c:out value="${row[2]}"/>" />
<input type="hidden" name="Department_orig" value="<c:out value="${row[4]}"/>" />
Department
<select id="Department" name="Department">
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option>
</select>
<input type="submit" value="Save" />
</form>
</c:forEach>
and then, in your update servlet (here called /runthis/servlet) you do some form of update employee and set the name and department where the row is id. note that i send the id in a hidden field, but it could just as well be sent in the url as you mentioned in our discussion above!
about your remark on updating every element in a record:
this can be done in different ways. one way is to load the record from database first, and compare the fields to know what fields to update. but that would require an extra read from the database before the update.
another way (the one i have prepared for in my proposed solution) is to have hidden fields, one for each input in your form. this hidden field holds the original value for its input counterpart. this way you can compare the original value with the new value without having to do a read from the database. on the other hand, you're now transferring the double amount of data over the line for each post request.
I'm working in a very simple and small web application, it is a jsp that handles a shopping cart.
What I do at this point is to iterate through all the products that are stored in the car and add them one by one to the jsp with each iteration.
This is the code that adds a row to the jsp in each iteration:
<tr>
<td>
<input type=text name=Quantity value=<%=quantity%>>
</td>
<td>
<input type=text name=id value=<%=id%>>
</td>
<td>
<input type=submit value="Delete" onclick=<%CustomSubmit(request, id); %>>
</td>
</tr>
As you can see I add to the end of each row a submit type control with a custom method for handling Click events, the use of this control is to remove from the car the respective product.
The problem that I have is that when I click in the delete button of a product, the id that is passed to the CustomSubmit(...) method is not the id of the product that I'm trying to remove but the id of the last product added to the jsp.
So, my question is how can I get the correct id from the item that I'm trying to remove?
The way i use to do it is as follows:
Replace
<input type=submit with a button
<input type="button" value="Delete" onclick="deleteIt('yourid');" />
add the deleteIt javascript function, in the function you fill a hidden input field with the id.
Then submit the page and the correct id gets passed to your page
Little sidenote its always prudent to escape all your Strings
dont use <input type=submit but use <input type="submit"
maybe like
<td>
<input type="text" name="id" value="<%=id%>">
</td>
<td>
<input type="button" value="Delete" onclick="deleteItem('<%=id%>')">
</td>
I assume your cart is a list of objects, each having the attributes id and quantity. So I would expect you code to look something like this (noting Peter's answer about using a 'button'):
<input type="button" value="Delete" onclick="CustomSubmit('<%=cartItem.id%>');"/>
I'm not entirely sure what you are trying to do with the 'request' parameter in your original code but if this is the HTTP request all you will get when you try to write it to the JSP is the result of the request.toString method.