Autofill a form based on DB content - java

I want to autofill a form value based on value I have selected before. When I select a product id and all of my product info should be filled. How to implement this? I want to auto complete product as well. I am using JSP but the problem is that I can get product id and I can get value in JS variable. So how can I assign JS to JSP variable and fetch content from DB.

Related

how to retrive textfield value in selenium if the value is not present in value attributes?

I wants to verify value that is present in Display Name field.
After entering First Name Display Name will populates automatically.
display name value is present in front-end but not present in DOM.
I tried with
driver.webdriver.findElement(By.id("DisplayName")).getText();
and
String abc= driver.webdriver.findElement(By.id("DisplayName")).getAttribute("value");
driver.findElement(By.xpath(".//*[#id='DisplayName']")).getAttribute("value");
Hi this works for me...

Spring MVC. Controller and JSP collaboration

I have jsp page which contains tab control. Each tab has category name as a caption and contains a table with information about groups of selected category. Clicking on the tab should make a query in the DB and populate the table in the tab with a list of category groups. So I have a method in controller
public List<Group> getGroups(int idCategory) {
return groupService.getList(idCategory);
}
How can I call this method in jsp?
The only way I see is to put a Map in the ModelMap where a key is a category Id and a value is a list of Groups. But I don't like this idea because we need to make all queries to fill the map, but we may not need some of them if we don't click all the tabs.
If you don't want to load all the data in the controller when you render the page, then you should probably load only the data for the tab which will be displayed initially and retrieve the data for the rest of the tabs with AJAX calls as needed.
For this you would have to have some client side logic in JavaScript and probably some dedicated controller on the backend which will return the groups for given category ID for example in JSON or XML.
The only way is with the model. If you want to use it that way you should make a call using ajax and then manipulating the data with javascript or a plugin for tables(DataTables for example).

How to select the particular row data in jsp using radio button

I know its a basic question and please bear with this.
I'm new to struts frame work and I'm creating a sample application using struts 1.2. I've inserted few values into the database using form beans. And i'm displaying all the database records in the jsp page using function. and i also placed the radio button for each row..
Now my question is, i want to select a particular data from the records displayed in the jsp page by clicking the radio button and i need to store the selected records into the another table.
Give me some suggestion regarding the condition specified above...
Thanks in advance..
Once you set those unique values in radio button, you can get the values for your purpose. Like using jquery,
var selRadio = $('input:radio[name=cname]:checked').val();
You can find out fiddle explains how to get checked value.
Use some ajax for send that value to server (servlet) and add the values to new table without refresh entire page.
$.ajax({
url : "ur_servlet_url" + selRadio,
type : "POST",
async : false,
success : function(data) {
alert("success");
}
});
Let me know if this helps.

Sending the selected rows from datatable to action method?

i am using data table to show the customer records on jsp page where i show customer name,last name,department,joining date in data table.
i have one more column which is select box. now user can user select any number of customer records from ui and send to action on server side for update. In my
jquery function i get the selected rows in data table. But i am not able to figure out how to send the these rows to action method?
what i am expecting is i send these rows from ajax to action method which has one method parameter as list(representing the selected rows) and i can extract field values from each row in list representing selected rows from UI.
But i am unsure what will be type of object that list will contain?Can someone point me in right direction with some brief example ?
Why dont you give a checkbox in each row whose value will be the id of the corresponding record
<input type="checkbox" name="rec" value="{id of the record}"/>
Then when you submit get the selected ids
var selectedRec="";
$("input[name='rec']:checked").each(function(){
selectedRec += $(this).val()+",";
});
you can now pass selectedRec to the action as a string variable. In your action you can split this string as array
String[] recArray = selectedRec.split(",");
Then in the loop you can get each id as follows
int customerId = Integer.parseInt(recArray[i]);
This is another way to do, where you don't need to iterate your selected record in JQuery and create a string in Script.
Step 1. Create checkbox
<s:checkbox name="Bean.record" id="record" fieldValue="%{id of the record}" />
Step 2. Create String[] in your Bean class
private String[] record;
That's all.... so whatever record you will select on your JSP, value will be added into String Array and you can grab it on your action.
Hope this will help.

How to select html dropdown value after filteration based on previous dropdown value

I have two dropdowns in html. Both dropdowns are getting data mysql first dropdown is "hostgroup" having value like "windows,linux" and second dropdown is "host" having value like"office,home,sidearea,localhost"
Basically I want to do it so when I select "linux" is hostgroup combobox it will filter "host" dropdown box and show only localhost in "host" dropdown and when I select "windows" in hostgroup dropdown it will filter it and remove localhost from host dropdown
My code for filling dropdown in html is,
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshostgroup.next())
{
html.append("<option value='"+rshostgroup.getString(2)+"'>"+rshostgroup.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
html.append("</td>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>");
html.append("Host");
html.append("</td>");
html.append("<td>");
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshost.next())
{
html.append("<option value='"+rshost.getString(2)+"'>"+rshost.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
Please dont get confuse that what html.append is and what is option value comming from
Actually I am using string builder appending my string builder html in .java(class) file and calling class html appended method in jsp. It is working fine and the option value of drop down is filling from ResultSet rs and rshost having data from mysql.
Firstly using java to build HTML element is a terribly bad idea.
Use JSTL to loop through the dropdown options instead of using Java to spit out HTML code.
Secondly the problem you mention has nothing to do with Java and everything to do with JavaScript.
Well, not entirely true as you might have to make an AJAX request to server get values to populate your secondary dropdown box.
What you need to do is attach a onChange listener for the primary dropdown so that when it changes your javascript code either makes a AJAX call to server to get values for your secondary dropdown box, or use hardcoded values held in some javascript variables.
You will have to loop through the secondary dropdown to remove all previous values and add new dropdown options to it or alternately you can remove the secondary dropdown box entirely and recreate it with the new values based on the selection of the primary dropdown box.
There's already more than enough articles in the internet so use your fiend Google to find the ones that suits your needs closely.

Categories

Resources