Not able to alert the value of Select value - java

I am tying to alert the value of namesEmp variable and i get this Ljava.lang.String;#3433205b as result.
my javacsript:
function getNames(names) {
var namesEmp = "";
var namesEmpText = "";
for(i=0;i<names.length;i++) {
if(names.options[i].selected) {
namesEmp = names.options[i].value;
namesEmpText = names.options[i].text;
alert(namesEmp);
alert(namesEmpText);
}
}
}
and here is my HTML:
<tr style="position:relative;left:19;top:1;display:none;">
<td style="position:relative;top:1;text-align:right;">Country:</td>
<td>
<select id="namePay" name="namesPay" onChange="setTimeout('getNames(f.name)',1);">
<%
String namesDesc = "";
if(namesList.size()>0){
for(int i=0;i<namesList.size();i++){
String[] namesValues = ((String)namesList.get(i)).split("~");
NamesText = namesValues[0];
%>
<option value="<%=namesValues%>"><%=NamesText%>
<%}
}%>
</select>
</td>
</tr>
When I alert namesEmpText, i get the the correct result. Any help would be appreciated.

The issue is here:
<option value="<%=namesValues%>"><%=NamesText%>
^
You are trying to render the Java object namesValues (a string array), as a select option value. The JVM doesn't know how to automatically convert an array to a string, so it renders the type name for it instead. Did you mean to include an indexer to that array? For example:
<option value="<%=namesValues[1]%>"><%=namesValues[0]%>

Related

What Spring Form backing object should be in the form where I want to pass a value from dropdown?

