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.
Related
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.
I am creating a page in thymeleaf where I retrieve a list from my controller to mount a table, in this table there is a selection field where I retrieve a list of values but I am not able to display the value that is already being retrieved of the database can help me? Please!
I already tried to use the following commands but could not recover the value:
th:field="*{valoresAtributo[__${stat.index}__].valorUsuario}"
I receive the following error: Neither BindingResult nor plain target object for bean name available as request attribute.
<table class="table table-sm table-striped table-bordered" id="dataTable">
<thead>
<tr><th colspan="4">Valores dos Atributos</th></tr>
</thead>
<tbody>
<tr class="text-black">
<td rowspan="2"> Atributo</td>
<td rowspan="2"> Local</td>
<td>Aplicação</td>
<td>Usuário</td>
</tr>
<tr>
<td id="vlAppl"></td>
<td id="vlUser"></td>
</tr>
<tr th:each="valorAtributo, stat : ${valoresAtributo}">
<td th:text="${valoresAtributo[__${stat.index}__].nomeAtributo}"></td>
<td th:text="${valoresAtributo[__${stat.index}__].valorLocal}"></td>
<td th:text="${valoresAtributo[__${stat.index}__].valorAplicaco}"></td>
<td>
<select class="form-control col-md-10" th:field="*{valoresAtributo[__${stat.index}__].valorUsuario}">
<option th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"
th:value="${{option.valorAtributo}}"
th:text="${option.significadoAtributo}">
</option>
</select>
</td>
</tr>
</tbody>
</table>
#RequestMapping(value="/seguranca/atributo/valores", params = {"atributo","siteOpt","applOpt","userOpt","aplicacao","usuario"}, method = RequestMethod.GET)
public String initAttributeValuesFormFilter(#RequestParam("atributo") Long idAtributo, #RequestParam("siteOpt") Integer idNivel1, #RequestParam("applOpt") Integer idNivel2, #RequestParam("userOpt") Integer idNivel3, #RequestParam("aplicacao") String aplicacao, #RequestParam("usuario") String usuario, Model model)
{
Integer userId = 0;
if(!Strings.isNullOrEmpty(usuario))
userId = userServiceImpl.findIdUsuarioByFantasia(usuario).intValue();
List<ValoresAtributoView> valoresAtributo = buscaValoresAtributos(idAtributo, idNivel1, idNivel2, idNivel3, 0, userId);
model.addAttribute("opUsuarios", userServiceImpl.findAllActiveUsers());
model.addAttribute("opAtributos", atributoService.findAll());
model.addAttribute("valoresAtributo",valoresAtributo);
return "/valoresAtributo";
}
I expected the field to display the value that is currently in the database and the options in the list of values.
Thank you all!
You can only use th:field if you are using th:object in a parent <form /> tag. (Which isn't clear from the HTML you posted -- but you probably are not since your are adding valoresAtributo directly as a model attribute.)
If you wish to show a preselected <option> but without using th:object and th:field you should instead use the th:selected attribute which should evaluate to true or false based on if that option should be selected. It should look something like this:
<select class="form-control col-md-10">
<option
th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"
th:value="${{option.valorAtributo}}"
th:text="${option.significadoAtributo}"
th:selected="${valorAtributo.valorUsuario == option.valorAtributo}" />
</select>
My way to retrieve list using thymeleaf:
For example list users with list of User entity getting to html page:
#GetMapping("/parsing/explorer")
public ModelAndView parsing(){
ModelAndView modelAndView = new ModelAndView("show");
modelAndView.addObject("users", generateUsersList());
return modelAndView;
}
show.html example:
<table class="w3-table-all w3-card-4">
<tr>
<th>Id</th>
<th>Name</th>
<th>Last name</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.getId()}"></td>
<td th:text="${user.getName()}"></td>
<td th:text="${user.getLastName()}"></td>
</tr>
</table>
So list of User (users) is assigned to the variable user in string <tr th: every = "user: $ {users}">
In the next block of code, we can also call the "User" fields the same way as java using getters, but Thymeleaf also allows you to refer to the fields: user.id/ user.name....
Determining variable in the select block:
<select name="userFromHtml" id=userId required>
<option th:each="user: ${users}">
<title th:text="${user.getLastName()} + ' ' + ${user.getName()} + ' ' + ${user.getIdIO}" th:value="${user.getId()}"/>
</option>
</select>
in this case is necessary to determine th:value in title block: th:value="${user.getId()}". So, in controller is passed variable userFromHtml with value id of selected user.
PS Although Thymeleaf allows you to define variable in direct, I prefer to use getters for data getting.
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.
I have my controller method:
#RequestMapping(value = "/provide", method = RequestMethod.GET)
public String list(Model model) {
List<Questionare> provide = scs.getPending();
logger.info("Number of questionares: "+provide.size());
model.addAttribute("certDatas", provide);
return "ssl/provide";
}
I have added logger.info in order to check if my list is being correctly created. It is. My logger log info such as :
2014-07-04 09:46:04,118 INFO
[pl.test.QuestionareControler] Number of questionares: 163
Now I want to display those 163 object in my jsp page in form of table like this:
<c:if test="${not empty provide}">
<table class="grid" style="width: 850px;">
<tr>
<th style="width: 100px;">Name</th>
<th style="width: 100px;">Surname</th>
<th style="width: 100px;">email</th>
</tr>
<c:forEach var="person" items="${provide}">
<%
i++;
%>
<tr>
<td>${person.name}</td>
<td>${person.suername}</td>
<td>${person.smtp}</td>
</c:forEach>
</table>
</c:if>
However my jsp page does not show that table (I mean if condition is not met I suppose, because I do not even see table header)
Can anyone give me a hint what am I doing wrong?
Your model is called certDatas and not provide.
Try this:
<c:if test="${certDatas != null}">
Check out the JSP expression language here:
http://www.tutorialspoint.com/jsp/jsp_expression_language.htm
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.