I can't figure out why i can't get an input value from a jsp. I'm using for cycle to make several input fiels for "choices", but when i'm trying to get values from a mvcportlet, it get nothing.
<aui:form action="<%=addPollURL%>">
<aui:fieldset>
<%
int optionCount = Integer.parseInt(optionCountS);
for (int i = 0; i < optionCount; i++) {
%>
<aui:input label="<%=Integer.toString(i + 1)%>" name="choice<%=i%>"
type="text" />
<%
}
%>
<aui:button-row>
<aui:button value="Add poll" type="submit" />
</aui:button-row>
</aui:fieldset>
</aui:form>
Here goes mvcportlet method
List<String> choices = new ArrayList<String>();
int count = Integer.parseInt(actualChoiceCount);
for (int i = 0; i < count; i++) {
System.err
.println("another choice"
+ ParamUtil
.getString(
actionRequest,
("choice" + i)));
choices.add(new String(ParamUtil.getString(actionRequest,
("choice" + i))));
}
Its really weird... but some ideas or tests
Is AddPollUrl an Action URL with named action and so your action is executed in your generic portlet?
Are you sure text fields are populated with values in the UI (there is no no explicit value in tag)? ParamUtil output would be the same without value that with a blank value
Try without type=text and write it as a single line (input tags)
Try aui:submit instead of aui:button type submit
Try adding an id to form or fields (Ive seen some problems with repeated forms if they dont have namespace)
Why new String(ParamUtil...)?
the most important... whats the output of your System.outs?
That happens because your input field has no value. Or at least it seems so.
You should modify the input to have the value parameter set to "choiceX" like:
<aui:input label="<%=Integer.toString(i + 1)%>" name="choice<%=i%>"
type="text" value="choice<%=i%>" />
Then you'll find it in actionRequest, like Jonny said:
request.getParameter("choice"+i);
This will return you the value of the input field, searching by it's name. So you can have your choice in the processAction method.
Regards !
Try using:
actionRequest.getParameter("choice" + i);
That's not the standard way of getting POST params from the request.
Related
I am having chicken and egg situation:
i have 2 servlets and i want the di object to show through search, not by display servlet
Question:
Is there way I can show the refID in my jsp page even if object is
empty? currently, In my code, i added default query just to fill the "dmsearch" so Jsp don't give
error(SearchDataManagerController.searchDMData(0, 0, 0);).
Or any other solution so my search value should show even display servlet is loaded?
Problem:
On page load, i don't necessarily want to show refId data (servlet 1). if it displays its ok BUT when i click on Search, it should display the data(servlet 2). it displays searched value in textbox but Servlet 1 loads and reset the value back.
Using "Get" in jsp
Code:
<% DataManager di = (DataManager) session.getAttribute("dmsearch"); %>
<input type="text" name="refId" id="refId" value ="<%= di.getiD() %>">
//Servlet display: 1:
DataManager dm;
String buttonClickStatus = request.getParameter("buttonClickStatus");
dm = SearchDataManagerController.searchDMData(0, 0, 0);
session.setAttribute("dmsearch", dm);
//Servlet search: 2:
dm = SearchDataManagerController.searchDMData(driverid, textid, weekid);
session.setAttribute("dmsearch", dm);
please help
Thank you
Solution:
DataManager dm = new DataManager();
int textid=0;
int driverid=0;
int weekid=0;
if(request.getParameter("textid")!=null) {
textid= Integer.parseInt(request.getParameter("textid"));
}
if(request.getParameter("driverid")!=null) {
driverid= Integer.parseInt(request.getParameter("driverid"));
}
if(request.getParameter("weekid")!=null) {
weekid= Integer.parseInt(request.getParameter("weekid"));
}
I'm not sure if this will help you but there is the Expression Language you can read about it, it's so usefull to avoid java code in jsp pages.
You can try this but I'm not sure if this is what you want:
<input type="text" name="refId" id="refId" value ="${ empty di ? 1 : di.getId()}">
Solution:
DataManager dm = new DataManager();
int textid=0;
int driverid=0;
int weekid=0;
if(request.getParameter("textid")!=null) {
textid= Integer.parseInt(request.getParameter("textid"));
}
if(request.getParameter("driverid")!=null) {
driverid= Integer.parseInt(request.getParameter("driverid"));
}
if(request.getParameter("weekid")!=null) {
weekid= Integer.parseInt(request.getParameter("weekid"));
}
I am using Coded UI for creating some test cases for a web application, while doing the same I have encountered an issue. I am not able to select a Radio Button using their Displayed Text, however if I use the ValueAttribute then its working fine. But, since value attribute is not containing a number which may not be of any logical use for a person creating test data, so I need to do same work using the Displayed Text of the Radio button.
Here is my html code
<td><input id="ContentPlaceHolder1_rbl_NewChanged_0" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1131">
<label for="ContentPlaceHolder1_rbl_NewChanged_0">New</label></td>
<td><input id="ContentPlaceHolder1_rbl_NewChanged_1" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1132">
<label for="ContentPlaceHolder1_rbl_NewChanged_1">Changed</label></td>
<td><input id="ContentPlaceHolder1_rbl_NewChanged_2" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1133">
<label for="ContentPlaceHolder1_rbl_NewChanged_2">Longstanding</label></td>
I have tried the following code. but didn't work
String selectType = data.getType().get(rowCnt);// data reading from excel stored to string variable
List<WebElement> type = driver.findElements(By.xpath("//input[#type='radio']"));
for (int i = 0; i < type.size(); i++) {
if (type.get(i).getText().equals(selectType)) {
type.get(i).click();
}
}
If you are getting the String values pertaining to the text of the <label> tags e.g. New, Changed, Longstanding. etc from excel then you can write a function which will acccept the texts as String as follows:
public void clickItem(String itemText)
{
driver.findElement(By.xpath("//td//label[starts-with(#for,'ContentPlaceHolder1_rbl_NewChanged_')][.='" + itemText + "']")).click();
}
Now you can call the function clickItem() by passing the String argument to click on the relevant Radio Button as follows:
String selectType = data.getType().get(rowCnt); // data reading from excel stored to string variable
clickItem(selectType); // will support clickItem("New"), clickItem("Changed") and clickItem("Longstanding")
Note: As per the HTML you have provided the clickItem() method should work with the implemented Locator Strategy. As an alternative you can also replace the clickItem() function to click on the <input> node as follows:
public void clickItem(String itemText)
{
driver.findElement(By.xpath("//td//label[.='" + itemText + "']//preceding::input[1]")).click();
}
Using Java Servlet 3.0 file upload, how can I get the names of files when I only know the name of the input?
For instance, I will have several inputs which are type="file", and will have the multiple attribute. At the time I'll need the file names, I'll know the input name, but I won't know the file names. I know how to get a file name from the header with substring, I just don't know how to get it from a specific input. It doesn't matter what order I get the filenames, I just need them from the correct input and put into an array for later processing. I've been stuck on this for about 3 weeks and can't find an answer, which probably means I'm looking for the wrong thing.
My inputs will be like this:
<input type="file" name="pic1" multiple="multiple">
<input type="file" name="pic2" multiple="multiple">
<input type="file" name="pic3" multiple="multiple">
And the code:
function setFileNames(arg, num){
for (c=0; c<=11;c++){
var d = c+1;
var key = arg.getAttribute('id');
var number = key.charAt(key.length-1);
var FileName = document.getElementById("FileName" + d + "-" + number);
var f = document.getElementById("pic" + number);
var name = f.files.item(c).name;
FileName.value = name;
}return;
}
I have a form with a button that will dynamically add a group of inputs of the same form.
I already managed to get it done, except for one issue.
I couldn't pass the parameters from radio input type with the same name to the servlet for everytime I add the fields. It only passed the value to servlet once. Weird thing is I could pass the text input type successfully.
Or is there any other way to pass the value from the radio button to the servlet?
Here's the code:
<script type="text/javascript">
$(document).ready(function(){
var counter = 2;
$("#addDynamicDivs").click(function () {
var newTextBoxDiv1 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv1');
newTextBoxDiv1.attr("style",'float: left;');
var newTextBoxDiv2 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv2');
newTextBoxDiv2.attr("style",'float: left;');
var newTextBoxDiv3 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv3');
newTextBoxDiv3.attr("style",'float: left;');
var newTextBoxDiv4 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv4');
newTextBoxDiv4.attr("style",'float: left;');
newTextBoxDiv1.after().html('<label>Speaker Name : </label>' +
'<input type="text" name="speakername" id="speakername" value="" >');
newTextBoxDiv2.after().html('<label>Speaker Country : </label>' +
'<input type="text" name="speakercountry" id="speakercountry" value="" >');
newTextBoxDiv3.after().html('<label>Speaker Company : </label>' +
'<input type="text" name="speakercompany" id="speakercompany" value="" >');
newTextBoxDiv4.after().html('<label>ID Type: </label>' +
'<ul name="idtype class="forms-list">'+
'<li><input type="radio" name="idtype" id="idtype" value="New ID">'+
'<label for="New ID">New ID</label></li>'+
'<li><input type="radio" name="idtype" id="idtype" value="Old ID">'+
'<label for="Old ID">Old ID</label></li></ul>');
newTextBoxDiv1.appendTo("#TextBoxesGroup");
newTextBoxDiv2.appendTo("#TextBoxesGroup");
newTextBoxDiv3.appendTo("#TextBoxesGroup");
newTextBoxDiv4.appendTo("#TextBoxesGroup");
});
});
From the servlet, the parameters are retrieved by this code:
String[] speakername = request.getParameterValues("speakername");
String[] speakercountry = request.getParameterValues("speakercountry");
String[] speakercompany = request.getParameterValues("speakercompany");
String[] idtype = request.getParameterValues("idtype");
I print out the length of each String array above, and I got 2 for each of the parameters except for idtype which the length is 1.
All the dynamic params are already included inside the form.
Radio buttons will usually only send one value out of the group with the request. By design, only one radio button can be selected out of the group. If you add more radio buttons with the same name to the form, the browser should still pass only one selected value for the group when you submit the form.
If you want to have multiple of this form passed, you will be best off differentiating between the dynamically added forms (it looks like you want all the data to be sent together, so you'll need to add a unique identifier to the name of each <input> element, but otherwise having them in separate <form>s would be preferable). I would not recommend relying on the browser passing multiple <input>s with the same name in the same <form>.
I'm trying to use Selenium WebDriver to input text to a GWT input element that has default text, "Enter User ID". Here are a few ways I've tried to get this to work:
searchField.click();
if(!searchField.getAttribute("value").isEmpty()) {
// clear field, if not already empty
searchField.clear();
}
if(!searchField.getAttribute("value").isEmpty()) {
// if it still didn't clear, click away and click back
externalLinksHeader.click();
searchField.click();
}
searchField.sendKeys(username);
The strange thing is the above this only works some of the time. Sometimes, it ends up searching for "Enter User IDus", basically beginning to type "username" after the default text -- and not even finishing that.
Any other better, more reliable ways to clear out default text from a GWT element?
Edited to add: The HTML of the input element. Unfortunately, there's not much to see, thanks to the JS/GWT hotness. Here's the field when it's unselected:
<input type="text" class="gwt-TextBox empty" maxlength="40">
After I've clicked it and given it focus manually, the default text and the "empty" class are removed.
The JS to setDefaultText() gets called both onBlur() and onChange() if the change results in an empty text field. Guess that's why the searchField.clear() isn't helping.
I've also stepped through this method in debug mode, and in that case, it never works. When run normally, it works the majority of the time. I can't say why, though.
Okay, the script obviously kicks in when the clear() method clears the input and leaves it empty. The solutions it came up with are given below.
The naïve one, presses Backspace 10 times:
String b = Keys.BACK_SPACE.toString();
searchField.sendKeys(b+b+b+b+b+b+b+b+b+b + username);
(StringUtils.repeat() from Apache Commons Lang or Google Guava's Strings.repeat() may come in handy)
The nicer one using Ctrl+A, Delete:
String del = Keys.chord(Keys.CONTROL, "a") + Keys.DELETE;
searchField.sendKeys(del + username);
Deleting the content of the input via JavaScript:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].value = '';", searchField);
searchField.sendKeys(username);
Setting the value of the input via JavaScript altogether:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].value = '" + username + "';", searchField);
Note that javascript might not always work, as shown here: Why can't I clear an input field with javascript?
For what it is worth I'm have a very similar issue. WebDriver 2.28.0 and FireFox 18.0.1
I'm also using GWT but can reproduce it with simple HTML/JS:
<html>
<body>
<div>
<h3>Box one</h3>
<input id="boxOne" type="text" onfocus="if (this.value == 'foo') this.value = '';" onblur="if (this.value == '') this.value = 'foo';"/>
</div>
<div>
<h3>Box two</h3>
<input id="boxTwo" type="text" />
</div>
</body>
</html>
This test fails most of the time:
#Test
public void testTextFocusBlurDirect() throws Exception {
FirefoxDriver driver = new FirefoxDriver();
driver.navigate().to(getClass().getResource("/TestTextFocusBlur.html"));
for (int i = 0; i < 200; i++) {
String magic = "test" + System.currentTimeMillis();
driver.findElementById("boxOne").clear();
Thread.sleep(100);
driver.findElementById("boxOne").sendKeys(magic);
Thread.sleep(100);
driver.findElementById("boxTwo").clear();
Thread.sleep(100);
driver.findElementById("boxTwo").sendKeys("" + i);
Thread.sleep(100);
assertEquals(magic, driver.findElementById("boxOne").getAttribute("value"));
}
driver.quit();
}
It could just be the OS taking focus away from the browser in a way WebDriver can't control. We don't seem to get this issue on the CI server to maybe that is the case.
I cannot add a comment yet, so I am putting it as an answer here. I want to inform you that if you want to use only javascript to clear and/or edit an input text field, then the javascript approach given by #slanec will not work. Here is an example: Why can't I clear an input field with javascript?
In case you use c# then solution would be :
// provide some text
webElement.SendKeys("aa");
// this is how you use this in C# , VS
String b = Keys.Backspace.ToString();
// then provide back space few times
webElement.SendKeys(b + b + b + b + b + b + b + b + b + b);