How to create a dynamic URL in SPRING MVC - java

I have a requirement to open a PDF when the user clicks on a particular employee id present in a table. Like:
Employee id url
123 View
234 View
When user clicks on the View corresponding to that row, details of Employee id should open in a pdf.
Someone please give me pointers on how to move forward on this. I am using JAVA Spring MVC framework.
Also need to know how will I create VIEW hyperlink, as this comes in a coulmn of a table. I am populating this table from Java Class.
My Controller Class:
#RequestMapping
public ModelAndView getHeaders(PortletRequest portlestRequest, PortletResponse portletResponse){
TableDAO dao = new TableDAO();
List<String> headersList = dao.getHeaders();
TableView tableView = new TableView();
tableView.setGetHeaderCount(headersList.size());
//System.out.println("tableView::");
tableView.setTableHeaders(headersList);
List prepaidBillingDetailsList = new ArrayList();
prepaidBillingDetailsList = dao.getPrepaidBillingInfo();
tableView.setPrepaidBillingDetailsList(prepaidBillingDetailsList);
return new ModelAndView("tableView", "tableView", tableView);
}
My JSP:
<table border="1px" bordercolor="black" width=80% align="center">
<tr>
<c:forEach var = "headerList" items = "${tableView.tableHeaders}">
<th>
${headerList}
</th>
</c:forEach>
</tr>
<c:forEach var = "headerList" items = "${tableView.prepaidBillingDetailsList}">
<tr>
<c:forEach var = "headerList" items = "${headerList.stringlst}">
<td>
${headerList}
</td>
<td>
<a href="<c:url value="editEmployee.htm">
<c:param name="msisdn" value="${tableView.msisdn}"/>
</c:url> ">Edit</a>
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
In this, First headerList will get my eadrs of the table, Next header list will display the data for of my employee class. Now in the last coulmn i need inks, which will take me to the PDF of that particular employee.

You can form your view link url something like www.appdomain.com/App/Profile?id=123
view
and then map your controller with this url and get the id parameter from url and with the id get the details of the user and display it.
Second way is you can also get the url like www.appdomain.com/App/Profile/123
view
i.e. id as a path variable and get this path variable in the controller and get the details and display.

Related

How to add the result variable of form submission to mapping?

I have this form in a jsp file where I dynamically populate a dropdown from a list. It works fine. This is the home jsp at core address "/" and it sends data to "/spravochnik/list".
<form action="/spravochnik/list" method="post">
<select name="tableName">
<c:forEach var="dropDown" items="${dropDown}">
<c:forEach var="valuesInRows" items="${dropDown.valuesInRows}">
<c:forEach var="valuesInRow" items="${valuesInRows}" varStatus="loop">
<c:if test="${loop.index %2 != 0}">
<option><c:out value="${valuesInRow}"></c:out></option>
</c:if>
</c:forEach>
</c:forEach>
</c:forEach>
</select>
<input type="submit" value="submit" />
</form>
Now I have a controller
#RequestMapping(value = "/spravochnik/list", method = { RequestMethod.POST, RequestMethod.GET })
public String getSpravochniks(Model m, #RequestParam("tableName") String tableName) {
Integer idPrepend = spravochnikService.getIdAtSpravName(tableName);
List<Spravochnik> spravList = spravochnikService.findAll(tableName);
m.addAttribute("spravList", spravList);
m.addAttribute("tableName", tableName);
m.addAttribute("idPrepend", idPrepend);
return "home";
}
It also works fine, and the resulting table (what I got from findAll) is shown with address /spravochnik/list. Question: How can I show it with address depending on what variable "tableName" i sent from the home.jsp? For example, if in the dropdown I select "employees" and pass it with the form under the name "tableName", I want the result to be shown under /spravochnik/list/employees and if I choose "vehicles" it should be "/spravochnik/list/vehicles"?
What I tried
-Adding variable to action
and in controller #RequestMapping(value = "/list/{tableName}" and tableName as #PathVariable. Actual Result: Not Found.
-The same with #RequestParam instead of Path Variable. Actual result: Not Found.

Unable to grab an element in a dynamic table where we have only text of a td tag

