Generating Quiz questions with options using Map in JSP - java

I have a form for sending "question" to server.
A "question" has fields: title, description and options (from 1 to 4 represented by Map<String, Boolean>). String - for option, Boolean - true/false.
So, how can I create a Map of every option and send it to server?
<div id="TYPE_TEST" class="opclass">
<p><label for="option1"/>Option 1:
<input id="option1" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option2"/>Option 2:
<input id="option2" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option3"/>Option 3:
<input id="option3" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option4"/>Option 4:
<input id="option4" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
</div>
Thank you,
Pavel

You need to represent like this
represent questions and options like this
//Map<question,options>
//where option also map
Map<String,Map<String,Boolean>> questions=new HashMap<String,Map<String,Boolean>>();
Map<String,Boolean> question1_options=new HashMap<String,Boolean>();
question1_options.put("option1",true);
question1_options.put("option2",false);
question1_options.put("option3",false);
question1_options.put("option4",false);
Map<String,Boolean> question2_options=new HashMap<String,Boolean>();
question2_options.put("option1",true);
question2_options.put("option2",false);
question2_options.put("option3",false);
question2_options.put("option4",false);
questions.put("question_1",question1_options);
questions.put("question_2",question2_options);
System.out.println(questions);

Following MaDHaN's code: inside JSP, use it with ${mapName["option1"]}.
If it works, I think MaDHaN deserves the accept.

You can do it in several ways. Some of them as follows:
<% Map<String,Boolean> map =new HashMap<String,Boolean>();
map.put("key1",true);
map.put("key2",false);
request.setAttribute("map", map);
%>
<%for(Map.Entry<String,Boolean> mapEntry: map.entrySet()){%>
<%= mapEntry.getKey() %>
<%= mapEntry.getValue() %>
<%}%>
Or
<c:forEach items="${map}" var="entry">
<c:out value ="${entry.key}" />
<c:out value ="${entry.value}"/>
</c:forEach>

Related

Selected Tag dissapears after form validation and submition in JSP

I'm a beginner in Spring and I'm trying to fix some small problem in my application. I have a form to edit the user profile. In this form, I have one dropdown list where admin user can select user role for the user that is being edited. When I'm going to edit user page then all data in form are populated from the database. Let's say I have two inputs in the form: input with lastName and select with user role. The select element contains all the roles that exist in the database but the selected role is matched to the role that the user actually have assigned.
This is part of my form - input lastName
<spring:bind path="lastName">
<div class="form-group">
<label for="lastName">Last name</label>
<form:input path="lastName" type="text" class="form-control ${status.error ? 'border border-danger' : ''}" id="lastName" />
<form:errors path="lastName" cssStyle="color: #ff0000;"/>
</div>
</spring:bind>
and this is a select element:
<form:select path="roles" id="role" multiple="false">
<form:options items="${allRoles}" itemValue="id" itemLabel="role"/>
</form:select>
Now, when I want to edit lastName eg. leave the blank field, then spring validates this field and throws an error. The problem is that role that was previously selected in select element is now unselected.
This is page HTML before validation:
input:
<div class="form-group ">
<label for="email" class="active">Email</label>
<input id="email" name="email" type="email" class="form-control" value="ola#test.com">
</div>
select:
<select id="role" name="roles"">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4" selected="selected">LECTURER</option>
</select>
and once lastName field is cleared and form submited:
input:
<div class="form-group">
<label for="lastName" class="">Last name</label>
<input id="lastName" name="lastName" type="text" class="form-control border border-danger" value="">
<span id="lastName.errors" style="color: #ff0000;">This field is required.</span>
</div>
select:
<div class="form-group">
<label for="role">Role Id</label>
<select id="role" name="roles">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4">LECTURER</option>
</select>
</div>
As you can see selected= "selected" attribute has disappeared from option 4. How to prevent this? Btw. I'm aware of this Spring MVC selected value in form:selected after form validation error
but it seems that this isn't work in my case.
I found some tip in archived page here
Now selected attribute is not removed from the option tag. I'm not sure if this is good way to solve this, but it works for me..
Modified select:
<form:form method="post" modelAttribute="editForm">
<c:forEach items="${editForm.roles}" var="role">
<c:set var="userRoleId" value="${role.id}" scope="request"/>
</c:forEach>
<form:select path="roles" id="role" multiple="false">
<c:forEach var="tempRole" items="${allRoles}">
<option value="${tempRole.id}" <c:if test="${tempRole.id == userRoleId}">selected="selected"</c:if> value="${tempRole.id}">${tempRole.role}</option>
</c:forEach>
</form:select>
</form:form>

Change button text on submit with velocity engine / java

