How to show some selected value of dropdownlist - java

Thymeleaf View
<option th:each="technology:${technology}"
th:value="${technology}"
th:selected="${technology==project.getTechnology()}"
th:text="${technology}"/>
Java Controller
model.addAttribute("technology",organization.getTechnology());

assuming that you are binding your domain as myobject in your controller which also have technology as an attribute. Then add th:field="*{technology}" your code will look like below
in controller
model.addAttribute("technology",organization.getTechnology());
model.addAttribute("myobject",new MyClass());
<form th:object="${myobject}">
<select class="selectpicker"
id="tech"
th:field="*{technology}">
<option th:each="technology:${technology}"
th:value="${technology}" th:text="${technology}">
</option></select>
<form>

Related

how can get dropdown selected value in textbox in jsp

i want selected value in input text field in jsp how to do please help? I am new with jsp.
<select class="leavetype" name="leavetype">
<option value="10">CL</option>
<option value="7">ML</option>
<option value="12">SL</option>
<option value="20">EL/PL</option>
<option value="5">CPL</option>
</select>
<input type="text" name="leave"/>
See this video:- https://www.youtube.com/watch?v=89rRD04QZE4
Hope so it will help you.
In jsp page you may write. ${param.leavetype}.
You can achieve through javascript by using onChange function. Within in function need to do like this document.getElementById ("leave").value = data.value;
Refer this Answer

html load conditional value to select tag

I try to load conditional value to a select tag based on the user's choice on a pervious form tag. it might require java or php. i am not competent in any of those two language.
the logic is explain as the following.
<form>
<input name="complaint" value="copyright" type="radio">copyright <br>
<input name="complaint" value="trademark" type="radio">trademark <br>
<input name="complaint" value="other_concern" type="radio"> other concerns
</form>
if complaint="copyright" then
<select>
<option value="image">image</option>
<option value="product">product</option>
</select>
elseif complaint="trademark" then
<select>
<option value="item">item</option>
<option value="image">image</option>
</select>
elseif complaint="other_concern"
<select>
<option value="explain">explain</option>
</select>
end if
any solution?
Heres a solution in Jquery. Add a class name to the inputs and hide the selct options by default
<input name="complaint" value="copyright" type="radio" class="complaint">
then add unique ID tags to the select options along with a css class i like to use, .addClass and .removeClass are slighty faster and more efficent than .hide() and .show() selectors
Add the css at the bottom of your css sheet
.hidden{position:absolute;width:0;height:0;overflow:hidden;visibility:0;padding:0;margin:0}
<select id="copyright" class="selectOptions hidden">
<option value="image">image</option>
<option value="product">product</option>
</select>
Finally add some JQuery You need to copy the remainder for your other hidden values
$('.complaint').click(function() {
$('.selectOptions').addClass('hidden');
var checkComplaint = $(this).val();
$('#' + checkComplaint).removeClass('hidden');
});

Get all select Values in Java

I have a SELECT tag in JSP page that contains few values :
<select name="selectNumber" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
And I want to get all that values in a Java Class.
What I have tried that :
HttpServletRequest req = (HttpServletRequest)ServletActionContext.getRequest();
String[] selectedNumbers = req.getParameterValues("selectNumber");
But that Array only contains the value I am selecting, and If I don't select any value it contains null. But I want all the option values of the select Tag in java class. Any help?
I am using Struts 2 framework.
if you want to send all the Options to Your Servlet then you must first select them all using simply using Jquery
Before Submitting the Form
$("#selectNumber option").prop('selected',true);
Note #is used for Id , which i prefer if you need to use only name then try
$('[name="selectNumber"]');

How to avoid the duplicate entry of select box option while edit page?

Could anyone please explain me how to avoid the duplicate enters of records in to the select box options during editing the page.I am using java to develop web application I am very new to that,I need to remove the duplicates from the select box option while editing.Could anyone please help me .
Sample Code
<td><select property="employeestatus" value="" id="employeestatus">
<c:if test="${employee.employeestatus!=null}">
<option value="${employee.employeestatus}">${employee.employeestatus}</option>
</c:if>
<option value="">Select</option>
<option value="Active">Active</option>
<option value="Terminated">Terminated</option>
<option value="Deceased">Deceased</option>
<option value="Resigned">Resigned</option>
<option value="InActive">InActive</option>
</select>
<html:errors property="employeestatus"/>
<p id="employeestatus" style="color:red"/>
</td>
I would suggest to add the selected attribute on the option corresponding to your "employeestatus" variable instead of duplicating this option on the first line.
See HTML option selected Attribute
By the <html:errors ...>, I gess you use Struts 1. If it's true, I would use the
<html:select ...> tag and
<html:options or optionsCollection ...>
with java Map store in some context (servlet, session or request) to configure options. That struts tag will select the good option corresponding to your variable value.
See Struts <html:select ...> doc

Pass Two different values on a form to a servlet?

Given the following HTML:
<form action="" name="<%=rs.getString(1)%>" method="post">
<select name="opcao">
<option value="Did not like">Did not like</option>
<option value="Ok">Ok</option>
<option value="Liked" selected="selected">Liked</option>
<option value="Loved!">Loved!</option>
</select>
</form>
Is it possible to get the name of the form and from the select tag to the same servlet as different parameters?
HTML form name is NOT submitted as part of the request. Although if you want, you can pass as form hidden field as below:
<form name="myForm" action="/my_servlet">
<input type="hidden" name="htmlFormName" value="myForm"/>
....
However if you have two form fields, then they are passed to servlet and can be accessed using request.getParameter("fieldName") method inside the servlet..

Categories