issues passing dropdown value to java - java

I have a bunch of select tags in my page where some of them allows the user to use the dropdown and some of them will be disabled at a given time. so I have a select tag in my jsp such as:
<html:select name="myobject" property="myfield" disabled="$(isDisabled ? 'disabled' : '')"/>
I wanted to set as readonly a select tag on my jsp but apparently is not possible so I had to put disable. Since disabled values are not passed back to the application when a user submits the action I created a hidden object of it to pass it as it's suggested everywhere to work around that...
<html:hidden name="myobject" property="myfield" indexed="true"/>
The problem is.. when the form is submited I don't get the new dropdown value selected by the user, I debug into my java code and what I receive is the value that was originally sent to the page instead of what the user picked. It works if I removed the hidden field but if I do so then the disabled selections won't displayed when refreshed cause disabled fields don't pass back the values and i'll receive null at my end... how do I fix this problem?
Thanks,

There may be a duplicate of name or property of the html hidden component.

Related

To prevent Null value entry in JSP page

I've been working on a simple project. Getting data from JSP using text box, then using the Java in backend to save it in the database. I've defined the fields as NOT NULL in database, but the problem is when I click on submit, the data is saved in database as "", I mean it doesnt take it as NULL and it stored it as BLANK or "". Is there any way to prevent the submit action even if any one field is NULL or without entry? I know I can check it in backend Java, but I've to check every value as NOT NULL if I do by this way. Stopping the Submit action if anyone of the field is NULL is a decent choice.
Try using required="true" for the fields you do not want to allow to be empty. For example:
<h:inputText id="user" styleClass="txtinput" value="#{login.user}" required="true"/>
You cannot control the value being sent unless you use the framework(e.g struts,spring,jsf) specific tags and it has inbuilt validation on the client side.
There is nothing called as null on the form.Everything goes as String.
You have to write client side validations for Blank or "" and check for this value before submission.
If the value is Blank or "" then do not submit.

How to maintain the contents in the text field after redircting to same page jsp.

Hi I have jsp page That is having some text fields,I will fill that text fields and I will submit that form to server side.If any error comes in the server side I will redirect to the same page with error message Now I want that text field to remain as it is but in my case it is clearing.How to make the text field as same.
The two options available to you are:
Don't reload the page. Instead submit the data via AJAX or validate it by javascript. That way you don't ever have to touch the data in the form. Make sure your javascript highlights the fields that have errors in some way.
The endpoint that you're POSTing your data to needs to be able to recognise that the data is invalid and include that data when returning the user to the same page. I'm not familiar with jsp, but generally you'd do that by including variables in your template for your form that could contain the data, and passing empty strings on the first load of the page. If the page is then shown to the user again after a failed form validation, pass back the POST data that you received in your form request.
There are two option, you can dispatch the request/response to the same page instead of redirect, but you need to add an attribute and recover it in the JSP, or you can add the attribute in the session , recover the value to de text field and remove it using (if you are using JSTL)

JSP drop down list to select first option by default on page load using struts

Hope someone can help me out on this.
I've got the following drop down list in JSP:
<html:select styleId="facilityId" property="facilityId">
<html:options collection="<%=Session.CONSUMER_FACILITY%>"
property="id" labelProperty="facilityDescription"
/>
</html:select>
That gets a collection of list to be displayed in the drop down from the Java class perfectly.
But now when I open the page where it shows the drop down list, how can I make the first option selected by default on page load, without clicking and selecting a option?
Can I do something inside that JSP code or some Javascript that can do that?
I'm using Struts.
Hope this makes sense what i'm looking for.
Set facilityId value to expected default during ActionForm initialization.
Guess you're using Struts 1.x

zk Listbox ListCell textbox password getValue