So now I got a admin panel for my website I decided to try some small things but I am not getting there yet.
So whenever I disable or enable a user I get a nice good/bad notification saying what happened but ofcourse the button will stay the same. Is their a way to change this according to the function?
#parse("/velocity/layout.vm")
##mainLayout()
<table id="adminPanel">
<tr>
<th>Email</th>
<th>Name</th>
<th>Reset Password</th>
<th>IsActive</th>
<th>Delete</th>
</tr>
#if($resetpassword)
<p class="good notification">$msg.get("RESET_PASSWORD")</p>
#elseif($blockuser)
<p class="bad notification">$msg.get("BLOCK_USER")</p>
#elseif($unblockuser)
<p class="good notification">$msg.get("UNBLOCK_USER")</p>
#elseif($deleteuser)
<p class="bad notification">$msg.get("DELETE_USER")</p>
#end
#foreach($user in $users)
#if($user.getIsadmin())
<div class="col">
<tr>
<th>$user.getEmail()</th>
<th>$user.getFirstname() $user.getSurname()</th>
<th><form method="post">
<input type="hidden" name="post" value="reset">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Reset Password" disabled>
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="block">
<input type="hidden" name="email" value="$$user.getEmail()">
<input type="submit" value="IsActive" disabled>
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="delete">
<input type="hidden" name="email" value="$user.getEmail()">
<input id="button" type="submit" value="Delete" disabled>
</form></th>
</tr>
</div>
#else
<div class="col">
<tr>
<th>$user.getEmail()</th>
<th>$user.getFirstname() $user.getSurname()</th>
<th><form method="post">
<input type="hidden" name="post" value="reset">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Reset Password">
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="block">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Disable">
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="delete">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Delete">
</form></th>
</tr>
</div>
#end
#end
</table>
#end
It is very unclear in your question about what buttons you want the text to be changed. Since I see only two buttons in your code, I can recommend you to do something like this (assuming that the User object has enough details to know whether a user is blocked or not).
<input type="button" #if ($user.isBlocked()) value="Unblock" #else value="Block" #end>

Elements change after logging into account in Selenium

I am facing difficulty finding elements using Xpath in selenium. I have a payment page and a field to enter the credit card number. When I simply write a script to enter the credit card number using id it is working. The problem comes when I log into a wallet before entering my CC details. After logging into the wallet, there are two id's by the same name. The script works when I use absolute xpath but that changes when I give the script to someone else.
Html before before into wallet
<form autocomplete="off" name="creditcard-form" method="post" action="submitTransaction?MID=oIVSOk48659529909784&ORDER_ID=PARCEL675442&route=" id="card" class="cc-form validated">
<input type="hidden" name="txnMode" value="CC" />
<input type="hidden" name="txn_Mode" value="CC" />
<input type="hidden" name="channelId" value="WEB" />
<input type="hidden" name="AUTH_MODE" value="3D" />
<input type="hidden" name="CARD_TYPE" id="cardType" value="" />
<input type="hidden" name="walletAmount" id="walletAmountCC" value="0" />
<ul class="grid">
<li class="mb20 card-wrapper">
<label class="mb10" for="cardNumber">ENTER CREDIT CARD NUMBER</label>
<p class="cd">
<input autocomplete="off" type="text" name="" class="ccCardNumber text-input large-input c cardInput type-tel" id="cn" size="16" maxlength="19" style="width: 278px" data-type="cc" value="">
<input type="hidden" name="cardNumber" value="" class="required">
</p>
After logging into Wallet:
<input type="hidden" name="txnMode" value="CC" />
<input type="hidden" name="txn_Mode" value="CC" />
<input type="hidden" name="channelId" value="WEB" />
<input type="hidden" name="AUTH_MODE" value="3D" />
<input type="hidden" name="CARD_TYPE" id="cardType" value="" />
<input type="hidden" name="walletAmount" id="walletAmountCC" value="0" />
<input type="hidden" name="addMoney" value="1" />
<ul class="grid">
<li class="mb20 card-wrapper">
<label class="mb10" for="cardNumber">ENTER CREDIT CARD NUMBER</label>
<p class="cd">
<input autocomplete="off" type="text" name="" class="ccCardNumber text-input large-input c cardInput type-tel" id="cn" size="16" maxlength="19" style="width: 278px" data-type="cc" value="">
<input type="hidden" name="cardNumber" value="" class="required">
</p>
Please help as I am new to programming and can't figure out how to handle this

how can we implement a radio button instead of a drop down menu?

<form action="Operation">
First number::<input type="text" name="firstno"></input></br>
Second number::<input type="text" name="Secondno"></input></br>
<select name="operation">
<option>Add</option>
<option>Substract</option>
<option>Multiply</option>
<option>Divide</option>
</select> </br>
<input type="submit" value="submit">
</form>
Here instead of a drop down menu I want to use a radio button. How can I implement this? Also I want to perform an operation in servlet page.
for radio buttons try this way
<form action="Operation">
First number::<input type="text" name="firstno"></input></br>
Second number::<input type="text" name="Secondno"></input></br>
<input type="radio" name="operation" value="add">add<br>
<input type="radio" name="operation" value="subtract">Substract<br>
<input type="radio" name="operation" value="Multiply">Multiply<br>
<input type="radio" name="operation" value="Divide">Divide<br>
<input type="submit" value="submit">
</form>
on the servlet try
String radio=request.getParameter("operation");
change your input type="radio"

getText() in WebDriver using Java

The following is the HTML code:
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
I tried to get the text "This field must not be empty" as follows by using WebDriver with Java:
driver.findElement(By.xpath("//div[#id='TITLE']/div")).getText();
driver.findElement(By.xpath("//div[#class='error']")).getText();
But, no text was retrieved. How can I do it? What's the wrong with my code?
Yes, there is a form. HTMl code including form as belows:
<form action="http://community.sandbox.no/content/save.do;jsessionid=F2BF733599D8D9F812B89ACBC20D37C5" method="post">
<div id="widget_polarisCommunityInput_113_leftcolumn">
<input type="hidden" value="article" name="articleType">
<input type="hidden" value="published" name="state">
<input type="hidden" value=" " name="successUrl">
<input type="hidden" value="" name="articleId">
<input type="hidden" value="http://sandbox.no/community/article/" name="redirect_url">
<input type="hidden" value="113" name="widget_id">
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
</form>
You can try as:
driver.findElement(By.xpath("//form/div[#id='widget_polarisCommunityInput_113_leftcolumn']/div[#id='TITLE']/div")).getText();

Categories