Struts forms still empty - java

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.

Related

Send radio button values to Controller Spring

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>

jsp paging problem when i go to next page searching keyword doesn't apply

when i search specific word only first page is classified. it shows pages and posts well on first page.
but when i go to page 2 or next page, seaching keyword doesn't apply on
is this address problem?
i guess this is sql or Paging.java problem because when i print log of page at BDAO it shows page well which i clicked.
also I don't know how can i transfer keyWord &keyField for that..!
I use oracle DB.
<%
String keyWord = (String)request.getParameter("keyWord");
String keyField = (String)request.getParameter("keyField");
%>
<script>
function searchCheck(frm){
//검색
if(frm.keyWord.value ==""){
alert("검색 단어를 입력하세요.");
frm.keyWord.focus();
return;
}
frm.submit();
}
function PageMove(page){
var keyWord = '<%=keyWord%>'
var keyField = '<%=keyField%>'
console.log(keyWord);
if(keyWord !=''){
location.href = "list.do?page="+page+"&keyWord=" + keyWord + "&keyField=" + keyField;
}
location.href = "list.do?page="+page;
}
</script>
</head>
<body>
<table width="800" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>번호</td>
<td>이름</td>
<td>제목</td>
<td>날짜</td>
<td>히트</td>
</tr>
<c:forEach items="${list}" var="dto">
<tr>
<td>${dto.bId}</td>
<td>${dto.bName}</td>
<td>
<c:forEach begin="1" end="${dto.bIndent}">-</c:forEach>
${dto.bTitle}</td>
<td>${dto.bDate}</td>
<td>${dto.bHit}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<form action="list.do" method="post" name="search">
<select name="keyField">
<option value="bTitle">글 제목</option>
<option value="bContent">글 내용</option>
<option value="bName">작성자</option>
</select>
<input type="text" name="keyWord">
<input type="button" value="검색" onclick="searchCheck(form)">
</form>
</td>
</tr>
<tr>
<td colspan="5"> 글작성 </td>
</tr>
</table>
<div class="toolbar-bottom">
<div class="toolbar mt-lg">
<div class="sorter">
<ul class="pagination">
<li>맨앞으로</li>
<li>앞으로</li>
<c:forEach var="i" begin="${paging.startPageNo}" end="${paging.endPageNo}" step="1">
<c:choose>
<c:when test="${i eq paging.pageNo}">
<li class="active">${i}</li>
</c:when>
<c:otherwise>
<li>${i}</li>
</c:otherwise>
</c:choose>
</c:forEach>
<li>뒤로</li>
<li>맨뒤로</li>
</ul>
</div>
</div>
</div>
You never seem to be passing the keyword or keyfield when you call pageMove(). You might as well look up their values inside the function instead of having them as parameters:
function PageMove(page){
var keyWord = document.getElementById("keyWord").value;
var keyField = document.getElementById("keyField").value;
location.href = "list.do?page=" + page + "&keyWord=" + keyWord + "&keyField=" + keyField;
}

How to enter values to group of textfields in a table using selenium

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");
}

unable to add or update records in servlet /jsp

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.

Inserting into database table with auto generated numbers through javascript

