I want to set the selected value of a select element from server side, but not getting idea how can I set the value, here is my code.
java code
model.addAttribute("dataSources", dataSourceRepository.findByDestinationId(sDestination.getId()));
jsp code:
<form:select onchange="refreshHistoryPage()" path="id" items="${dataSources}" itemLabel="name" itemValue="id"/>
Related
I have a variable that is obtain inside each block in thymeleaf. I want to send that variable to a certain method in my controller. The limitation is that the variable is obtain inside a block which makes it local, not accessible global.Hence I get an error while trying to use it. How can I move inside the scope to get the variable so as I can use it global in thymeleaf.
<form th:action="#{/masomo/somo(date=${dateMpya.date})}" method="POST">
<select id="date" name="date" required="true">
<option value="none" selected disabled hidden >
Select a Date
</option>
<th:block th:each="somoChagua : ${masomoChagua}">
<option th:each="dateMpya: ${somoChagua}" th:value="${dateMpya}" th:text="${dateMpya.date}" ></option>
</th:block>
</select>
<button type="submit"><i class="fa fa-search"></i> </button>
</form>
There can be many different "dateMpya" objects for each "somoChagua".
But there is only one submit button.
So which "dateMpya" should be used for the submit button value?
I think what you are actually trying to do here is get the value of the "dateMpya" which the user selected in the drop-down. Is that right?
If that's the case there is no need to add any attributes to the submit button. You would access that value by using the name of the select element, which is "date".
EDIT: For the same reason you also need to remove the (date=${dateMpya.date}) part of the form action as well. The value selected in the drop-down will automatically be submitted under the name of the select element "date", it does not need to be specified.
I have a table that is dynamically created that has rows of movies.
It has a title column,
a media type column,
a rating column,
and a column that contains a "view" button.
When one of these view buttons is clicked,
I would like to go to a page that contains all the details of that movie by sending the title to the controller so I can query for it in mySql.
The problem is that for all these buttons,
the value is always going to be "view".
So my solution is to make the name of the button different as shown in the code below (this is the raw html generated from the jsp):
<html>
<body>
<form action="someAction">
<table>
<tr>
<th>title</th>
<th>type</th>
<th>rating</th>
</tr>
<tr>
<td>title1</td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" name="mediaType.title1" value="view"></td>
</tr>
<tr>
<td>title2</td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" name="mediaType.title2" value="view"></td>
</tr>
<tr>
<td>title3</td>
<td>BLU-RAY</td>
<td>PG-13</td>
<td><input type="submit" name="mediaType.title3" value="view"></td>
</tr>
</table>
</form>
</body>
</html>
I could then use a parameterMap to figure out which view was pressed.
That is messy and Spring has to have some way of being able to do this.
I thought something like this would work in the controller:
#RequestMapping("someAction", params = "mediaType.{title}=view")
public ModelAndView loadPage(String title) {
// use title to query mysql
// build Model
// return ModelAndView
}
However this doesn't work.
Is there something that I can use in Spring that would be simpler and cleaner like above instead of getting the parameterMap from the request?
If I'm understanding this correctly, you have rows of information organized into a table. You're looping through some kind of collection, which has the side effect of making each input field have similar 'name' attributes. As a result, it's difficult to determine which input fields you really care about. You've then chosen to use the button as an identifier (presumably, because only the one button you actually click on get's added to the request -all the others don't get submitted) to determine which 'row', and subsequent input fields.
I think this might all come down to which 'button' you're using. If you're using a literal input tag (type="submit" or "button") the 'value' attribute is what the user sees as the text on the button -so, you're forced to play shenanigans with the 'name' attribute (presumably by adding an index to the name, splitting the string once you get it out of the request, and using that identifier to get the other parameters out of the request that also have the same identifier appended to their 'name' attribute).
JSP
<input type="submit" name="view${varStatus.index}" value="View" />
HTML Source
<input type="submit" name="view3" value="View">
You should probably use the button tag instead. It allows the text that the user sees to be different than the value that is submitted in the request.
JSP
<button type="submit" name="view" value="${varStatus.index}" >View</button>
HTML Source
<button type="submit" name="view" value="3">View</button>
This gives you the 'identifier' for the row.
How Spring fits into this:
I played with the #RequestMapping and the #RequestParam annotations, and was unable to get Spring to give me the values directly into the controller. But, I was able to do it by writing a custom HandlerMethodArgumentResolver along with a custom annotation.
note: I'm using annotations and java config instead of xml config, AND probably more importantly -this was a quick and dirty example to get it working. adjust it as needed for your situation.
HandlerMethodArgumentResolver
Annotation
Controller method
Instead of putting the table inside one form, you could put multiple forms inside the table, in the TRs:
<tr>
<form action="someAction">
<td>title1<input type='hidden' name='title' value='title1' /></td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" value="view"></td>
</form>
</tr>
Then each title has its own form and you send in the title (always with parameter name of title) from a hidden input.
The solution lies in this statement:
"I have a table that is dynamically created that has rows of movies".
Presumably,
you are looping through a list of movies and generating the table in the jsp file and each row has some unique identifier.
Add a hidden to identify the selected row.
Set the value of the hidden value in an onclick handler from the submit buttons.
Here is some example stuff:
<form ...>
<c:forEach blah var="row">
<tr>
...
<td><input type="submit" value="View" onclick="setSelectedRow('${row.id}')"/></td>
</c:forEach>
<input type="hidden" id="blammy" name="blammy" value=""/>
</form>
<script type="text/javascript">
function setSelectedRow(rowId)
{
... set blammy.value equal to rowId.
}
</script>
// the id is used by JavaScript to find the object in the DOM.
// the name is what appears in the request.
#RequestMapping("someAction")
public ModelAndView loadPage(
#RequestParameter("blammy") final String blammy)
{
...
}
i am using spring mvc,
<form:select class="form-control" id ="cmsphyexamtesttype_cmsPhysicalExamCategory_id" path="cmsPhysicalExamCategory.id">
<form:option value="0" label="--- Please select the Category ---"/>
<form:options items="${cmsphyexamtestcategorys}" itemLabel="name" itemValue="id" />
</form:select>
html code
<select id="cmsphyexamtesttype_cmsPhysicalExamCategory_id" name="cmsPhysicalExamCategory.id" class="form-control">
<option value="0">--- Please select the Category ---</option>
<option value="2">Genaral</option><option value="3">EYE</option><option value="4">HENT</option><option value="5">CHEST</option>
</select>
this one is working fine with new form , but in edit mode,it is not working do you have any idea it gives selected value while rendered to editing mode, i am new to spring mvc, is there any thing wrong with this code?
You are path binding the drop down value to the
cmsPhysicalExamCategory.id
So, the selected value will be retained in the form when you recall the form again for some other operation like 'edit.
You can change the value in the drop down and the new value will be path binded to the model. Here, its not getting binded. It may be because of the error in path bind/form submit.
Please post the form submit code/controller code for more help.
From your question what I get is that you have drop down and on submitting again , you want to render the same form, you are getting the change value but drop down value still persist to the default , first attribute of drop down, and you want to have the attribute same as value, in case this is the elaborated issue, you need to explicitly check in list for what value you want to select and set the attribute as Selected.
Below is my first table in my jsp page which is getting generated dynamically. This table will have multiple rows.
First column in the first row is `radio button`, and second column in the first row is `GenId`.
First column in the second row is `radio button`, and second column in the second row is `GenId` again.
...
Below is my same table code -
<p>First table</p>
<Table id="table1">
<tr>
<TD>
<input type="radio" name="test" id="radioButton" onclick="this.form.submit()>
</TD>
<TD>
<span id="importantColumnData">${Data.getGenId().get(i)}</span>
</TD>
... some other TD's
</tr>
</Table>
Now what I am suppose to do is as soon as I click on the radio button then I need to use the GenId of that row to pass to the servlet. Meaning, if I am clicking first row radio button, then I need to pass GenId value in the second column in the First row only to servlet.
As soon as I click on the radio button, it is able to call servlet properly but whenever I am trying to retrieve the GenId value in the servlet I always get null.
This is how I am trying to retrieving the value of GenId in the servlet -
String genIdValue = request.getParameter("importantColumnData");
Any thoughts what wrong I am doing and how to get the value?
I am totally new to this and I have tried a lot but no luck so far.. Any example will help me to understand better.
You can store the value as an hidden input field so it guarantee the value is send as parameter so:
<TD>
<span id="importantColumnData">${Data.getGenId().get(i)}</span>
<input type="hidden" value="${Data.getGenId().get(i)}" name="importantColumnData" />
</TD>
With this change you can get the value with the code request.getParameter("importantColumnData");
I saw a sample code in this website to dynamically populate second dropdown men based on a selection of first dropdown. I have account number dropdown and based on the selection, I need to populate second dropdown with corresponding email-id.
code is here :Populating Child Dropdown (second option using java script).
But can someone let me know how to populate the values inthe second dropdown ?
<script>
var dd2options = ${dd2optionsAsJSObject};
var dd3options = ${dd3optionsAsJSObject};
function dd1change(dd1) {
// Fill dd2 options based on selected dd1 value.
var selected = dd1.options[dd1.selectedIndex].value; ... }
</script>
My code is as below- how to change the code to do this ?
<td>
1. Member Account Number
<span class="bodyCopy">
<font color="#ff0000"> * </font>
</span>:
<html:select
name="DataForm"
property="Member.accountNumber"
styleClass="formContent"
style="width:80px">
<html:options collection="<%= WorkConstants.RENewDropdowns.PACCT %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
My second dropdown is as below:
<td>
3. Member <br>E-mail Address:<br />
<span class="bodyCopy"></span>
<html:select
name="DataForm"
property="Member.emailAddress.emailAddress"
style = "width:150px"
styleClass="formContent">
<html:options collection="<%= WorkConstants.RENewDropdowns.PEMAIL %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
Appreciate your help on this as I'm new to java script.
Problem can be solved in following ways
Using onchange event of select element, on first box submit form and populate data in second select box.
Use ajax to fetch data in the same way instead of submitting form
Fetch all the main and related data, store them in arrays. Call them according to key. You can use JSON to solve