How can i pass these radio button values to my controller? I know i can use
(#RequestParam("radioButtonName") String radioButtonValue) {
But how could i use this if my "radioButtonNames" is always different? I have a lot of question and i am using foreach for this.
<table >
<tr th:each="question : ${questions}">
<!-- Question field -->
<td th:value="${question.qst_id}" th:text="${question.qst_title}"></td>
<!-- Answer field -->
<td th:text="${answers[0].answ_title}"></td>
<td>
<table >
<tr>
<td>1<input type="radio" th:name="${question.qst_id}" th:value="${answers[0].answ_id}"></td>
<td>2<input type="radio" th:name="${question.qst_id}" th:value="${answers[1].answ_id}"></td>
<td>3<input type="radio" th:name="${question.qst_id}" th:value="${answers[2].answ_id}"></td>
<td>4<input type="radio" th:name="${question.qst_id}" th:value="${answers[3].answ_id}"></td>
<td>5<input type="radio" th:name="${question.qst_id}" th:value="${answers[4].answ_id}"></td>
</tr>
</table>
</td>
<td th:text="${answers[4].answ_title}"></td>
</tr>
<tr>
<td><button type="submit">Save</button></td>
</tr>
</table>
You can use th:field for storing your radio button value and create a column of radio button in your model something like this.
<tr>
<td><label for = "gender">Gender</label></td>
<td><input type = "radio" name = "gender" th: field = "* {gender}" value = "male" />
<label for = "radioA">Male</label>
<input type = "radio" name = "gender" th: field = "* {gender}" value = "female" />
<label for = "radioB">Women</label>
</td>
</tr>
Related
I want to enter values to a group of text fields using selenium in a table as shown below.
I tried in this way but it didn't work and raise an InvalidElementStateException.
List<WebElement> marks = driver.findElements(By.xpath(".//table/tbody/tr/td/input"));
for (WebElement mark : marks) {
mark.sendKeys("10");
}
EDIT :
In my html, the td contains an extra hidden inputs also.
<tbody>
<tr class="text-center student-mark" id="1">
<td class="text-left">Name</td>
<td class="activityTableBody hidden" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_classTest"
id="55630af2a0c4655a1ce06fcd_classTest_556da05c699e70287ca203b0_classTest" value="0">
</td>
<td class="activityTableBody" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_activity1"
id="55630af2a0c4655a1ce06fcd_activity1_556da05c699e70287ca203b0_Task-1" value="0">
</td>
<td class="activityTableBody" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_activity2"
id="55630af2a0c4655a1ce06fcd_activity2_556da05c699e70287ca203b0_Task-1" value="0">
</td>
<td class="activityTableBody">
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_activity3"
id="55630af2a0c4655a1ce06fcd_activity3_556da05c699e70287ca203b0_Task-1" value="0">
</td>
<td class="activityTableBody" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_activity4"
id="55630af2a0c4655a1ce06fcd_activity4_556da05c699e70287ca203b0_Task-1" value="0">
</td>
<td class="activityTableBody" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_activity5"
id="55630af2a0c4655a1ce06fcd_activity5_556da05c699e70287ca203b0_Task-1" value="0">
</td>
<td class="tableBodySA hidden" >
<input type="text" class="table-input 55630af2a0c4655a1ce06fcd_markSA" value="0">
</td>
<td class="tableBodySA hidden" *emphasized text*>
<input type="text" class="table-input saMark 55630af2a0c4655a1ce06fcd_markSA100" width="20%" value="0">
</td>
</tr>
.
.
</tbody>
You should select the input in a td which does not have class 'hidden'
List<WebElement> marks = driver.findElements(By.xpath(
".//table/tbody/tr/td[contains(#class ,'activityTableBody' ) and not(contains (#class ,'hidden'))]/input"));
for (WebElement mark : marks) {
Integer studentMark = i++ % 6;
System.out.println(studentMark);
mark.click();
mark.sendKeys(Integer.toString(studentMark));
}
This working example (from the HTML you provied) fills all the visible cells :
WebDriver driver = new FirefoxDriver();
driver.get("https://fiddle.jshell.net/40vr7top/show");
driver.switchTo().frame(0);
List<WebElement> inputs = driver.findElements(By.cssSelector("td.activityTableBody input"));
for (WebElement input : inputs) {
input.clear();
input.sendKeys("99");
}
Not sure the best way to do this...
I have an HTML FORM with multiple rows.. But one of the columns also has multiple values.
I know I can receive the event into a String array with getParamaterValues(). But for the variable days, how do I get that into an array of arrays so it's still related to the event column?
It's all coming out of a database so I can't just hard code it.
I'm sure I actually know the answer but my brain is not cooperating... Thanks in advance for the kick-start... I'm using Java servlets. But it's really just an HTML question.
e.g.
<tr>
<td>
<input type="checkbox" name="event" value="ev1">
<td>
<input type="checkbox" name="days" value="Sat">
<input type="checkbox" name="days" value="Sun">
<tr>
<td>
<input type="checkbox" name="event" value="ev2">
<td>
<input type="checkbox" name="days" value="Fri">
<input type="checkbox" name="days" value="Sat">
<input type="checkbox" name="days" value="Sun">
<tr>
<td>
<input type="checkbox" name="event" value="ev3">
<td>
<input type="checkbox" name="days" value="Sat">
<input type="checkbox" name="days" value="Sun">
<input type="checkbox" name="days" value="Mon">
Append the value from the events to the name attribute of the days like ev1_days, ev2_days, ev3_days:
<tr>
<td>
<input type="checkbox" name="event" value="ev1">
</td>
<td>
<input type="checkbox" name="ev1_days" value="Sat">
<input type="checkbox" name="ev1_days" value="Sun">
</td>
</tr>
...
...
Then you can do something like:
String[] events = request.getParamaterValues("event");
for(int i=0; i<events.length; i++)
{
String[] days = request.getParamaterValues(events[i] + "_days");
//do something with days....
}
You should actually close your td's and tr's by the way.
You will have to change name of Checkboxes that belongs to the same event to a unique name.
HTML
<tr>
<td>
<input type="checkbox" name="event" value="ev1">
<td>
<input type="checkbox" name="daysA" value="Sat">
<input type="checkbox" name="daysA" value="Sun">
<tr>
<td>
<input type="checkbox" name="event" value="ev2">
<td>
<input type="checkbox" name="daysB" value="Fri">
<input type="checkbox" name="daysB" value="Sat">
<input type="checkbox" name="daysB" value="Sun">
<tr>
<td>
<input type="checkbox" name="event" value="ev3">
<td>
<input type="checkbox" name="daysC" value="Sat">
<input type="checkbox" name="daysC" value="Sun">
<input type="checkbox" name="daysC" value="Mon">
Script
var events = [];
var daysA = [];
var daysB = [];
var daysC = [];
var jsonData="";
function c(){
var eventCB = document.getElementsByName("event");
var daysACB = document.getElementsByName("daysA");
var daysBCB = document.getElementsByName("daysB");
var daysCCB = document.getElementsByName("daysC");
for(var i=0;i<eventCB.length;++i){
events.push(eventCB[i].value);
}
for(var i=0;i<daysACB.length;++i){
daysA.push(daysACB[i].value);
}
for(var i=0;i<daysBCB.length;++i){
daysB.push(daysBCB[i].value);
}
for(var i=0;i<daysCCB.length;++i){
daysC.push(daysCCB[i].value);
}
jsonData ='{events:['+events.toString()+'],days:[['+daysA.toString()+'],['+daysB.toString()+'],['+daysC.toString()+']]}'
}
Passing data to Servlet
var jsonComplete = JSON.stringify(jsonData);//You will get below string
//{events:[ev1,ev2,ev3],days:[[Sat,Sun],[Fri,Sat,Sun],[Sat,Sun,Mon]]}
$.ajax({
url:"URLServlet",
type:"POST",
dataType:'json',
data: {jsonComplete : jsonComplete },
success:function(data){
// do whatever required
},
});
Receiving Data in Servlet Using org.json
JSONObject objJSON = new JSONObject(request.getParameter("jsonComplete "));
JSONArray arrEvents = objJSON.getJSONArray("events");
JSONArray arrDays = objJSON.getJSONArray("days");
for(int i=0;i<arrEvents.length();++i){
out.println("event["+i+"]:"+arrEvents.getString(i));//Similar to event[0]:ev1
}
for(int i=0;i<arrDays.length();++i){
JSONArray arrDaystemp = arrDays.getJSONArray(i);
out.println("days["+i+"]:[");
for(int i=0;i<arrDaystemp.length();++i){
out.println(arrDaystemp.getString(i));//Similar to days[0]:[Mon,Tue]
}
out.println("]");
}
I am beginner developer and i'm working on a web application in Java whith struts.
I have a problem with my struts form which stay empty after user enters informations. I've tried many way to resolve it but nothing worked. Maybe the code lines will help you to help me ;).
My Jsp page :
<tr>
<td colspan="5">
<div class="content" align="center">
<form action="statistiques.do" method="POST">
<input type="hidden" name="operation" value="module1">
<tr>
<td><font size="3">État : </td>
<td>
<select name="choixEtat">
<option value='1' selected>Tous</option>
<option value='2' >Actif</option>
<option value='3' >Inactif</option>
</select>
</td>
</tr>
<tr>
<td>Date de début :<span style="font-weight: bolder; color: red;"> *</span></td>
<td>
<input type="text" name="dateDebut" size="10" maxlength="10" id="date" value=""
required="required">
</td>
</tr>
<tr>
<td>Date de fin :<span style="font-weight: bolder; color: red;"> *</span></td>
<td>
<input type="text" name="dateFin" size="10" maxlength="10" id="date2" value=""
required="required">
</td>
</tr>
<tr>
<td><font size="3">Chargé(e)(s)</td>
<td>
<select name="chargeMatricule">
<option value="0" selected></option>
<% for (Utilisateur u : utilisateurs) {%>
<option value="<%=u.getUtilisateurMatricule()%>"><%=u.getUtilisateurNom() + ", " + u.getUtilisateurPrenom()%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<td><font size="3">Activité : </td>
<td>
<select name="activiteNom">
<option value="0" selected></option>
<% for (String s : activites) {%>
<option value="<%=s%>"><%=s%></option>
<%}%>
</select>
</td>
</tr>
<td colspan='5' align="center">
<input type="submit">
</td>
</tr>
</form>
</div>
</td>
and my action :
public class ActionStatistiques extends DispatchAction {
public ActionForward module1(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FormStatMod1 forms = (FormStatMod1) form;
ServiceStatistiques service = new ServiceStatistiques();
String dd = forms.getDateDebut();
String dateDebut = dd.substring(6, 10) + '-' + dd.substring(3, 5) + '-' + dd.substring(0, 2);
String df = forms.getDateFin();
String dateFin = df.substring(6, 10) + '-' + df.substring(3, 5) + '-' + df.substring(0, 2);
Collection[] collections = service.module1(forms.getChoixEtat(), dateDebut, dateFin);
Collection<Flux> listef = collections[1];
String activiteNom = forms.getActiviteNom();
String chargeMatricule = forms.getChargeMatricule();
Hope, someone will see what's wrong.
Have a nice day!!
Use <html:form>
Here is the example - http://struts.apache.org/release/1.3.x/faqs/actionForm.html
Normally the form is rendered using Struts tags but you can use JSP EL expressions to populate the input fields. For example
<input type="text" name="dateFin" size="10" maxlength="10" id="date2" value="${formStatMod1.dateFin}" required="required">
Ok i found my way!!! Like many times it was a stupid omission ... a setter missed in my form!!! Never will forget to check it.
I'm trying to submit form with post method like :
<form action="/context/controller" method="post">
<input type="hidden" name="update" value="add_record"/>
<table>
<tr>
<td>Name : </td>
<td><input type="text" name="u_name" /></td>
</tr>
<tr>
<td>Visible :</td>
<td>
<input type="radio" name="visible" value="0"/> No
<input type="radio" name="visible" value="1"/> Yes
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Add record"/>
<input type="reset" value="Reset"/></td>
</tr>
</table>
</form>
and I'm getting form data in doPost() of controller servlet as :
String updateParam = request.getParameter("update");
System.out.println("Param value : " + updateParam);
if (updateParam.equals("add_record")) {
recordDAO = new RecordDAO();
System.out.println("add if true Param value : " + updateParam);
Record record = new Record();
String name = request.getParameter("u_name");
String visible = request.getParameter("visible");
record.setName(name);
subject.setVisible(visible);
recordDAO.addRecord(record);
request.getRequestDispatcher("/view.jsp").forward(
request, response);
}
this line
System.out.println("Param value : " + updateParam);
prints Param value : add_record on the console but if condition never becomes true, thus I'm unable to add records, please tell me where is my bug which is preventing code of if condition from execution.
There is one whitespace before "add_record", that's why your condition never becomes true.
So modify your condition to if (updateParam.equals(" add_record")) { or, which I prefer, modify your parameter to output the string without whitespaces.
How will I pass the data as an ArrayList into my servlet?
<Table id="table1">
<tr>
<td>
<input type="hidden" value="test1_1" name="name1_1" />
</td>
<td>_
<input type="hidden" value="test2_1" name="name2_" />
</td>
<td>
<input type="hidden" value="test3_1" name="name3_1" />
</td>
</tr>
<tr>
<td>
<input type="hidden" value="test1_2" name="name1_2" />
</td>
<td>
<input type="hidden" value="test2_2" name="name2_2" />
</td>
<td>
<input type="hidden" value="test3_2" name="name3_2" />
</td>
</tr>
<tr>
<td>
<input type="hidden" value="test1_3" name="name1_3" />
</td>
<td>
<input type="hidden" value="test2_3" name="name2_3" />
</td>
<td>
<input type="hidden" value="test3_3" name="name3_3" />
</td>
</tr>
</Table>
List<Model> newList = new ArrayList<Model>();
The servlet newList must contains 3 Model
Model1 with value of [getSomething1 = test1_1, getSomething2 = test2_1, getSomething3 = test3_1]
Model2 with value of [getSomething1 = test1_2, getSomething2 = test2_2, getSomething3 = test3_2]
Model3 with value of [getSomething1 = test1_3, getSomething3 = test2_3, getSomething3 = test3_3]
And this will not limit to 3 Model in a list but will depends on the number of row.
It will create a number of models based on the number of row in the JSP.
You can't pass it as an ArrayList, but you can read it as a String[] if you give all the inputs you want in the array the same name.
<input name='test' />
<input name='test' />
<input name='test' />
In your servlet, use request.getParameterValues(name) (which returns a String[]) rather than request.getParameter(name) which only returns one value.
String[] testValues = request.getParameterValues("test");