hello i have a zul like this
<listcell sclass="textColumn"><textbox type="password" value="*+*+*+*+"/></listcell>
<listcell sclass="textColumn"><textbox type="password" value="*+*+*+*+"/></listcell>
as you can see i am setting the value of textbox value="++++" on records coming from the DB but i have a button set a new record on it using this code
model.add(clazz);
of course after the rendering i have cleaning both inputs values to null.[when adding a new record]
when i try to save the records on DB i am checking in zul the values of the 2 inputs and i getting the right values the user input values but when i get the listBox in Java Code i am getting still "++++" on both inputs this is driving me nuts
here is my code
(Textbox)((Listcell)(item.getChildren().get(5))).getFirstChild()).getValue();
(Textbox)((Listcell)(item.getChildren().get(6))).getFirstChild()).getValue();
both are returning "++++" on other hand the previously added items from DB i can modify and i can see the new values the problem comes when i add a new item set to null my question is
((Textbox)((Listcell)(item.getChildren().get(5))).getFirstChild()).setValue(null);
((Textbox)((Listcell)(item.getChildren().get(6))).getFirstChild()).setValue(null);
if this is not working why i am checking null values on afterRender.
this is the values i am receiving
onAfterRender:start[after set the values to null both empty this is in .zul]
onAfterRender:end
onBeforeSend:start[after the inputs of the user before going to Controller this is in .zul].
qwerty qwerty
onBeforeSend:end
inJava:start[the values i am receiving in the listbox before going to save them in this is in .Java]
*+*+*+*+<--Java-->*+*+*+*+
inJava:end
Why are you using MVC ? You can do this in MVVM very easily See this ZK Listbox may be it will help you to USE MVVM for Item rendered you can check this exampleZK Listbox ItemRendered Example

dynamically update textbox value from database when combobox value is changed

Here is the situation. I have a drop down menu. The option sin this drop down menu are being populated by fetching some values from the database. To do this following is what i have done.. :-
<select name="product_list" onchange="selectProduct(this.value)">
<option value="none">Select one</option>
<%
List<String> options = new ArrayList<String>();
DynamicCombo comboBox = new DynamicCombo();
options = comboBox.generateComboBox();
Collections.sort(options);
int tempVar = 0;
while (tempVar < options.size()) {
out.print("<option value=\"");
out.print(options.get(tempVar));
out.print("\">");
out.print(options.get(tempVar));
out.print("</option>");
tempVar++;
}
%>
</select>
DynamicCombo is a class that has a method called 'generateComboBox()'. This method simply returns an array list containing all the values that are fetched from the database, which is what i need to show in my drop down box in the front end (jsp page). On my jsp page i simply iterate through this list and print it as options appropriately.
This works absolutely fine.
Now i have another text box on my form, say 'textbox1'. Now the requirement is that this text box value should be updated depending on what the user has selected from the above drop down box.
So for example if the user selects 'prod1'(which is a primary key in the backend database table) option from the drop down box, then the corresponding value ( the product name) should be fetched from the database table and should be updated in the textbox named 'textbox1'.
The other thing is this entire thing is contained in a form which is supposed to be finally submitted to the servlet for further processing.
So how can i achieve this.
i figured out the solution to my own problem. It might not be the most elegant way of doing it, but it does the job pretty well.
So as per my requirement, what i exactly wanted to do was.... insert a value (that will be fetched from the database) into a text box on my form depending on what the user chooses from the drop down box that is already present on my form.
To achieve this, i went about and thought if some how i could nest a form withing my main form, it'd solve my issue. But i discovered that nesting of forms is not allowed. So the next option i thought of was to some how submit the same form without the user clicking on the submit button and also handle it appropriately as an 'incomplete' submit (in the sense that the form is still to be submitted manually by the user by clicking on the submit button) on the server.
So i simply made use of the 'onChange' event of a drop down box. I created an additional hidden field on my form.I wrote a simple javascript function that would simply set the value of the hidden field to the string-"partial Submit" and would submit my main form (say named 'form1') as :-
document.getElementById("hidden_id").setAttribute("value","partial submit");
form1.submit;
The function that does the above will be called whenever (and everytime) the onchange event of the drop down box gets fired.
When the user finally clicks on the submit button on the form to submit the finally completed form, then another javascript function is called that simply sets the value of the hidden field on the form to the string, "final submit" and would submit the form as :-
document.getElementById("hidden_id").setAttribute("value","final submit");
form1.submit;
Now on my server, i checked for the value of this hidden field as :-
if(request.getParameter("hidden_id").equals("partial Submit"))
{
// make a database connection, pass the value user selected from the drop down box
// to a prepared statement that does the query for getting the 'productName' from
// the database, collect the returned string in a variable and set a
// request attribute with this returned value. This value can simply be used in the
// jsp to fill in the value part of the textbox1.
}
else
{
if(request.getParameter("hidden_id").equals("final Submit"))
{
// do the rest of the final processing that needs to be done when user finally
// submits the completed form.
}
else
{
// throw an exception to take care of the possibility that the user might send
// in a 3rd value as a value for the hidden field.
}
}
Since you havent provided the code for selectProduct(this.value) , i presume that it submits the jsp page as when you change the value in the drop down.
If that the case in the servelt, set the value that you want to show in jsp in request object
request.setAttribute("valuetodisplay" ,valuetodisplay);
and now in jsp
<input type="text" value ='<%= request.getAttribute("valuetodisplay")%>' />

Categories