I Have created a from for my website. My form posts into two separate tables, namely: 'members' and 'users'
My form seems to input into my database fine, apart from under Membership Type 'membershipnumber'. My membership number is auto generated using javascript.
Please can someone help me and point me in the right direction as to why this isnt working and how i can get it to work.
<form action="receptionist.php" method="post">
<td align="center" style="background-color:lightblue;"><b>-Personal Information-<br></td> </b>
</p>
<tr>
<td align="left"> Last name: <input type="text" name="lastname"></td>
<td></td>
<td align="left"> Medical Information: <input type="text" name="medicalinformation"></td>
</tr>
<tr>
<td align="left"> First name: <input type="text" name="firstname"></td>
<td></td>
<td align="left">Membership Type: <select name="membershiptype" id="membershiptype">
<option value="">Select One</option>
<option value = "Gold">Gold</option>
<option value = "Silver">Silver</option>
</select></td>
</tr>
<tr>
<td align="left"> Date Of Birth: <input type="text" name="dateofbirth"><br></td>
<td></td>
<td align="left"> Gender:<input type ="radio" name="gender" value="Male"> Male
<input type ="radio" name="gender" value="Female"> Female<br>
</tr>
<td align="left">Membership Referral: <input type="text" name="membershipreferral"><br></td>
<td></td>
<td align="left"> Trainer Required: <input type ="radio" name="trainerrequired" value="Yes"> Yes
<input type ="radio" name="trainerrequired" value="NO"> No<br></td>
</tr>
<td align="left"> Membership Number:</td>
<td align="left" id="xyz"><input type="text" name="membershipnumber" disabled></td>
<script>
var num = Math.floor(Math.random() * 9000) + 1000;
document.getElementById("xyz").innerHTML=num;
</Script>
<td align="left"> Contract: <input type ="radio" required> Agree <br></td>
</tr>
<td align="left">
Please select contract duration
<select name="contract" id="contract">
<option value="">Select One</option>
<option value = "1 month">1 month</option>
<option value = "2 month">2 month</option>
<option value = "3 month">3 month</option>
<option value = "4 month">4 month</option>
<option value = "5 month">5 month</option>
<option value = "6 month">6 month</option>
<option value = "7 month">7 month</option>
<option value = "8 month">8 month</option>
<option value = "9 month">9 month</option>
<option value = "10 month">10 month</option>
<option value = "11 month">11 month</option>
<option value = "12 month">12 month</option>
</select><br></td>
</tr>
</tr>
</br>
<td align="center" style="background-color:lightblue;"><b>-Address-<br></td> </b>
</p>
</tr>
<td align="left">House Number/Street: <input type="text" name="housenumberstreet"><br></td>
<td></td>
<td align="left"> City: <input type="text" name="city"><br></td>
</tr>
<td align="left"> County: <input type="text" name="county"><br></td>
<td></td>
<td align="left"> Postcode: <input type="text" name="postcode"><br></td>
</tr>
<td align="left"> Tel: <input type="text" name="tel"><br></td>
<td></td>
<td align="left"> E Mail: <input type="text" name="email"><br></td>
</tr>
<td align="center" style="background-color:lightblue;"><b>-Billing-<br></td> </b>
</tr>
</p>
<td align="left"> Bank Branch: <input type="text" name="bankbranch"><br></td>
</tr>
<td align="left"> Card Holder Name: <input type="text" name="cardholdername"><br> </td>
</tr>
<td align="left">Card Number: <input type="text" name="cardnumber"><br></td>
<td align="left">Sort Code: <input type="text" name="sortcode"></td>
</tr>
<td align="left"><font size="3.2" color="red"><b>Valid From</font><br> </b>
Valid Month <select name="validmonth" id="validmonth" required>
<option value="">Select One</option>
<option value = "January">January</option>
<option value = "February">February</option>
<option value = "March">March</option>
<option value = "April">April</option>
<option value = "May">May</option>
<option value = "June">June</option>
<option value = "July">July</option>
<option value = "August">August</option>
<option value = "September">September</option>
<option value = "October">October</option>
<option value = "November">November</option>
<option value = "December">December</option>
</select>
Valid Year <select name="validyear" id="validyear" required>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
</select><br></td>
</tr>
<td align="left"> <font size="3.2" color="red"><b>Expiry</font><br> </b>
Expiry Month <select name="expirymonth" id="expirymonth" required>
<option value="">Select One</option>
<option value = "January">January</option>
<option value = "February">February</option>
<option value = "March">March</option>
<option value = "April">April</option>
<option value = "May">May</option>
<option value = "June">June</option>
<option value = "July">July</option>
<option value = "August">August</option>
<option value = "September">September</option>
<option value = "October">October</option>
<option value = "November">November</option>
<option value = "December">December</option>
</select>
Expiry Year <select name="expiryyear" id="expiryyear" required>
<option value="2014">2014</option>
<option value="2013">2015</option>
<option value="2012">2016</option>
<option value="2011">2017</option>
<option value="2010">2018</option>
<option value="2009">2019</option>
<option value="2008">2020</option>
<option value="2007">2021</option>
</select><br></td>
</tr>
<td align="left"> Security Code: <input type="text" name="securitycode"><br></td>
</tr>
<td align="left"><p style="background-color:lightblue;">
Please double check entered information and tick box to proceed
<input type="radio" required></br></td>
</tr>
<td align="left"> <input type="submit"></td>
</form>
You just replace
<td align="left" id="xyz"><input type="text" name="membershipnumber" disabled></td>
this line to
<td align="left"><input type="text" id="xyz" name="membershipnumber" disabled></td>
this line.
everything your code snippet is ok.
Hope that it helps you.
I believe the following post might answer your question: reading innerHTML of HTML form with VALUE attribute (& its value) of INPUT tags.
In short: Setting the value attribute explicitly using "document.getElementById("xyz").setAttribute("value", num);" should work. See the above-mentioned post for further details with regards to why innerHTML wouldn't work. Hope it helps.

Categories