I'm trying to submit selected items from a table and moake some modifications on them but I couldn't get it work.
MyObject.java
public class MyObject{
boolean checkControl = true; //default true
private String name;
private String code;
//getters & setters
}
MyObjectForm.java
public class MyObjectForm {
private List<MyObject> myList;
public List<MyObject> getMyList() {
return myList;
}
public void setMyList(List<MyObject> myList) {
this.myList= myList;
}
}
list-myObjects.jsp
<form:form action="submitList" method="post" modelAttribute="myObjectForm">
<table>
<tbody>
<c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
<tr>
<td>
<spring:bind path="myList[${status.index}].checkControl">
<input type="checkbox" value="<c:out value="${status.value}"/>" name="isChecked" <c:if test="${row.checkControl}"> checked="checked" </c:if> />
</spring:bind>
</td>
<td>${row.name}</td>
<td>${row.code}</td>
</tr>
</c:forEach>
</tbody>
</table>
<button type="submit">Submit</button>
</form:form>
And the controller
#RequestMapping(value = "/submitList", method = RequestMethod.POST)
public String save(#ModelAttribute("myObjectForm") MyObjectForm myObjectForm, Model model) {
List<MyObject> selectedtList = myObjectForm.getMyList(); //returns null
if (selectedtList == null) {
System.out.println("no objects selected");
}
else {
//Make some computation
}
model.addAttribute("resultArray", selectedtList);
return "display-items";
}
Some users asked me to explain this in more detail by email. So I decided to submit it here, Hope this helps.
I'm using spring annotations, to do the job. here is what I did to process selected checkboxes,
I have a java entity class which includes a boolean value for the checkbox, for example a Person class
// Person.java
class Person {
private Long id;
private String name;
private boolean check;
//here goes getters and setters
}
than I have a form object in java, which contains a list of Person
//PersonForm.java
class PersonForm {
private List<Person> personList;
//getters and setters here
}
in my case, there are two jsp pages, the first one lists items, with checkboxes, and the second one lists selected items.
the first jsp file is list.jsp
//list.jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="datatables" uri="http://github.com/dandelion/datatables"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
....
<body>
<form:form action="sent-list" method="post" modelAttribute="personForm">
<table id="item-list" class="table table-striped table-bordered">
<thead class="dataTableHeader">
<tr>
<th width="10%" style="text-align:center">
CheckBox
</th>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<c:forEach items="${personForm.personList}" var="listItem" varStatus="status">
<tr>
<td style="text-align:center">
<form:checkbox path="listItem[${status.index}].check"/>
</td>
<td>${listItem.id} <form:hidden path="listItem[${status.index}].id"/></td>
<td>${listItem.name} <form:hidden path="listItem[${status.index}].name"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<button class="btn btn-large btn-success pull-right" type="submit">POST</button>
</form:form>
</body>
</html>
the second jsp file is as follows
//sent-list.jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="datatables" uri="http://github.com/dandelion/datatables"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
.....
<body>
<form:form action="#" method="post" modelAttribute="personForm">
<table id="item-list" class="table table-striped table-bordered">
<thead class="dataTableHeader">
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<c:forEach items="${resultList}" var="personItem" varStatus="status">
<tr>
<td>${personItem.id}</td>
<td>${personItem.name}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form:form>
</body>
</html>
and finally there is a controller, which makes the computation
//PersonController.java
#Controller
class PersonController {
#RequestMapping(value = "/sent-list", method = RequestMethod.POST)
public String save(#ModelAttribute("personForm") PersonForm personForm, Model model){
for(Person personItem : personForm.getPersonList){
//make some computation here
}
}
}
I hope this helps.
Sounds like a binding issue. Have you tried using Spring's <form:checkbox> tag rather than <spring:bind>? It will automatically generate the checkbox attributes as well as adding a hidden field that Spring uses to determine whether the checkbox is 'on' or 'off'.
<form:form action="submitList" method="post" modelAttribute="myObjectForm">
<table>
<tbody>
<c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
<tr>
<td>
<form:checkbox path="myList[${status.index}].checkControl"/>
</td>
<td>${row.name}</td>
<td>${row.code}</td>
</tr>
</c:forEach>
</tbody>
</table>
<button type="submit">Submit</button>
</form:form>
Related
I have a problm with update link for updating my transaction. At one jsp I have a list with transactions, and after pressing Update" which is link i wanted to pass transactionId, update it, save and get back to list. I've used c:param hidden and it doesnt work. I have no idea why it doesnt pass id and autofill forms. It seems like values are passed in session when ive clicked testing transaction:
http://localhost:8080/transaction/addTransaction?transactionId=64&userId=1
Here is transaction Controller:
#Controller
#RequestMapping("/transaction")
public class TransactionController {
#Autowired
TransactionService transactionService;
#Autowired
CategoryService categoryService;
#Autowired
UserService userService;
#GetMapping("/addTransaction")
public String transactionsList(Model theModel){
Transaction transaction = new Transaction();
theModel.addAttribute("user",userService.getAllUsers());
theModel.addAttribute("category", categoryService.getAllCategories());
theModel.addAttribute("newTransaction", transaction);
return "addTransaction";
}
#PostMapping("/saveTransaction")
public String saveTransaction(#ModelAttribute("newTransaction") Transaction
newTransaction,
BindingResult theBindingResult,
HttpServletRequest request) throws
ParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = request.getParameter("transactionDate");
LocalDate localDate = LocalDate.parse(date, formatter);
Date formatedDate =
Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.ofHours(-3)));
newTransaction.setTransactionDate(formatedDate);
transactionService.saveTransaction(newTransaction);
return "redirect:/user/userPage";
}
#GetMapping("/deleteTransaction")
public String deleteUser(#RequestParam("transactionId") int idFromTransactionToDelete,
#RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes){
transactionService.deleteTransactionById(idFromTransactionToDelete);
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
return "redirect:/user/userPage";
}
#GetMapping("/updateTransaction")
public String updateUser(#RequestParam("transactionId") int
idFromTransactionToUpdate,
#RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes,
Model theModel){
Transaction transactionToUpdate = transactionService.getSingleTransactionById(idFromTransactionToUpdate);
transactionToUpdate.setUser(userService.getUserById(loggedUserId));
theModel.addAttribute("newTransaction",transactionToUpdate);
theModel.addAttribute("category", categoryService.getAllCategories());
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
redirectAttributes.addFlashAttribute("newTransaction",
transactionToUpdate);
return "addTransaction";
}
}
Transactions list page:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<html>
<head>
<title>User page</title>
</head>
<body>
<c:url var="addTransactionLink" value="/transaction/addTransaction"/>
<c:url var="addFixedTransactionLink"
value="/fixedTransaction/addFixedTransaction"/>
<h1>Welcome ${loggedUser.login} !</h1>
<br>
<h2>Here are your latest transactions:</h2>
<br>
<a href=${addTransactionLink}>
<input type="button" value="Add Transaction"/>
</a>
<a href=${addFixedTransactionLink}>
<input type="button" value="Add Fixed Transaction"/>
</a>
<table>
<tr>
<th>Category</th>
<th>Price</th>
<th>Description</th>
<th>Date</th>
</tr>
<c:forEach var="transaction" items="${userTransactions}">
<c:url var="deleteTransactionLink"
value="/transaction/deleteTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<tr>
<td>${transaction.category.categoryName}</td>
<td>${transaction.moneyAmount}</td>
<td>${transaction.description}</td>
<td>${transaction.transactionDate}</td>
<td>Delete </td>
<td>Update </td>
<td>${transaction.transactionId}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
And add transaction formular:
<%# taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add Transaction Form</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/resources/css/style.css"/>
</head>
<body>
<h1>Add transaction to database:</h1>
<form:form action="saveTransaction" modelAttribute="newTransaction"
method="post">
<form:hidden path="transactionId"/>
${newTransaction.toString()}
${newTransaction.transactionId}
<table>
<tr>
<td><label>Choose category:</label></td>
<td><form:select name="category.id" path="category.id">
<c:forEach items="${category}" var="category">
<form:option value="${category.id}">${category.categoryName}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Choose user:</label></td>
<td><form:select name="user.id" path="user.id">
<c:forEach items="${user}" var="user">
<form:option value="${user.id}">${user.login}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Amount:</label></td>
<td><form:input path="moneyAmount" />
</td>
</tr>
<tr>
<td><label>Transaction date:</label></td>
<td><form:input type = "date" path="transactionDate"/>
</td>
</tr>
<tr>
<td><label>Description:</label></td>
<td><form:input path="description" />
</td>
</tr>
<tr>
<label></label>
<td><input type="submit" value="Submit" class="save"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Problem seems to be in your code. You have defined in jsp:
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
while in java code:
#GetMapping("/updateTransaction")
public String updateUser(#RequestParam("transactionId") int
In JSP Change it to :
<c:url var="updateTransactionLink"
value="/transaction/updateTransaction">
I have my Struts2 Action class as below and I am expecting the actionerror message should be displayed in my JSP page using the tag: <s:actionerror />
However the message did not show up, and I've found that if I change in the getModel() method return form; to return null;, the error message could be displayed again! How can I show the error message at the same time returning the form object in getModel() method?
public class StartSearchApplicationAction
extends ActionSupport
implements ModelDriven, SessionAware {
protected Map<String, Object> session;
private Formbean form;
public String execute() {
addActionError("Testing Error Message");
session.put("form", form);
return "success";
}
public Formbean getModel() {
form = (Formbean) session.get("form");
if (form == null) {
form = new Formbean();
}
return form;
}
public void setSession(Map<String, Object> session){
this.session = session;
}
}
Updated on 20-Oct-2015 - My JSP (it is the tiles template page)
Note that even I change the statement <s:if test='%{#session.hasError == "Y"}'> to <s:if test="hasActionErrors()">, the result is the same
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%# page language="java" %>
<%# page contentType="text/html; charset=UTF-8" %>
<%# page errorPage="/jsp/error.jsp" %>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><s:text name="global.heading.title"/></title>
</head>
<body>
<table class="main" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="860" valign="top" bgcolor="#FFFFFF">
<table border="0" valign="top" align="left">
<s:if test='%{#session.hasError == "Y"}'>
<tr>
<table width="500" border="0" cellspacing="0" cellpadding="2" align="center" bgcolor="#006600">
<tr>
<td width="16" class="ErrorMessageBoxTitle"><img src="/<s:text name="global.system.root"/>/images/smessage.gif" width="16" height="14" align="absmiddle" alt="System Errors"></td>
</tr>
<tr>
<td colspan="2" class="FixTdSize">
<table width="496" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
<tr>
<td align="center" class="FixTdSize">
<table border="0" cellspacing="12" cellpadding="0" width="480">
<tr>
<td width="35" class="ErrorMessageTitle"><img src="/<s:text name="global.system.root"/>/images/messager.gif" width="31" height="31" alt="System Errors"></td>
<td class="ErrorMessageTitle" width="409"> </td>
</tr>
<tr>
<td width="35" class="ErrorMessageBody"> </td>
<td class="label" width="409"><font color=red><s:actionerror/></font></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</tr>
<tr><td> </td></tr>
</s:if>
<tr>
<td height="30"><tiles:insertAttribute name="searchpanel"/></td>
</tr>
<tr>
<td><img src="/<s:text name="global.system.root"/>/images/line.gif" width="100%" height="2"></td>
</tr>
<tr>
<td><tiles:insertAttribute name="message"/></td>
</tr>
<tr>
<td><tiles:insertAttribute name="body"/></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</table>
<tiles:insertAttribute name="menu"/>
</body>
</html>
after investigation I finally did it with referencing to this question:
Passing parameters to action through ModelDriven in Struts 2
I think the reason is Modeldriven interceptor pushes the model on top of the value stack (i.e. 0-index) and thus the jsp could not access the actionError (the original 0-index was the action class).
Instead of using <s:actionerror/>, the page could able to display the actionError using <s:property value="[1].actionErrors[0]"/>, I am not sure whether this is a good approach but it serve the purpose.
Hi I am getiing the above exception : java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Projet' available as request attribute. Please find my jsp and controller class.
The code I have is like below.
Controller code:
#Controller
#RequestMapping(value="/directeur")
public class AdminController {
#Autowired
private IAdmin directeur;
#RequestMapping(value="/index")
public String index(Model model)
{
model.addAttribute("Projet",new Projet());
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
#RequestMapping(value="/saveProjet",method = RequestMethod.POST)
public String saveProjet(Projet p,BindingResult result,Model model) throws IOException
{
if(result.hasErrors())
{
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
directeur.ajouterProjet(p);
model.addAttribute("Projet",new Projet());
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
}
Jsp Code:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<div id="formProjet">
<f:form modelAttribute="Projet" action="saveProjet" method="POST"
enctype="multipart/form-data">
<table>
<tr>
<td>ID Projet</td>
<td><f:input path="idProjet" /></td>
<td><f:errors path="idProjet" cssClass="errors"/></td>
</tr>
<tr>
<td>Nom</td>
<td><f:textarea path="nomProjet" /></td>
<td><f:errors path="nomProjet" cssClass="errors"/></td>
</tr>
<tr>
<td>Description du Projet</td>
<td><f:textarea path="description" /></td>
<td><f:errors path="description" cssClass="errors"/></td>
<tr><td><input type="submit" value="enregistrer"/></td></tr>
</table>
</f:form>
</div>
<div id="">
<table>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Description</th>
<th>Photo</th>
</tr>
<c:forEach items="${Projets }" var="proj">
<tr>
<td>${proj.idProjet }</td>
<td>${proj.nomProjet }</td>
<td>${proj.description }</td>
<td></td>
</tr>
</c:forEach>
</table>
</div>
Please help me to solve a problem!
I have table created in JSP and filled with data from my DB, and the problem is - I have not idea how to access that JSP data from my controller.
For example - I need to pass appropriate ID (just String) to my controller from JSP to execute Delete method.
My jsp:
<body>
<form action="/editCategory" method="POST">
<h3>Existing categories</h3>
<%
List<Category> categories = (List<Category>) request.getAttribute("model");
if (categories != null) {
%>
<table border="1">
<tr>
<th width="24">ID</th>
<th width="80">Name</th>
<%--<th></th>--%>
</tr>
<%
for (Category category : categories) {
%>
<tr>
<td><%= category.getId() %> <% request.setAttribute("id", category.getId());%>
</td>
<td><%= category.getName() %>
</td>
<td>
<input type="submit" name="delete" value="Delete"/>
</td>
</tr>
<%
}
%>
</table>
<%
} else {
%>
<b>Categories list is empty :(</b>
<%
}
%>
</form>
</body>
My controller methods:
public Model getModel(HttpServletRequest req, HttpServletResponse resp) throws DBException
{
if (req.getParameter("submit")!=null){
addCategory(req);
}
if (req.getParameter("delete")!=null){
deleteCategory(req);
}
categories = categoryDAO.getCategories();
return new Model("/editCategory.jsp", categories);
}
private void deleteCategory(HttpServletRequest req) {
System.out.println(req.getAttribute("id") + " printed");
}
For now I want just to see that correct ID is taken!
Please help!
you could do it this way:
<%
for (Category category : categories) {
%>
<tr>
<td><%= category.getId() %>
<input type="hidden" name="allIds" value="<%= category.getId() %>" /></td>
<td><%= category.getName() %>
</td>
<td>
<input type="submit" name="delete" value="Delete"/>
</td>
</tr>
<%
}
%>
as the type says hidden is not visible.
after that read the values in your servlet with:
String[] lAllIds = request.getParameterValues("allIds");
Good day.
I have a JSP view, and a controller, which gives it a model to be populated with.
Here is my controller`s method for JSP.
#RequestMapping("/seller")
public ModelAndView seller() {
if(isUserInRole("SELLER_ROLE"))
{
List<SellerStatistics> entities = sellerService.getAllStatistics().getContent();
List<String> statuses = sellerService.getStatuses();
Map<String, Object> model = new HashMap<String, Object>();
model.put("gridRows", entities);
model.put("statuses", statuses);
return new ModelAndView("seller",model);
}
return new ModelAndView("login");
}
gridRows contains information which must be inserted into table in JSP. One of the columns in that table is "status". Status must be a drop down box to be able to change this row`s status.
Here is my JSP:
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# include file="header.jsp" %>
<div class="container">
<div class="row">
<div class="span12">
<h1>Sellers welcome</h1>
<div class="top-menu">
<span>Период</span><input type="text">
<span>Период</span><input type="text">
<span>Период</span><input type="text">
<span>Период</span><input type="text">
<span>Период</span><input type="text">
<input type="button">
</div>
<table class="table table-bordered table-hover table-condensed">
<thead>
<tr>
<th>id</th>
<th>Email</th>
<th>Телефон</th>
<th>Дата регистрации</th>
<th>Сайт откуда первый раз пришел</th>
<th>Первый поисковый запрос</th>
<th>Оплата</th>
<th>Счет/Реализация/Счет фактура</th>
<th>Журнал посещений</th>
<th>Журнал коммуникаций</th>
<th>Статус</th>
</tr>
</thead>
<c:forEach items="${gridRows}" var="cur">
<tr>
<td>${cur.id }</td>
<td>${cur.email }</td>
<td>${cur.phone}</td>
<td><fmt:formatDate pattern='dd.MM.yyyy HH:mm' value='${cur.registrationDate}'/></td>
<td>${cur.referer}</td>
<td>${cur.searchQuery}</td>
<%--Payments--%>
<td>
<c:forEach items="${cur.payments}" var="payment">
<fmt:formatDate pattern='dd.MM.yyyy' value='${payment.created}'/>
${payment.value} рублей
</c:forEach>
</td>
<td>${cur.aii}</td>
<td>${cur.visits} страниц<br>Подробно</td>
<td>
<c:forEach items="${cur.communicationLog}" var="communication">
${communication.time} ${communication.description}
</c:forEach>
</td>
<td>
<!--Status must be here-->
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
How do i create drop down box in JSP, and how do i select the current value for each row?
Try this:
<select>
<c:forEach var="item" items="${statuses}">
<c:choose>
<c:when test="${cur.status == item}">
<option selected>${item}</option>
</c:when>
<c:otherwise>
<option>${item}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>