Handling ui-dialog in Selenium Webdriver using Java - java

In our application, once after providing the details of creating an order clicks Save, a dialog gets open and wants user’s re-confirmation whether the order needs to place or not.
Now the difficulty comes in identifying and submitting confirmation on this newly opened dialog – where unable to switch into this new dialog.
Set<String> window = wd.getWindowHandles();
Iterator<String> it = window.iterator();
String parentwindow = it.next();
String childwindow = it.next();
wd.switchTo().window(childwindow);
ccg.clickconfirmSalesEntitySave();
wd.switchTo().window(parentwindow);
Not for certain, whether the window method works for dialog too..
Can someone help me on this concern, please.
Below is the HTML of this dialog.
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" style="display: block; z-index: 1002; outline: 0px none; height: auto; width: 300px; top: 37px; left: 514px;" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-dialog-confirm">
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix" unselectable="on" style="-moz-user-select: none;">
<span id="ui-dialog-title-dialog-confirm" class="ui-dialog-title" unselectable="on" style="-moz-user-select: none;">Create an order</span>
<a class="ui-dialog-titlebar-close ui-corner-all" href="#" role="button" unselectable="on" style="-moz-user-select: none;">
<span class="ui-icon ui-icon-closethick" unselectable="on" style="-moz-user-select: none;">close</span>
</a>
</div>
<div id="dialog-confirm" class="popup ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; height: 155px;">
<br/>
<table>
<tbody>
<tr>
<td class="dialogContent">Please confirm your request</td>
</tr>
</tbody>
</table>
<br/>
<br/>
<div style="border:0.1px solid #DCDCDC;"/>
<br/>
<form id="xyzGroupVO" method="post" action="/xyz/xyz/Maintenance?execution=e2s2">
<div align="center">
<input id="confirmOrderSave" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" value="Yes" name="_eventId_xyzGroupSave"/>
<input id="confirmOrderDontSave" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" value="No" name="_eventId_xyzGroupDontSave"/>
</div>
</form>
</div>
</div>

In your code,you can get child window through first it.next(). Try following:
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
Iterator<String> windowIterator = browser.getWindowHandles();
while(windowIterator.hasNext())
{
String windowHandle = windowIterator.next();
browser.switchTo().window(windowHandle);
}

Related

how to resolve this itext v5 pdf format error?

Using itext and xmlworker version 5.5.13.2
I have a need to convert static HTML with basic CSS into a pdf. But the resulting PDF has not right format/ spacing/ position. Can someone help me out by pointing the mistake i have done or the something i have missed in the code part.
HTML
<!DOCTYPE html>
<html>
​
<head>
</head>
​
<body>
<div style="margin:5px; width:100%">
<div style="border:1px solid black;padding: 25px;">
<div class="row1">
<div class="floatR" style="min-width: 33%;width: 33%;min-height: 1px; float: right;">
<h3>
Tax Invoice/Bill Of Supply/ Cash Memo
</h3>
(Original for Recipient)
</div>
</div>
<div class="row1 marginT50p">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<h4>
Sold By:
ArtisanIndia Shoppping Private Limited,
</h4>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px ;float: left;">
<h4>
Sold By:
ArtisanIndia Shoppping Private Limited,
</h4>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textUnderline">Comapny GSTIN:</label>JKDHFBGKJDJHJ
<br />
<label class="textUnderline">Comapny PAN:</label>SDJKHFGB
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textUnderline">Biller GSTIN:</label>JKDHFBGKJDJHJ
<br />
<label class="textUnderline">Biller PAN:</label>SDJKHFGB
</div>
</div>
​
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Order ID:</label>235235
<br />
<label class="textBold">Order Date:</label>12/12/1912
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Shipping Address:</label>
<br />
<label class="textBold"> </label>
<br />
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
​
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
​
<label class="textBold">Invoice Date:</label>12/12/1912
<br />
<label class="textBold">Order ID:</label>AI/2012/907656767
​
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Place Of Supply:</label> Maharastra
<br />
<label class="textBold">Place Of Delivery:</label>Maharastra
​
</div>
</div>
​
<div class="row1 marginT25">
<table style="width:100%">
<tr>
<th>Sr. No.</th>
<th>Product</th>
<th>Description</th>
<th>HSN</th>
<th>GST %</th>
<th>QTY</th>
<th>Gross Amount</th>
<th>Discount</th>
<th>Taxable value</th>
<th>CGST</th>
<th>SGST</th>
<th>IGST</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
​
<tr>
<td colspan="5">Total</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
​
</div>
<div class="row1">
<div style="min-width: 25%; width: 25%; min-height: 1px;float: left;">
<label class="textUnderline textBold">
Seller Registered Address:
ArtisanIndia Shoppping Private Limited,
</label>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
​
<div class="row1 marginT50p">
<div style="min-width: 25%; width: 25%; min-height: 1px;float: left;">
E. & O. E.
​
</div>
<div class=" floatR" style="min-width: 25%; width: 25%; min-height: 1px; float: right;">
<div class="signbox">
Sign/Stamp
</div>
<span> Ordered Through
</span>
<span class="textUnderline">
​
ArtisanIndia Shoppping Pvt Limited
​
<br />
Authorized Signature
</span>
</div>
</div>
</div>
</div>
​
</body>
​
</html>
CSS
* {
font-family: sans-serif;
font-size:8px
}
.width33 {
min-width: 33%;
width: 33%;
min-height: 1px;
border:1px solid red
}
.width25 {
min-width: 25%;
width: 25%;
min-height: 1px;
border: 1px solid green;
}
.row1:after {
content: "";
display: table;
clear: both;
}
.floatL {
float: left;
}
.floatR {
float: right;
}
.marginT25 {
margin-top: 25px;
}
.marginT50p {
margin-top: 50px
}
.textUnderline {
text-decoration: underline;
}
.textBold {
font-weight: bold;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
font-size: 10px;
}
th,
td {
height: 40px;
text-align: center;
}
.signbox {
vertical-align: middle;
text-align: center;
height: 50px;
border: 1px solid black;
/* margin: auto; */
}
Java
UploadUtil s3Upload = new UploadUtil();
PdfWriter pdfWriter = null;
ByteArrayOutputStream gg = new ByteArrayOutputStream();
try{
Document document = new Document();
PdfWriter.getInstance(document, gg);
document.open();
XMLWorkerHelper xmlWorkerHelper = XMLWorkerHelper.getInstance();
xmlWorkerHelper.parseXHtml(pdfWriter, document, new FileInputStream("C:\\Users\\jj\\Desktop\\asdf\\tt.html"), new FileInputStream("C:\\Users\\jj\\Desktop\\asdf\\ttt.css"));
document.close();
FileOutputStream fos = new FileOutputStream("C:\\Users\\jj\\Desktop\\asdf\\test.pdf");
fos.write(gg.toByteArray());
fos.close();
}
catch(Exception e){
System.out.println("asdf");
}
the resulting pdf generated looks something like this.
instead of
Can someone out here point out the reason why this is happening or what I have missed or done wrong?
iText 5.5.13.2 is the old version of iText product and it's not supported anymore. Most likely there are some not supported functionality in the html or css. I can guess that float properties are not supported. Probably there are issues with percentage width. You can try to play around with your html and css files to try to make pdf look acceptable, but the easiest way is to use the newest version of iText product which is iText 7