I have a table whose data changes according to what is added and deleted. In the table there are multiple columns Name, Qty, type, Status. All i have is the Name Text with me , i need to find the status field of that row which has that Name.
Problem is the html tags of have same class names and i tried grabbing the parent and sibling everything failed. Please find the html structure of the table below:
<table>
<thead> </thead>
<tbody>
<tr>
<td class = "c1">
<a class = txtclass> text1 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "1" class = "status1"> /div>
</td>
</tr>
<tr>
<td class = "c1">
<a> text2 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "2" class = "status2"> /div>
</td>
</tr>
</tbody>
</table>
So all i have with me is text2 and i need to get the with the status of that row.
How do i proceed. I tried
List<WebElement> ele = driver.findElements(By.xpath("//*[#class = 'txtClass'][contains(text(),'text')]"));
for(WebElement el1:ele)
{
WebElement parent = el1.findElement(By.xpath(".."));
WebElement child1= parent.findElement(By.xpath("//td[4]/div"));
System.out.println(child1.getAttribute("class"));
}
this gives me the class name of the status of the first row in the table always.
Same i tried with
WebElement child = el1.findElement(By.xpath("//following-sibling::td[4]/div[1]"));
i got the same thing class name of the first row in the table. I figured since the class name of the are same for all child elements it will always grab the first row elements, and not the one from the row.
Please help i am stuck here for long, let me know if you need any other details.
You are trying using -
el1.findElements(By.xpath("//following-sibling::td[4]/div[1]"));
It is matching all the element present with format td[4]/div[1] in your page and retrieving first match.
You have to use following xpath to grab status present under div based on you text.
driver.findElement(By.xpath(".//tr/td[contains(.,'text1')]/following-sibling::td[3]/div")).getAttribute("class");
If your requirement to extract all status try following code-
List<WebElement> allElements = driver.findElements(By.xpath(".//tr/td[contains(.,'text2')]/following-sibling::td[3]/div"));
for(WebElement element:allElements)
{
String status = element.getAttribute("class");
System.out.println(status);
}
I think this approach suitable for you:
Get all div elements containt attribute status :
List<WebElement> listChildStatus = driver.findElements(By.xpath(".//tr[.//a[contains(.,'text')]]//div"));
Get specific div elements containt attribute status :
WebElement childStatus = driver.findElement(By.xpath(".//tr[.//a[contains(.,'{TEXT}')]]//div"));
{TEXT} = the text value that you have
You need to locate the element with the text, go one level up, and then get the sibling with the status
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[3]/div"));
String status = statusElement.getAttribute("class"); // will be status2
If you don't want to relay on index you can use last() to find the last sibling
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[last()]/div"));

Getting value from s:select inside an iteration

I am iterating a list object in jsp, I want to get the <s:select> value when submitting the form.
My jsp is like this:
<s:iterator value="listeTypeEvent" status="event">
<tr>
<td>
<s:select id = "criticite"
list = "criticite"
name = "degreCriticite"
value = "%{event.degreCriticite}"/>
</td>
<td>
<s:select id = "famlle"
list = "listFamille"
name = "familleEvent.idFamille"
listKey = "idFamille"
listValue = "libelleFamille"
value = "%{event.idFamille}"/>
</td>
</tr>
</s:iterator>
The error is that the code in the question is confusing status (that instantiates an IteratorStatus object, and does not have degreCriticite nor idFamille attributes) with var (that instantiates a variable containing the current element in the iteration).
Another minor error is that id must be unique in an HTML page.
The missing piece to send back a value of an element created inside an iteration is the creation of an index (through the help of the aforamentioned IteratorStatus object):
<s:iterator value="listeTypeEvent" var="event" status="ctr">
<tr>
<td>
<s:select id = "criticite_%{#ctr.index}"
list = "criticite"
name = "degreCriticite[%{#ctr.index}]"
value = "%{event.degreCriticite}"/>
</td>
<td>
<s:select id = "famlle_%{#ctr.index}"
list = "listFamille"
name = "familleEvent[%{#ctr.index}].idFamille"
listKey = "idFamille"
listValue = "libelleFamille"
value = "%{event.idFamille}"/>
</td>
</tr>
</s:iterator>

Passing selected rows from one jsp table to another jsp table

In my jsp(index.jsp) I display some items in table with checkbox at the beginning of each row. If user check a row and click the confirm button it goes to other jsp(second.jsp) where I need to display all the rows which are checked in index.jsp.
my table looks like this and assume i enclosed table in form tag.
<form:form method="post" action="/somereq" modelattribute="transferInvoice">
<table id="assets-tbl" border="1"
class="table table-bordered table-hover table-striped"
<tbody id="assets-tbl-body" >
<c:forEach items="${transferinvoice}" var="invoice" varStatus="status">
<tr>
<td><input id="cartcheckbox" class="case" type="checkbox" name="case" value="${invoice}"/></td>
<td>${invoice.some}</td>
<td>${invoice.num}</td>
<td>${invoice.name}</td>
<td>${invoice.loca}</td>
<td>${invoice.time}</td>
</tr>
</c:forEach>
</tbody>
Can any one help me?
I tried this but some how able to fetch the selected rows into controller but how to pass these values to another jsp as java bean type unless can't able to parse the results in second.jsp
#RequestMapping(value = "somereq", method = RequestMethod.POST)
protected ModelAndView transferInvoiceConfirm(final HttpServletRequest request, final HttpServletResponse response)throws Exception {
Model model = new ExtendedModelMap();
String[] checkeditems =request.getParameterValues("caseob");
List<String> list = Arrays.asList(checkeditems);
List<beantype> invoicelist = new ArrayList<beantype>(list);
model.addAttribute("invoiceList", list);
return new ModelAndView("asset/secondform", model.asMap());
}
and in the second jsp, I am displaying the checked items in table as
<c:forEach items="${invoiceList}" var="invoice" varStatus="status">
<tr>
<td>${invoice.name}</td>
<td>${invoice.code}</td>
<td>${invoice.model}</td>
<td>${invoice.loc}</td>
<td>${invoice.date}</td>
</tr>
</c:forEach>
Somehow i feel, i am doing wrong things. Can any one please help to fix this issue?
Change this line
String[] checkeditems =request.getParameterValues("caseob");
To this line
String[] checkeditems =request.getParameterValues("case");
Because as you defined, your input name is "case" .
<td><input id="cartcheckbox" class="case" type="checkbox" name="case" value="${invoice}"/></td>
I hope this helps you!
Write a javascript which will trigger on onclick event of the Confirm button. In the javascript made a request for the second.jsp.