I have a form:
<f:form>
<f:select path="refmap" onchange="myMethod();" name="tableName" id="tableName" >
<f:options items="${valmap}"/>
</f:select>
<input type="submit" value="Submit" formAction="return constructUrl()"/></f:form>
Mentioned functions, but guess they aren't the big issue here:
function myMethod(){
var e = document.getElementById("tableName");
var tableName = e.options[e.selectedIndex].value;
}
function constructUrl(){
var e = document.getElementById("tableName");
var tableName = e.options[e.selectedIndex].value;
return "/spravochnik/list/"+tableName;
part of my controller leading to this page:
Map refmap = new LinkedHashMap<>();
refmap.put("tableMap", valmap);
mod.addAttribute("refmap", refmap);
mod.addAttribute("command", refmap);
valMap just has names of tables (that's what I'm using in dropdown);
The thing is, the jsp is swearing at me for refmap not having valid gettter/setter. I just want to pass along the dropdown value and nothing else. What backing object should I choose?
Can you do similar to this with your code and try ? It works for me.
<select name="country">
<c:forEach items="${countries}" var="country">
<option value="${country.key}">${country.value}</option>
</c:forEach>
</select>

Disable dropdown list without any event

I have one jsp and want to disable dropdown list using scriplet without any onclick method or event in dropdown menu
<%
boolean something= true;
if(something){
// here I want to get plc1 using id and want to disable it if it is true
}else{
//do some thing
}
%>
my dropdown html code is here
<td bgcolor='' align="center" ><select id = "plc1" name="place1" onclick="this.parentNode.style.backgroundColor = this.value">
<option value='red'>Park here</option>
<option value='green'>Cancel</option>
</select></td>
how to do it? any hint please?
just use disabled attribute: <select id="plc1" disabled>
<%
String state = "";
if(something){
state = "disabled";
}
%>
<select id="plc1" <%= state %>>
<%
boolean something=false;
String state = "";
if(something){
state = "disabled";
// here I want to get plc1 using id and want to disable it if it is ture
}else{
state = "enable";
}
%>
Html
<select <%= state %> id = "plc1" >
Now its working Thanks

ServletRequest.getParameterValues returns only first value

I am assigning an integer converted into string value to a hidden input through a loop where s1 is the loop iterator.
<% int k=0;
String s1 = null;
while(mar.next()){
k++;
s1 = Integer.toString(k);%>
<table>
<tr><td><b>User Name </b> </td> <td><%= mar.getString("UserName") %></td></tr>
<tr><td><b>Type </b></td><td> <%= mar.getString("Type") %></td></tr>
<tr><td><b>Complain </b></td> <td><%= mar.getString("Complaint") %></td></tr>
<tr><td><b>Status </b> </td> <td><%= mar.getString("Status") %></td></tr>
<tr><td><b>Date </b> </td> <td><%= mar.getString("Date") %></td></tr>
</table>
<form action = "demo1.jsp" method = "post">
<% out.println(mar.getString("Id")); %>
<input type="radio" name = "<%= mar.getString("Id") %>"value="pending"checked>Pending
<input type="radio" name = "<%= mar.getString("Id") %>" value="done">Done
<input type="submit" name = "B" id = "submit" >
<input type = "hidden" name = "try" value = <%= s1 %> >
<% out.println(s1); %>
</form>
<br>
<br>
<%
}
Below is the code which should return a string of values from 1 to 3 (values of s1)
String[] vals;
vals = new String[3];
out.println(vals.length);
vals = request.getParameterValues("try");
out.println(vals.length);
for(int i=0; i<vals.length; i++){
out.println("Hy");
out.println(vals[i]);
}
It is only returning one value which is the first one and printing vals.length prints 1.
You are creating three forms. For each form there will be separate request made. Since each form contains only one hidden input you will always get only one value for each request for below code
request.getParameterValues("try");
Now if you have to submit all the forms with one click. here or here are the answers

how to display value in dropdown list depending on another dropdownlist using jsp?

I'm tying to bind the value to dropdownlist i.e named as subtype which is depending on another dropdownlist i.e named as type. Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.
`<table>
<tr>
<td>
<label id="type">Chosen category : </label>
</td>
<td>
<% String getType=request.getParameter("id");
out.println("<h4>"+getType+" <a href='post free ads.jsp'>Change the
category</a> </h4>");
%>
</td>
</tr>
<tr>
<td>
<label id="typeselect">Select type : </label>
</td>
<td>
<select name="type">
<option value="none">Select</option>
<%
Connection conn = NewClass.getOracleConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
where stuff_type='"+getType+"' ");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td> <label id="subtypeselect">Select Subtype : </label></td>
<td>
<select name="subtype">
<option value="none">Select</option>
<%
try
{ %>
<% String typeselected = request.getParameter("type");
Statement stmtmt = conn.createStatement();
ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
where stuff_type='"+getType+"' and company='"+typeselected+"' ");
while(rse.next()){
%>
<option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option>
<%
}
%>
<% }
catch(Exception e)
{
out.println(e.getMessage());
}
%>
}
</select>
</td>
</tr>
</table>`
You need AJAX for this to get it work.
Your jsp is compiled into a servlet and then delivered to your client. After that no more changes are possible.
You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.
Easiest way to start with AJAX is to get some library for example jQuery
$.get('ajax/test.html', function(data) {
alert(data);
});
This code allow us to download from HTML page some content from URL 'ajax/test.html'.
Second argument is callback function. Variable data has content of page 'ajax/test.html'
More: http://api.jquery.com/jQuery.get/

Repopulating database driven textbox on form

I have a database driven textbox that needs to be repopulated when the user hits the back button (a back button that I have created on the form)
Currently, I am able to repopulate non database driven fields on the form using http sessions just fine. I cannot seem to apply to same logic to database driven fields.
The code on my jsp looks as follows:
<td><select name = "actionType" tabindex = "1" value="<%if(session.getAttribute("actionType")== null) out.print(""); else out.print(session.getAttribute("actionType"));%>">
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>>
<%=actType%>
</option>
<%
} %>
</select></td>
Any ideas?
The issue doesn't have anything to do with the fact that the value comes from the database, the problem is that specifying a value on a select tag won't preselect the option. You need to add a "selected" flag to the option itself.
The following should work:
<td><select name = "actionType" tabindex = "1" >
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>
<% if (session.getAttribute("actionType") == actTypeVal) {
System.out.println("selected = 'true'");
} %>
>
<%=actType%>
</option>
<%
} %>
</select></td>

Categories

Resources