Object received from springboot backend and resent back has fields containing null values

I use an angular+springboot web application in which an object ( for this example- clinic object ) fetched from springboot in angular is not modified, and then is sent back to springboot, and some of its prperties's values for no apparent reason just become null, and others ( a boolean property get value of false, and integer properties get value of 0 ) get default values.
This is a table in which clinic objects are desplayed, and from which a modal to update a clinic is opened.
<table class="table">
<thead>
<tr>
<th scope="col" style="text-align:right;"></th>
<th scope="col" style="text-align:right;">State</th>
<th scope="col" style="text-align:right;">Foundation Year</th>
<th scope="col" style="text-align:right;">Supervisor</th>
<th scope="col" style="text-align:right;">Clinic Name</th>
</tr>
</thead>
<tbody>
<tr [ngClass]="{'not-active-clinic':!clinic.active}" *ngFor="let clinic of clinics">
<td style="text-align:right;">
<button class="btn-primary mr-1" (click)="openEditModal(editModal,clinic)"><i
class="fa fa-edit"></i></button>
<!--Edit Modal -->
<ng-template #editModal let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Clinic Edit</h4>
<button style="margin-left: 0;" type="button" class="close" aria-label="Close"
(click)="modal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form dir="rtl">
<div class="row">
<div class="col-3">
<label>
Clinic Name: </label>
</div>
<div class="col-9">
<input style="width: 100%;" type="text" name="clinicName"
[(ngModel)]="edittedClinic.clinicName">
</div>
</div>
<div class="row">
<div class="col-3">
<label>
Supervisor</label>
</div>
<div class="col-9">
<select (change)="onChange()" style="width: 100%;"
name="clinicalSupervisorId"
[(ngModel)]="edittedClinic.clinicalSupervisorId">
<option *ngFor="let s of supervisors" [value]="s.id">
{{s.firstName+' '+s.lastName}}
</option>
</select>
</div>
</div>
<div class="row">
<div class="col-3">
<label>
description:</label>
</div>
<div class="col-9">
<textarea style="width: 100%;height:350px"
[(ngModel)]="edittedClinic.description" name="description">
</textarea>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark"
(click)="modal.close('Save click');onEdit(clinic)">Edit</button>
</div>
</ng-template>
<button class="btn btn-sm btn-danger btn-delete-account"
(click)="openDeleteModal(deleteModal)">
<span class="spinner-border spinner-border-sm"><i
class="fa fa-trash-alt"></i></span>
</button>
<!--Delete Modal -->
</td>
<td style="text-align:right;">
<button (click)="onChangeClinicState(clinic)">
{{clinic.active?"Add To Archive":"Restore"}}</button>
</td>
<td style="text-align:right;">{{clinic.yearFounded}}</td>
<td style="text-align:right;">{{clinic.clinicalSupervisorId}}</td>
<td style="text-align:right;">{{clinic.clinicName}}</td>
</tr>
</tbody>
</table>
This the method in angular to fetch the edited object to send it to service in which there is the http call to send the clinic object to springboot:
onEdit(clinicToEdit:Clinic)
{
this.dashboardService.updateClinicDetails(this.edittedClinic).subscribe(
data=>{
clinicToEdit=Object.create(this.edittedClinic);
},
err=>{
}
)
}
This is the method in the service to send the http put call
updateClinicDetails(clinic:Clinic)
{
return this.http.put(BASE_URL +`/api/v1/clinic/${clinic.clinicName}`,clinic);
}
This is the API method in springboot to get the clinic object sent from angular.
#PutMapping(path = "{clinicName}")
public int updateClinicById(#PathVariable("clinicName") String clinicName, #RequestBody Clinic clinicToUpdate)
{
return clinicService.updateClinic(clinicName, clinicToUpdate);
}
This is the constructor of clinic object invoked in order to fetch the JSON sent to springboot from angular.
public Clinic(#JsonProperty("clinicName") String clinicName,
#JsonProperty("clinicalSupervisorId") int clinicalSupervisorId,
#JsonProperty("description") String description, #JsonProperty("yearFounded") int yearFounded,
#JsonProperty("active") boolean active)
{
this.clinicName = clinicName;
this.clinicalSupervisorId = clinicalSupervisorId;
this.yearFounded = yearFounded;
this.description = description;
this.active = active;
}
Now, the thing is when modified, those mentioned properties don't contain null values, or their default values, What is the explanation to this, and what's the best way to solve it?

Spring Application: Problem Parsing Form Object To Controller Using #Model attribute. Error org.attoparser.ParseException:Field

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" media="all" th:href="#{/css/bootstrap.min.css}">
<title>Home</title>
</head>
<body class="p-3 mb-2 bg-light text-black">
<div class="container">
<div id="logoutDiv">
<h1 th:text="${'Welcome ' + name}">Name</h1>
<form action="#" th:action="#{/logout}"method="POST">
<button type="submit" class="btn btn-secondary float-right">Logout</button>
</form>
</div>
<div id="contentDiv" style="clear: right;">
<nav style="clear: right;">
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-item nav-link active" id="nav-files-tab" data-toggle="tab" href="#nav-files" role="tab" aria-controls="nav-files" aria-selected="true">Files</a>
<a class="nav-item nav-link" id="nav-notes-tab" data-toggle="tab" href="#nav-notes" role="tab" aria-controls="nav-notes" aria-selected="false">Notes</a>
<a class="nav-item nav-link" id="nav-credentials-tab" data-toggle="tab" href="#nav-credentials" role="tab" aria-controls="nav-credentials" aria-selected="false">Credentials</a>
</div>
</nav>Upload<
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-files" role="tabpanel" aria-labelledby="nav-files-tab">
<p th:text="${message}" th:if="${message ne null}" class="alert alert-primary"></p>
<form action="#" enctype="multipart/form-data" th:action="#{'/file/uploadFile'}" th:method="POST" >
<div class="container">
<div class="row" style="margin: 1em;">
<div class="col-sm-2">
<label for="fileUpload">Upload a New File:</label>
</div>
<div class="col-sm-6">
<input type="file" class="form-control-file" id="fileUpload" name="fileUpload">
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-dark">/button>
</div>
</div>
</div>
</form>
<div class="table-responsive">
<table class="table table-striped" id="fileTable">
<thead>
<tr>
<th style="width: 20%" scope="col"></th>
<th style="width: 80%" scope="col">File Name</th>
</tr>
</thead>
<tbody>
<tr th:each="file : ${files}">
<td>
<a target="_blank" class="btn btn-success" th:href="#{/file/{filename}(fileName = ${file.filename})}">View</a>
<a class="btn btn-danger" th:href="#{/file/delete{filename}(filename = ${file.filename})}" >Delete</a>
</td>
<th scope="row" th:text="${file.filename}" ></th>
</tr>
</tbody>
</table>
</div>
</div>
<div class="tab-pane fade" id="nav-notes" role="tabpanel" aria-labelledby="nav-notes-tab">
<button style="margin: 0.25em;" type="button" class="btn btn-info float-right" onclick="showNoteModal()">
+ Add a New Note
</button>
<div class="table-responsive">
<table class="table table-striped" id="userTable">
<thead>
<tr>
<th style="width: 20%" scope="col"></th>
<th style="width: 20%" scope="col">Title</th>
<th style="width: 60%" scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr th:each="note : ${notes}">
<td>
<button id="edit-note" type="button" class="btn btn-success"
th:attr="data-id=${note.getNoteId},
data-title=${note.getNoteTitle},
data-description=${note.getNoteDescription}"
onclick="showNoteModal (this.getAttribute('data-id'),this.getAttribute('data-title'),this.getAttribute('data-description'))">Edit/View</button>
<a id="delete-note" class="btn btn-danger" th:href="#{/note/delete/{noteId}(noteId = ${note.getNoteId()})}">Delete</a>
</td>
<th id="notetitle" scope="row" th:text="${note.getNoteTitle()}">Example Title</th>
<td id="notedescription" th:text="${note.getNoteDescription()}">Example Description </td>
</tr>
</tbody>
</table>
</div>
<div class="modal fade" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="noteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="noteModalLabel">Note</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
-Here is the error that I get-
<form action="#" method="POST" th:action="#{/note/add}" th:object="${Notes}">
<input type="hidden" name="noteId" id="note-id">
<div class="form-group">
<label for="note-title" class="col-form-label">Title</label>
<input type="text" name= "noteTitle" class="form-control" id="note-title" maxlength="20" required th:field="*{noteTitle}">
</div>
<div class="form-group">
<label for="note-description" class="col-form-label">Description</label>
<textarea class="form-control" name="noteDescription" id="note-description" rows="5" maxlength="1000" required th:field="*{noteDescription}"></textarea>
</div>
<button id="noteSubmit" type="submit" class="d-none"></button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="$('#noteSubmit').click();">Save changes</button>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" id="nav-credentials" role="tabpanel" aria-labelledby="nav-credentials-tab">
<button style="margin: 0.25em;" type="button" class="btn btn-info float-right" onclick="showCredentialModal()">
+ Add a New Credential
</button>
<div class="table-responsive">
<table class="table table-striped" th:object="${credentials}" id="credentialTable">
<thead>
<tr>
<th style="width: 20%" scope="col"></th>
<th style="width: 35%" scope="col">URL</th>
<th style="width: 20%" scope="col">Username</th>
<th style="width: 25%" scope="col">Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn btn-success">Edit</button>
<a class="btn btn-danger">Delete</a>
</td>
<th scope="row">Example Credential URL</th>
<td>Example Credential Username</td>
<td>Example Credential Password</td>
</tr>
</tbody>
</table>
</div>
<div class="modal fade" id="credentialModal" tabindex="-1" role="dialog" aria-labelledby="credentialModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="credentialModalLabel">Credential</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="#" method="POST">
<input type="hidden" name="credentialId" id="credential-id">
<div class="form-group">
<label for="note-title" class="col-form-label">URL</label>
<input type="text" name= "url" class="form-control" id="credential-url" maxlength="100" required>
</div>
<div class="form-group">
<label for="note-title" class="col-form-label">Username</label>
<input type="text" name= "username" class="form-control" id="credential-username" maxlength="30" required>
</div>
<div class="form-group">
<label for="note-title" class="col-form-label">Password</label>
<input type="text" name= "password" class="form-control" id="credential-password" maxlength="30" required>
</div>
<button id="credentialSubmit" type="submit" class="d-none"></button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="$('#credentialSubmit').click();">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script th:src="#{/js/jquery-slim.min.js}"></script>
<script th:src="#{/js/popper.min.js}"></script>
<script th:src="#{/js/bootstrap.min.js}"></script>
<!--For opening the note modal-->
<script type="text/javascript">
// For opening the note modal
function showNoteModal(noteId, noteTitle, noteDescription) {
$('#note-id').val(noteId ? noteId : '');
$('#note-title').val(noteTitle ? noteTitle : '');
$('#note-description').val(noteDescription ? noteDescription : '');
$('#noteModal').modal('show');
}
// For opening the credentials modal
function showCredentialModal(credentialId, url, username, password) {
$('#credential-id').val(credentialId ? credentialId : '');
$('#credential-url').val(url ? url : '');
$('#credential-username').val(username ? username : '');
$('#credential-password').val(password ? password : '');
$('#credentialModal').modal('show');
}
</script>
</body>
</html>
-I get an error when trying to parse the Note Form into my Note Controller. I try to parse my model object Notes in the form in my HTML home form into the Controller but it can not except the fields from the form. My first error is this:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "home" - line 109, col 148), Line 109 is the fields from the form
I also get a second error: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Notes' available as request attribute-
--- Note Controller ---
#Controller
#RequestMapping("note")
public class NoteController {
private NoteService noteServices;
private UserService userService;
public NoteController(NoteService noteServices, UserService userService) {
this.noteServices = noteServices;
this.userService = userService;
}
// Add an new note
#PostMapping("add")
public String addNote(#ModelAttribute(value="Notes")Notes Notes, Authentication authentication, Model model) throws IOException {
String userName = authentication.getName();
User user = userService.getUser(userName);
Integer userid = user.getId();
if(Notes.getNoteId() == null){
noteServices.addNote(Notes,userid);
}else{
noteServices.editNote(Notes);
}
return "result";
}
#GetMapping("/delete/{noteId:.+}")
public String deleteNote(#PathVariable Integer noteId, Authentication authentication, RedirectAttributes redirectAttributes){
noteServices.deleteNote(noteId);
redirectAttributes.addFlashAttribute("deleteNoteSuccess","Note deleted successfully.");
return "redirect:/result";
}
private Integer getUserId(Authentication authentication) {
String userName = authentication.getName();
User user = userService.getUser(userName);
return user.getId();
}
}
---
--- POJO Model ---
public class Notes {
private Integer noteId;
private String noteTitle;
private String noteDescription;
private Integer userId;
public Notes(String noteTitle, String noteDescription, Integer userId) {
this.noteTitle = noteTitle;
this.noteDescription = noteDescription;
this.userId = userId;
}
public Integer getNoteId() {
return noteId;
}
public void setNoteId(Integer noteId) {
this.noteId = noteId;
}
public String getNoteTitle() {
return noteTitle;
}
public void setNoteTitle(String noteTitle) {
this.noteTitle = noteTitle;
}
public String getNoteDescription() {
return noteDescription;
}
public void setNoteDescription(String noteDescription) {
this.noteDescription = noteDescription;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
}

Spring MVC: Multiple Row Form Submit

I had a requirement where using Spring MVC we had to take inputs multiple rows of data from user. The form had many rows which user can edit and submit My code look like this.When I tried to display the area list it is null What will be the reason
My view is
<form data-th-action="#{/qprmonitorlottery}" method="POST" >
<section>
<div class="panel panel-default" style="margin-top: 15px;">
<!-- Default panel contents -->
<div class="panel-heading">エリア間補完の有無</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="13%" style="background: #FFFFFF; border-top: 1px solid #ffffff; border-left: 1px solid #ffffff;" > </th>
<th colspan="3" style="text-align: center">都市規模</th>
</tr>
<tr>
<th>エリア </th>
<th width="28%">大都市 </th>
<th width="29%">中都市</th>
<th width="30%">郊外 </th>
</tr>
<tr data-th-each="qprLottery,stat : ${qprLottery}">
<td data-th-text = "${qprLottery.areaName}">北海道 </td>
<td> <span style="display: inline-block; width: 80%">
<input type="number" data-th-value = "${qprLottery.rate1}" class="form-control" data-th-name="firstRate +${stat.index}" required >
<span class="help-block with-errors"></span>
</span> <span style="position: relative; top:-10px;" >%</span> </td>
<td> <span style="display: inline-block; width: 80%">
<input type="number" data-th-value = "${qprLottery.rate2}" class="form-control" data-th-name="secondRate +${stat.index}" required >
</span> <span style="position: relative; top:-10px;" >%</span> </td>
<td> <span style="display: inline-block; width: 80%">
<input type="number" data-th-value = "${qprLottery.rate3}" class="form-control" data-th-name="thirdRate +${stat.index}" required >
</span> <span style="position: relative; top:-10px;" >%</span> </td>
</tr>
</table>
</div>
</div>
</div>
</section>
<div class="row">
<div align="center" style="padding: 30 0px;">
<button type="submit" class="btn btn-primary" value="実行">実行
</button>
</div>
</div>
</form>
Controller is
#RequestMapping(value = "/qprmonitorlottery", method = RequestMethod.POST)
public String setQprMonitorLottery(#Valid #ModelAttribute() MasterCitySizeAreaForm form, BindingResult result) {
CitySizeByAreaView a = new CitySizeByAreaView();
System.out.println("Area list" + a.getRate1());
System.out.println("Area list-------------" + form.getAreaList());
if (result.hasErrors()) {
return "new-monitor-lottery";
}
return "new-monitor-lottery";
}
form classes are
public class MasterCitySizeAreaForm {
private ArrayList<CitySizeAreaForm> areaList;
public ArrayList<CitySizeAreaForm> getAreaList() {
return areaList;
}
public void setAreaList(ArrayList<CitySizeAreaForm> areaList) {
this.areaList = areaList;
}
}
form class 2
public class CitySizeAreaForm {
private int areaId;
private Integer area;
private Integer citySize;
private String areaName;
private String citySizeName;
private Float rate1;
private Float rate2;
private Float rate3;
public CitySizeAreaForm() {
}
public CitySizeAreaForm(Integer area, String areaName, Float rate1, Float rate2, Float rate3) {
this.area = area;
this.areaName = areaName;
this.rate1 = rate1;
this.rate2 = rate2;
this.rate3 = rate3;
}
}
replace:
<tr data-th-each="qprLottery,stat : ${qprLottery}">
with
<tr data-th-each="qprLottery,stat : ${qprLottery.areaList}">
This link helped me!!!Do try it!!
http://jsltech.blogspot.com/2013/12/how-add-multiple-objects-into-database.html
can use this format for displaying:-
<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
<tr>
<td>${contact.firstname}</td>
<td>${contact.lastname}</td>
<td>${contact.email}</td>
<td>${contact.phone}</td>
</tr>
</c:forEach>

JSP user Authentication in a web base application

<title>Reprint</title>
<style type="text/css">
.btn_hover:hover{
color: darkblue ;
}
</style>
<%
if (session.getAttribute("user") == null) {
response.sendRedirect("login.jsp?ms=Please Login");
}
%>
</head>
<body onload="addDate();
addDate1();
addDate3();" style="background-color: aliceblue;" >
<form action="search" method="Post" name="search">
<table align="" border="1">
<tr align="">
<td >
<h3 align="center" >Reprint Request Form <img src="sarasavi.png" /> </h3>
<%
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat(" yyyy.MM.dd");
out.print("<h4 align=\"center\">" + ft.format(dNow) + "</h4>");
%>
</td>
</tr>
<tr>
<td>
<h6 >
Enter ISBN or Scan the Barcode <i style="color: #545454" class=" icon-barcode"></i>
</h6>
<input type="text" placeholder="ISBN " name="isbn"/>
<input type="submit" value="SEARCH" class="btn_hover" style=" border-radius: 30px;" />
<!-- -->
<l style="font-size: 18px; font-weight: bold;">logged in as </l><%
ResultSet rs = (ResultSet) session.getAttribute("user");
if (rs != null && rs.first()) {
ResultSet r4 = DB.search("Select * from User where uid='" + rs.getString("User_uid") + "'");
if (r4.next()) {
%>
<%=r4.getString("uName")%>
<%
}
}
%>
Log out
</td>
</tr>
</table>
</form>
<table border="1">
<tr>
<td>
<ul class=" tabs left " >
<li><a href="#tabr1" >Stores</a></li>
<li>Publishing Department</li>
<li>Printing Department</li>
<li>Director</li>
</ul>
<!-- End Tabs -->
<!-- Stores -->
<form action="stores" method="Post" name="stores">
<div id="tabr1" class="tab-content">
<!-- HALF & HALF -->
<div class=" col_6">
<label for="text1" style=" font-size: 16px;">Title </label><br/>
<input name="title" type="text" placeholder="Title" style="width: 355px; height: 28px;" /><br/><br/>
<label for="text2" style=" font-size: 16px;">Author </label><br/>
<input name="author" type="text" placeholder="Author" style="width: 355px; height: 28px;" /><br/><br/>
<label for="text3" style=" font-size: 16px;">Price </label></span> <br/>
<input name="price" type="text" placeholder="price" style="width: 95px; height: 28px;" /><br/><br/><br/>
<!-- Radio -->
<label style=" font-size: 16px;">Move</label><br/><br/>
<input type="radio" name="Move" id="radio1" value="Fast" />
<label for="radio1" class="inline" >Fast</label><br/>
<input type="radio" name="Move" id="radio2" Value="Medium" />
<label for="radio2" class="inline">Medium</label><br/>
<input type="radio" name="Move" id="radio3" value="Slow" />
<label for="radio3" class="inline">Slow</label><br/>
<!-- Radio -->
</div>
<div class="col_6">
<label for="text4" style=" font-size: 16px;">Stock in hand </label> <br/>
<input name="stock" type="text" placeholder="" style="width: 100px; height: 28px;" /><br/><br/>
<label style=" font-size: 16px;">Last GRN Date</label><br/>
<input type="date" name="date1" ><br/><br/>
<label for="text5" style=" font-size: 16px;">Last GRN Quantity </label></span> <br/>
<input name="Lastqty" type="text" placeholder="Qty" style="width: 100px; height: 28px;" /><br/><br/>
<label for="text4" style=" font-size: 16px;">Print Attempt </label> <br/>
<input name="attempt" type="text" placeholder="eg -: 3rd " style="width: 100px; height: 28px;" /><br/><br/><br/>
<input type="submit" value="Save" class="btn_hover"/>
<input type="reset" value="Reset" class="btn_hover" />
<input name="p_id" type="text" style="width: 100px; height: 28px; visibility:hidden;" />
</div>
</div>
</form>
<!-- Stores -->
<!-- Publishing Department -->
<form action="publishing" method="Post" name="publish" onsubmit="return(validate());">
<div id="tabr2" class="tab-content">
<div class="col_4">
<!-- Check box -->
<input type="checkbox" name="infrom" value="Informed" />
<label for="chek1" class="inline" style="font-size: 16px; font-weight: bold;" >Informed the Author </label><br/><br/><br/>
<!-- Check box -->
<!-- Table sortable -->
<label style="font-size: 18px; font-weight: bold;"> Last Print </label> <br/><br/>
<table width="500px" class="" style=" border: 1px; border-color: navy;" >
<tr>
<td colspan=6 align="center"
style="background-color:teal">
<b>Last Print</b></td>
</tr>
<tr style="background-color:lightgrey;">
<td><b>Print</b></td>
<td><b>Date</b></td>
<td><b>Quantity</b></td>
<td><b>Move</b></td>
</tr>
<%
int count = 0;
String color = "#F9EBB3";
if (request.getAttribute("piList") != null) {
ArrayList al = (ArrayList) request.getAttribute("piList");
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext()) {
if ((count % 2) == 0) {
color = "#eeffee";
}
count++;
ArrayList pList = (ArrayList) itr.next();
%>
<tr style="background-color:<%=color%>;">
<td><%=pList.get(12)%></td>
<td><%=pList.get(5)%></td>
<td><%=pList.get(9)%></td>
<td><%=pList.get(7)%></td>
<!--<input name="4" type="text" value=" <%=pList.get(6)%>" style="width: 100px; height: 28px;" />-->
</tr>
<%
}
}
if (count == 0) {
%>
<tr>
<td colspan=4 align="center"
style="background-color:#eeffee"><b>No Record Found..</b></td>
</tr>
<% }
%>
</table>
<!-- Table sortable -->
<br/><br/>
<label for="text6" style=" font-size: 16px;">Printer </label><br/>
<select name="printer">
<%
ResultSet rs3 = DB.search("Select * from printer_names");
while (rs3.next()) {
out.print("<option value=" + rs3.getString("printer_id") + " >" + rs3.getString("Printer_Name") + "</option>");
}
%>
</select>
<input type="button" value="Add a Printer" style="border-radius: 25px" />
<br/><br/>
<!-- Select -->
<label for="select1">Paper Type</label> <br/>
<select name="paper">
<option value="-1" selected="" >- Choose a Paper Type -</option>
<option value="1">Bank Paper</option>
<option value="2">Book Printing Paper </option>
<option value="3" >Art Paper</option>
<option value="4">Bulk</option>
</select>
<!-- Select -->
<br/><br/>
<label for="text7" style=" font-size: 16px;">Delivery for Printing </label><br/>
<input name="Delivery" type="text" placeholder="Type here" style="width: 355px; height: 28px;" /><br/><br/>
<label for="text8" style=" font-size: 16px;">Quantity Suggested</label><br/>
<input name="qty" type="text" placeholder="Type here" style="width: 355px; height: 28px;" /><br/><br/>
</div>
<div class="col_6" style="padding-left: 100px;">
<label style=" font-size: 16px;">Date</label><br/>
<input type="text" id="date1" name="date1" readonly="true " ><br/><br/>
<label for="text9" style=" font-size: 16px;">Altered Page Numbers </label></span> <br/>
<input name="alt" type="text" placeholder="Eg-: 4 , 5 , 6 , 34" style="width: 355px; height: 28px;" /><br/><br/><br/>
<!-- Check box -->
<input type="checkbox" name="sample" value="Given" id="cheksample" />
<label for="check2" class="inline" style="font-size: 16px; font-weight: bold;">Sample Copies </label><br/><br/><br/>
<!-- Check box -->
<label for="text10" style=" font-size: 16px;">Altered Tracing (No.s) </label> <br/>
<input name="alt_trac" type="text" placeholder="" style="width: 355px; height: 28px;" /><br/><br/>
<label style=" font-size: 16px;">Book Cover</label><br/><br/>
<input type="radio" name="book_cvr" value="Same_Cover" />
<label for="radio4" class="inline" >Same Cover</label><br/><br/>
<input type="radio" name="book_cvr" value="new_Cover" />
<label for="radio5" class="inline">New Cover</label><br/><br/><br/>
<input type="submit" value="Save" class="btn_hover"/>
<input type="reset" value="Reset" class="btn_hover" />
</div>
</div>
</form>
<!-- Publishing Department -->
<!-- Printing Department -->
<form action="Printing" method="Post">
<div id="tabr3" class="tab-content">
<div class="col_4">
<!-- Select2 -->
<label for="select2">Size</label> <br/>
<select id="select2" name="size">
<option value="0">- Choose a Size -</option>
<option value="SD 1/8">SD 1/8</option>
<option value="D 1/8">D 1/8</option>
<option value="D 1/4">D 1/4</option>
<option value="Cr 1/8">Cr 1/8</option>
<option value="Cr 1/4">Cr 1/4</option>
<option value="A4">A4</option>
<option value="A5">A5</option>
<option value="B4">B4</option>
</select><br/><br/><br/>
<!-- Select2 -->
<label style="font-size: 18px; font-weight: bold;">No. of Pages</label><br/><br/>
<label>Black & White -</label>
<input type="text" id="text11" name="BW" style="width: 100px; height: 28px;" ><br/><br/>
<label style="padding-right: 45px">Colour -</label>
<input type="text" id="text12 " name="clr" style="width: 100px; height: 28px;" ><br/><br/><br/>
<label style="font-size: 18px; font-weight: bold;"> Last Production Cost</label><br/><br/>
<!-- Table sortable2 -->
<table width="500px" class="" style=" border: 1px; border-color: navy;" >
<tr>
<td colspan=6 align="center"
style="background-color:teal">
<b>Last Production Cost</b></td>
</tr>
<tr style="background-color:lightgrey;">
<td><b>Year</b></td>
<td><b>Qty</b></td>
<td><b>Cost</b></td>
<td><b>Unit Cost</b></td>
</tr>
<%
int count1 = 0;
String color1 = "#F9EBB3";
if (request.getAttribute("piList") != null) {
ArrayList al = (ArrayList) request.getAttribute("piList");
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext()) {
if ((count % 2) == 0) {
color = "#eeffee";
}
count++;
ArrayList pList = (ArrayList) itr.next();
%>
<tr style="background-color:<%=color%>;">
<td><%=pList.get(5)%></td>
<td><%=pList.get(9)%></td>
<td><%=pList.get(13)%></td>
<td><%=pList.get(14)%></td>
</tr>
<%
}
}
if (count == 0) {
%>
<tr>
<td colspan=4 align="center"
style="background-color:#eeffee"><b>No Record Found..</b></td>
</tr>
<% }
%>
</table><br/><br/>
<!-- Table sortable2 -->
<label style="font-size: 18px; font-weight: bold;"> Estimated Printing Cost</label><br/><br/>
<label for="text13" style=" font-size: 16px;">Quantity</label> <br/>
<input id="text13" type="text" placeholder="" name="qty" style="width: 155px; height: 28px;" /><br/><br/>
<label for="text14" style=" font-size: 16px;">Cost</label> <br/>
<input id="text14" type="text" placeholder="" name="cost" style="width: 155px; height: 28px;" /><br/><br/>
<label for="text15" style=" font-size: 16px;">Unit Cost</label> <br/>
<input id="text15" type="text" placeholder="" name="unit" style="width: 155px; height: 28px;" /><br/><br/>
</div>
<div class="col_3">
<!-- Select3 -->
<label for="select2">Paper</label> <br/>
<select id="select3" name="paper">
<option value="0">- Choose a Paper -</option>
<option value="60 gsm">60 gsm</option>
<option value="70 gsm">70 gsm</option>
<option value="80 gsm">80 gsm</option>
<option value="100 gsm">100 gsm</option>
<option value="120 gsm">120 gsm</option>
<option value="150 gsm">150 gsm</option>
</select><br/><br/>
<!-- Select3 -->
<!-- Select4 -->
<label for="select4">Binding</label> <br/>
<select id="select4" name="binding">
<option value="0">- Choose a Binding Methode -</option>
<option value="Hard">Hard Binding</option>
<option value="Perfect">Perfect Binding</option>
<option value="Suddle">Suddle Binding</option>
<option value="Thread">Thread Binding/Perfect Binding</option>
</select><br/><br/>
<!-- Select4 -->
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<input type="submit" value="Save" class="btn_hover"/>
<input type="reset" value="Reset" class="btn_hover" />
</div>
<div class="col_3">
<label style=" font-size: 16px;">Date</label><br/>
<input type="text" id="date" readonly="true" name="date" ><br/><br/>
</div>
</div>
<!-- Printing Department -->
</form>
<!-- Director -->
<form action="director" method="Post">
<div id="tabr4" class="tab-content">
<div class="col_6">
<label for="text16" style=" font-size: 16px;">Present Price </label><br/>
<input name="present_price" type="text" placeholder="current Price" style="width: 355px; height: 28px;" /><br/><br/>
<label for="text17" style=" font-size: 16px;">Revise Price </label><br/>
<input name="revise_price" type="text" placeholder="new Price" style="width: 355px; height: 28px;" /><br/><br/>
<label for="text18" style=" font-size: 16px;">Quantity </label></span> <br/>
<input name="qty" type="text" placeholder="Qty" style="width: 355px; height: 28px;" /><br/><br/><br/>
<!--<input type="submit" value="Cancel" class="btn_hover" /> -->
<!--<input type="reset" value="Reset" class="btn_hover" />-->
</div>
<div class="col_3">
<label style=" font-size: 16px;">Date</label><br/>
<input type="text" id="date3" readonly="true" name="date3" ><br/><br/><br/>
<label style=" font-size: 20px; font-weight: bolder;" class="inline">APPROVAL </label><br/><br/>
<input type="radio" name="approval" value="APPROVED" />
<label style=" font-size: 16px; font-weight: bold;" class="inline" > Approved </label><br/><br/>
<input type="radio" name="approval" value="NOT_APPROVED" />
<label style=" font-size: 16px; font-weight: bold;" class="inline" > Not Approved </label><br/><br/><br/><br/>
<input type="submit" value="OK" class="btn_hover"/>
</div>
<div class="col_8">
<!-- Textarea -->
<label for="textarea1" style="font-size: 18px; font-weight: bold;"> Remark</label><br/>
<textarea name="remark" placeholder="Type Here" style="width: 650px; height: 200px;" ></textarea>
</div>
</div>
</form>
<!-- Director -->
</td>
</tr>
</table>
<!-- Tabs -->
</body>
`i have made a little webapp in Java using jsp and am having a little problem with Login Authentication..
i have 4 different users. have 4 tabs in a jsp page
i need to give them the limited access to Tabs.
eg-director (can see all the tabs) officer 1 (can see only have accesses to 3 of tabs ) officer 2 (can see only have accesses to 2 of tabs ) officer 3 (can see only have accesses to 1 of tabs )
it is like a security clearance i tried to so many ways and failed and i am thinking to do that with passing a Boolean to Database and give the access levels for that i need a help if it is with a small sample log in project that will be very help full for me
if it can do with the EER also it ill be ok
using JSTL might be your good friend in this situation!
say if servlet return privilege of a logined user with LEVEL
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="${LEVEL >= 4}">
<p>director only page<p>
</c:if>
<c:if test="${LEVEL >= 3}">
<p>officer 1 only page<p>
</c:if>
<c:if test="${LEVEL >= 2}">
<p>officer 2 only page<p>
</c:if>
<c:if test="${LEVEL >= 1}">
<p>officer 3 only page<p>
</c:if>
the code provided above is just a skeleton, you need to change the <p> inside it to your desired tab

Categories

Resources