Filter table values on JSP with values from SQL database

I'm trying to make an servlet-based application that gets values from a SQL database and presents them in a dynamically created table on a JSP, which is already working. The next step is to create some sort of filter that only exhibits the table lines that have a certain value in one of its columns. My idea was to add a dropdown menu and select the lines with that item in the "Problem" column. I believe that to be a bit straightforward. The catch is that the values to fill in that dropdowm menu are in a table "Problem" on the SQL database; and each of its rows have the id and name attributes. Currently, I'm trying to go with the name attribute and trying to compare it with the "Problem" column that is obtained from my query (SQL Server 2008 R2) (
SELECT h.[name], c.[department], p.[name] AS problem, it.[name] AS interaction, ip.[title], ip.[date]
FROM [Database].[dbo].[Input] ip, [Database].[dbo].[Interaction] it, [Database].[dbo].[Problem] p, [Database].[dbo].[HelpdeskUser] h, [Database].[dbo].[Costumer] c
WHERE h.[id] = ip.[user_id]
AND it.[id] = ip.[interaction_id]
AND c.[id] = ip.[costumer_id]
AND p.[id] = ip.[problem_id]
ORDER BY date DESC";
), but, obviously, I'm hitting my head against the wall because this isn't working. So I'd like to ask you if you can help me somehow with this and if I'm overlooking something obvious that could unlock this whole process.
ViewInputServlet.java
public class ViewInputServlet extends GenericServlet {
public static final String PROBLEMS = "problems";
public static List<Problem> problems;
private void setProblems(HttpServletRequest req, HttpServletResponse resp) throws EmptyResultSetException {
Session session = HibernateUtilities.getSessionFactory().openSession();
if (ProblemDAO.hasRecords(session)) {
problems = ProblemDAO.selectProblems(session);
req.setAttribute(PROBLEMS, problems);
}
}
}
ViewInput.jsp
<% List<Problem> problems = (List<Problem>) request.getAttribute(ViewInputServlet.PROBLEMS); %>
<div class="innerField">
<table class="datatable" id="tableResults">
<thead>
<tr>
<th>NAME</th>
<th>DEPARTMENT</th>
<th>PROBLEM</th>
<th>TITLE</th>
<th>DATE</th>
</tr>
</thead>
<% for (Issue issue : (List<Issue>) request.getAttribute(ViewInputServlet.LIST)) {%>
<tr>
<td><%=issue.getName()%></td>
<td><%=issue.getDepartment()%></td>
<td id="prob_type"><%=issue.getProblem()%></td>
<td><%=issue.getTitle()%></td>
<td><%=issue.getDate()%></td>
</tr>
<%}%>
</table>
<div class="label">Show by Problems: </div>
<div class="field">
<div class="ui-widget">
<select name="<%=ViewInputServlet.PROBLEMS%>" id="chooseProblems">
<%if (problems != null) {
for (Problem problem : problems) {%>
<option value="<%=problem.getName()%>"><%=problem.getName()%></option>
<%}
}%>
</select> <input type="button" value="Reset" id="btn_reset" />
</div>
</div>
</div>
(the column "Problem" is the key here: it's the one I want to filter values by, and that's why I've given it an ID, in a previous attempt that failed)
Functions.js
$("#chooseProblems").change(function () {
$("#tableResults").find("td").each(function () {
if ($(this).text !== $("#chooseProblems").val())
$(this).hide();
else
$(this).show();
});
});
If there's some more info you require or doubts on my reasoning, ask away :)
Could there be a problem with your string comparison in java script. should you be using match method instead of !==.
Also when you say $(this).text or $(this).hide or $(this).show , you are actually referring to column of row not the row.

Categories