I am a newbie to spring boot and thymeleaf,
I have a list of books ina table with checkboxes, I am not sure how to pass selected booksId s from the view to the controller and use them by the borrow or Return bttons? could you please help?
Here is my Html file https://wtools.io/paste-code/b5g4
and this is the relevant part from my bookService implementation :
public void borrowBook(String userEmail, String bookIds, Model model) {
if (!CollectionUtils.isEmpty(books)) {
User user = userRepository.findByEmail(userEmail);
List<String> requestedBooks = getRequestedBookIds(bookIds);
List<Book> borrowedBooks = new ArrayList<>();
List<Book> invalidBooks = new ArrayList<>();
for (Book book : books) {
if (requestedBooks.contains(book.getId()) && !book.isBorrowed() && user != null) {
book.setBorrowed(true);
book.setBorrowedBy(user.getFirstName());
borrowedBooks.add(book);
model.addAttribute("bookStatus", "Book BOrrowed By " + user.getFirstName());
} else {
invalidBooks.add(book);
model.addAttribute("bookStatus", "No Books are available");
}
}
model.addAttribute("inValidBooks", invalidBooks);
model.addAttribute("bookList", borrowedBooks);
}
}
#SuppressWarnings("unchecked")
private List<String> getRequestedBookIds(String bookIds) {
List<String> requestedBookIds = null;
try {
requestedBookIds = new ObjectMapper().readValue(bookIds, ArrayList.class);
} catch (Exception e) {
e.printStackTrace();
}
return !CollectionUtils.isEmpty(requestedBookIds) ? requestedBookIds : new ArrayList<>();
}
and this is from the controller:
#GetMapping(value = "/available", produces = MediaType.APPLICATION_JSON_VALUE)
public String getAvailableFreeBooks(Model model) {
List<Book> availableBooks= bookService.getAllAvailaBooks();
model.addAttribute("listBooks", availableBooks);
return "available_books";
}
In your html you would probably:
<input type="checkbox" th:field="*{requestedBooks}" value="${book.getId}">
omit the id (if you don't need it).
use th:field (instead of name).
set value to the id of the current book.
In your controller: requestedBooks (#ModelAttribute("requestedBooks") List<String> requestedBooks) will (should) contain all checked book ids.
Ref: https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#checkbox-fields
A sample repository:
https://github.com/xerx593/soq67602860
Uppdate:
To process the checkboxes client-sided (jquery),
you can obtain an array of ids like:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#btnBorrow").click(function() {
var reqBookIds = new Array();
$('input[name="requestedBooks"]:checked').each(function() {
reqBookIds .push(this.value);
});
alert("Number of selected Books: "+reqBookIds .length+"\n"+"And, they are: "+reqBookIds);
// do stuff with reqBookIds ...
)};
});
</script>
With the mentioned <input type="checkbox" .../> (consider that <input/> should be inside a <form/>!!) and a button like:
<button id="btnBorrow">Borrow</button>
..the userEmail must come from client side???
Related
I have a problem with org.primefaces.component.diagram, i want to add an action when click on any overlay or connector, i make this using jquery, but the problem is that there is no identifier for the connection, after search i was able to get the ids of the 2 end points of the connection but if there is many connection between the same points then i cannot distinguish between them, i tried to override the diagram and add "connectionId" attribute on the connection but i got an exception in the front end :
Uncaught ReferenceError: connectionId590236 is not defined at eval (eval at (jquery.js.xhtml?ln=primefaces&v=5.2:14), :1:1488)
screenshot
The closet solution would be is to use setId on Element in the DefaultDiagramModel creation.
An example would be as the following:
Element elementA = new Element("A", "20em", "6em");
elementA.setId("element-a");
Element elementB = new Element("B", "10em", "18em");
elementB.setId("element-b");
Element elementC = new Element("C", "40em", "18em");
elementC.setId("element-c");
...
Since PrimeFaces doesn't provide the control you are searching for, and the original component comes from jsPlumb, you may rely on that to achieve what you are looking for.
First make sure that the <p:diagram> has a widgetVar value, es. diagramWV
An example would be the following:
$(document).ready(function () {
//timeout makes sure the component is initialized
setTimeout(function () {
for (var key in PF('diagramWV').canvas.getAllConnections()) {
if (PF('diagramWV').canvas.getAllConnections().hasOwnProperty(key)) {
//Elemenets Events
// on source just once
$(PF('diagramWV').canvas.getAllConnections()[key].source).off('click').on('click', function () {
console.log($(this).attr('id'))
});
// on target just once
$(PF('diagramWV').canvas.getAllConnections()[key].target).off('click').on('click', function () {
console.log($(this).attr('id'))
});
//Connection Event
PF('diagramWV').canvas.getAllConnections()[key].bind("click", function (conn) {
console.log("source " + conn.sourceId);
console.log("target " + conn.targetId);
});
}
}
}, 500);
});
Note: The canvas property of the widgetVar is the current instance of jsPlumbInstance
Here's an online demo, and a small working example on github.
finally i found an acceptable solution :
-> add an label overlay on the connection and set the identifier on it.
org.primefaces.model.diagram.Connection conn = new org.primefaces.model.diagram.Connection(
EndPointA, EndPointB);
LabelOverlay labelOverlay = new LabelOverlay(connection.getId(), "labelOverlayClass", 0.3);
conn.getOverlays().add(labelOverlay);
-> then add JS function to handle on dbclick action on the connection and get the id from its related overlay using the classes "._jsPlumb_overlay" and "._jsPlumb_hover"
<p:remoteCommand name="connectionClicked"
actionListener="#{yourBean.onConnectionDoubleClick}" />
<script type="text/javascript">
var connectionId;
$('._jsPlumb_connector').on('dblclick', function(e) {
$('._jsPlumb_overlay._jsPlumb_hover').each(function() {
connectionId = $(this).text();
});
connectionClicked([ { name : 'connectionId', value : connectionId } ]);
});
});
</script>
-> finally in the bean you extract the id and do whatever you want
public void onConnectionDoubleClick() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
String connectionId = params.get("connectionId");
if(StringUtils.isBlank(connectionId))
return;
.........
I was able to add a click event to Overlay by extending the primefaces Overlay class. If you make a change to the toJS() class (taking heavy inspiration from the Primefaces LabelOverLay) then you can write your own overlay with the jsplumb overlay constructor. Here's my implementation of a ClickableLabelOverlay.
public class ClickableLabelOverlay implements Overlay {
private String label;
private String styleClass;
private double location = 0.5;
private String onClick;
public ClickableLabelOverlay() {
}
public ClickableLabelOverlay(String label) {
this.label = label;
}
public ClickableLabelOverlay(String label, String styleClass, double location, String onClick) {
this(label);
this.styleClass = styleClass;
this.location = location;
this.onClick = onClick;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getStyleClass() {
return styleClass;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public double getLocation() {
return location;
}
public void setLocation(double location) {
this.location = location;
}
public String getOnClick() {
return onClick;
}
public void setOnClick(String onClick) {
this.onClick = onClick;
}
public String getType() {
return "Label";
}
public String toJS(StringBuilder sb) {
sb.append("['Label',{label:'").append(label).append("'");
if(styleClass != null) sb.append(",cssClass:'").append(styleClass).append("'");
if(location != 0.5) sb.append(",location:").append(location);
if(onClick != null) sb.append(",events:{click:function(labelOverlay, originalEvent){").append(onClick).append("}}");
sb.append("}]");
return sb.toString();
}
}
Put any javascript you want to execute inside of the onClick variable and it'll run when you click on the overlay. For convenience I added it to the set of default overlays for my diagram.
diagram.getDefaultConnectionOverlays().add(new ClickableLabelOverlay(...)
I have a problem in regarding my TempData["Something"] because I want to customize every content of the Something. Below is my code:
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveLoanContract(ModelLoan lc, string message)
{
var myList = new ModelLoan();
var countLoan = _Context.LoanContract.Count(c => c.EmployeeId == lc.loanContract.EmployeeId && c.LoanTypeId == lc.loanContract.LoanTypeId);
if (ModelState.IsValid)
{
//Some Logic
TempData["SuccessMessage"] = new { CssClassName = "alert-success", Title = "Success!", Message = "You have successfully applied. Please wait for confirmation. Thank you!" };
return RedirectToAction("Index");
}
}
View:
#if(TempData["SuccessMessage"] != null)
{
<div class="alert #TempData["SuccessMessage"].CssClassName">
<strong>#TempData["SuccessMessage"].Title</strong> #TempData["SuccessMessage"].Message
</div>
}
But I got an error on some objects CssClassName, Title, Message in TempData["SuccessMessage"] and I don't have any idea yet why. I know there is a small trick to fix this. Can someone help me with this? Thanks!
you cannot store anonymous type into TempData but you can create a class and convert tempdata to the class like this
public class ClsMsg
{
public string CssClassName { get; set; }
public string Title { get; set; }
public string Message { get; set; }
}
in action
TempData["SuccessMessage"] = new ClsMsg { CssClassName = "alert-success", Title = "Success!", Message = "You have successfully applied. Please wait for confirmation. Thank you!" };
and in view
#if (TempData["SuccessMessage"] != null)
{
ClsMsg msg = TempData["SuccessMessage"] as ClsMsg;
<div class="alert #msg.CssClassName">
<strong>#msg.Title</strong> #msg.Message
</div>
}
I am using view model to display a dropdownlist and i am also trying to get the value of the selected list, here is my view model
public class CreateJobViewModel
{
public int[] SelectedIndustriesIds { get; set; }
public IList<SelectListItem> IndustriesList { get; set; }
}
My controller
public ActionResult Create()
{
var industryList = repository.GetAllIndustries();
var model = new CreateJobViewModel
{
IndustriesList = industryList.Select(i => new SelectListItem
{
Value = i.IndustryId.ToString(),
Text = i.Name
}).ToList()
};
return View("~/Views/Dashboard/Job/Create.cshtml", model);
}
My post controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateJobViewModel model)
{
try
{
var job = new Job()
{
Title = "hi",
EmploymentHourId = 1,
LocationId = 1,
Salary = 50,
SalaryPeriodId = 1,
PostCode = 2131,
Role = "world",
Description = "hello",
IsPublished = false,
ShiftId = 1,
WorkDayId = 1,
NumberOfPosition = 5,
Meal = false,
SecondYearVisa = true,
Sponsorship = true,
Accommodation = true,
DurationId = 1,
IndustryExperiencePeriod = 5,
Id = User.Identity.GetUserId(),
};
foreach (int id in model.SelectedIndustriesIds)
{
var industry = repository.Industry(id);
job.Industries.Add(industry);
}
foreach (int id in model.SelectedSpecialRequirementsId)
{
var special = repository.SpecialRequirement(id);
job.SpecialRequirements.Add(special);
}
repository.AddJob(job);
return RedirectToAction("Create");
}
catch
{
return View("~/Views/Dashboard/Job/Create.cshtml");
}
}
Every time i try to submit the selected value, i get Object reference not set to an instance of an object Error on the following line in my view:
#model Taw.WebUI.Models.CreateJobViewModel
#Html.ListBoxFor(m => m.SelectedIndustriesIds, Model.IndustriesList) -- here i get the error
Any reason why?
When you submit the form your throwing an exception (confirmed in the comments) and in the catch block you are returning the view, which throws the exception you are seeing because Model.IndustriesList is null. You need to re-assign the value before you return the view.
Since you need to assign SelectLists in the GET method and in the POST method if you return the view, I tend to re-factor this to a separate method to keep the controller code a bit cleaner. Note the following code is based on your model property being public SelectList IndustriesList { get; set; } which is a bit simpler than building IList<SelectListItem>
private void ConfigureViewModel(CreateJobViewModel model)
{
var industryList = repository.GetAllIndustries();
model.IndustriesList = new SelectList(industryList, "IndustryId", "Name")
// any other common stuff
}
and then in the action methods
public ActionResult Create()
{
var model = new CreateJobViewModel();
ConfigureViewModel(model);
return View(model);
}
public ActionResult Create(CreateJobViewModel model)
{
try
{
....
}
catch
{
ConfigureViewModel(model);
return View(model);
}
}
Note its also good practice to test if the model is valid before attempting to save it
public ActionResult Create(CreateJobViewModel model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model); // return the view so the user can correct validation errors
}
....
I have a jsp page in which i have implemented J-Table. I have a field in jtable called ClassID which i want to make as dropdown. So how am i trying to do it as :
$('#UserTableContainer').jtable({
title : 'Table of Users',
actions : {
listAction : 'CRUDController?action=list',
fields : {
ClassID : {
title : 'ClassID',
list : true,
width : '50%',
edit : true,
option:'CRUDController?action=getClassID'
},
My Model i.e. the BEAN Class is :
private int id;
private String name;
private String classID;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassID() {
return classID;
}
public void setClassID(String classID) {
this.classID = classID;
}
When listAction : 'CRUDController?action=list' is called i get the populated data in jtable from servlet to the jsp
But when control comes to this line : option:'CRUDController?action=getClassID' , It goes to the dopost method of servlet class, searches the action=getClassID and then creates the jsonArray. But when the control comes back to the jsp page, it is unable to populate the dropdown in the JTable
My servlet code is :
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("action") != null) {
List<UserModel> lstUser = new ArrayList<UserModel>();
String action = (String) request.getParameter("action");
Gson gson = new Gson();
response.setContentType("application/json");
if (action.equals("list")) {
try {
// Fetch Data from User Table
lstUser = daoForMat.getAllUserList();
// Convert Java Object to Json
JsonElement element = gson.toJsonTree(lstUser, new TypeToken<List<UserModel>>() {
}.getType());
JsonArray jsonArray = element.getAsJsonArray();
String listData = jsonArray.toString();
// Return Json in the format required by jTable plugin
listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
response.getWriter().print(listData);
} catch (Exception ex) {
String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getMessage() + "}";
response.getWriter().print(error);
ex.printStackTrace();
}
else if(action.equals("getClassID") ){
System.out.println("I came to action getClassID");
List<String> lstClassID = new ArrayList<String>();
//Here i am able to get the List containing classID
lstClassID = GetClassList();
JsonElement element = gson.toJsonTree(lstClassID , new TypeToken<List<String>>() {
}.getType());
JsonArray jsonArray = element.getAsJsonArray();
String listData = jsonArray.toString();
// Return Json in the format required by jTable plugin
listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
response.getWriter().print(listData );
//return jsonArray;
}
}
}
What am i doing wrong? I have written the same code for action.equals("getClassID") as that for if (action.equals("list")). **For the later condition i am able to populate the Jtable but for this condition - action.equals("getClassID") i am not able to populate the dropdown in JTable.
The only difference is when action=list i am writing a list of BEAN Class to the response. ie.List<UserModel> lstUser = new ArrayList<UserModel>(); as you can see in the servlet code
and when action=getClassID i am writing a list of String to the response i.e.List<String> lstClassID = new ArrayList<String>();
What should i write in option field of JTABLE in the jsp page so that i can populate the dropdown?** Looking forward to your solutions. Thanks in advance
Hey you can try this.
declare a field whch u gonna use as dropdown like this
Location:
{
title: 'Location',
width: '12%',
list: true,
options: '/JTablePractice.aspx/GetContinentalOptions',
},
and write this in your front end
public static object GetContinentalOptions()
{
using (var db = new ASPPracticesEntities1())
try
{
var numbers = db.Members.Select(c => new { DisplayText = c.Location, Value = c.Location }).ToList();
return new { Result = "OK", Options = numbers };
}
catch (Exception ex)
{
return new { Result = "ERROR", Message = ex.Message };
}
}
Donno how to write it in java but hopw it help u
I am trying to make a simple multi-language website. Overriding FindView to get the file based on the language is easy, my problem comes in the routing part.
I would like to do is: (have a website with 2 languages - pt-br and en-us, let abbreviate to onlu pt and en - the default would be PT)
User types www.mysite.com -> would find the languages in the user header request and if the user doesn´t have any of them, he would be redirected to the default PT, so the end result would be www.mysite.com in Portuguese. If he has en, he would be redirected to www.mysite.com/en/ , the result would be the view in English.
If the user types www.mysite.com/pt/ -> I would check that he wants the default and would redirect to www.mysite.com in Portuguese.
I did a custom engine to get the correct view based on the language. But it´s not working 100% because the routing problem.
IN my case I am trying to route with
routes.MapRoute(
"Localization", // Route name
"{lang}/{controller}/{action}", // URL with parameters
new { lang = "pt", controller = "Home", action = "Index" } //defaults
);
But with this, when someone types /pt/ will not redirect to the root of the site.
Another problem with the route is that I don´t want to anyone typing the controller name, I just want the action, my website just have one Home controller with few actions.
Another problem is the that I want a different action name, like, CONTACT in English and CONTATO in Portuguese, they should appear on the address bar like www.mysite.com/Contato or www.mysite.com/en/Contact
Mu Engine I did base on this solution http://www.counity.at/blog/2012/asp-net-mvc3-localization-using-culture-dependent-views/ by Guido Breitenhuber
Here is the code with some tweaks (it´s not working 100% yet)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
namespace Ala.MultiLanguage
{
public class LocalizedViewLocation : RazorViewEngine
{
private static readonly string[] _emptyLocations = new string[0];
public string[] LocalizedViewLocationFormats { get; set; }
public string[] LocalizedMasterLocationFormats { get; set; }
protected string[] LocalizedPartialViewLocationFormats { get; set; }
public LocalizedViewLocation()
{
// Define the localized view locations
// 0: Language
// 1: View name
// 2: Controller name
LocalizedViewLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
MasterLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
LocalizedPartialViewLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (String.IsNullOrEmpty(partialViewName))
throw new ArgumentException("Parameter partialViewName is null or empty.", "partialViewName");
string[] searched;
var controllerName = controllerContext.RouteData.GetRequiredString("controller");
var partialPath = GetPath(controllerContext, LocalizedPartialViewLocationFormats, partialViewName, controllerName, out searched);
if (String.IsNullOrEmpty(partialPath))
{
var baseRes = base.FindPartialView(controllerContext, partialViewName, useCache);
if (baseRes.View != null)
return baseRes;
return new ViewEngineResult(searched.Union(baseRes.SearchedLocations));
}
return new ViewEngineResult(CreatePartialView(controllerContext, partialPath), this);
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (String.IsNullOrEmpty(viewName))
throw new ArgumentException("Parameter viewName is null or empty.", "viewName");
string[] viewLocationsSearched;
string[] masterLocationsSearched;
var controllerName = controllerContext.RouteData.GetRequiredString("controller");
var viewPath = GetPath(controllerContext, LocalizedViewLocationFormats, viewName, controllerName, out viewLocationsSearched);
var masterPath = GetPath(controllerContext, LocalizedMasterLocationFormats, masterName, controllerName, out masterLocationsSearched);
if (String.IsNullOrEmpty(viewPath) || (String.IsNullOrEmpty(masterPath) && !String.IsNullOrEmpty(masterName)))
{
var baseRes = base.FindView(controllerContext, viewName, masterName, useCache);
if (baseRes.View != null)
return baseRes;
return new ViewEngineResult(viewLocationsSearched.Union(masterLocationsSearched).Union(baseRes.SearchedLocations));
}
return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this);
}
private string GetPath(ControllerContext controllerContext, string[] locations, string name, string controllerName, out string[] searchedLocations)
{
searchedLocations = _emptyLocations;
if (String.IsNullOrEmpty(name))
return String.Empty;
if (IsSpecificPath(name))
return String.Empty;
return GetPathFromGeneralName(controllerContext, locations, name, controllerName, ref searchedLocations);
}
private static bool IsSpecificPath(string name)
{
char c = name[0];
return (c == '~' || c == '/');
}
private string GetPathFromGeneralName(ControllerContext controllerContext, string[] locations, string name, string controllerName, ref string[] searchedLocations)
{
var result = String.Empty;
searchedLocations = new string[locations.Length];
for (int i = 0; i < locations.Length; i++)
{
var location = locations[i];
var virtualPath = string.Format(CultureInfo.InvariantCulture, location, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, name, controllerName);
if (FileExists(controllerContext, virtualPath))
{
searchedLocations = _emptyLocations;
result = virtualPath;
break;
}
searchedLocations[i] = virtualPath;
}
return result;
}
}
}
Well, after some timing trying to do in the best way, I ´ve found one solution much easier and fast to implement.
Just mapped the routes that I want one-by-one in the global.asax. This will work and will be fast to implement if you have only few pages and languages.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default-BR", "", new { controller = "Home", action = "Index" });
routes.MapRoute("Default-EN", "en", new { controller = "Home", action = "IndexEN" });
routes.MapRoute("HotelBR", "Hotel", new { controller = "Home", action = "Index" });
routes.MapRoute("HotelEN", "en/Hotel", new { controller = "Home", action = "IndexEN" });
routes.MapRoute("Apartamento", "Apartamento", new { controller = "Home", action = "Apartamentos" });
routes.MapRoute("Apartamentos", "Apartamentos", new { controller = "Home", action = "Apartamentos" });
routes.MapRoute("Apartments", "en/Apartments", new { controller = "Home", action = "ApartamentosEN" });
routes.MapRoute("Localizacao", "Localizacao", new { controller = "Home", action = "Localizacao" });
routes.MapRoute("Location", "en/Location", new { controller = "Home", action = "LocalizacaoEN" });
routes.MapRoute("Tarifa", "Tarifa", new { controller = "Home", action = "Tarifas" });
routes.MapRoute("Tarifas", "Tarifas", new { controller = "Home", action = "Tarifas" });
routes.MapRoute("Rates", "en/Rates", new { controller = "Home", action = "TarifasEN" });
routes.MapRoute("Reserva", "Reserva", new { controller = "Home", action = "Reservas" });
routes.MapRoute("Reservas", "Reservas", new { controller = "Home", action = "Reservas" });
routes.MapRoute("Booking", "en/Booking", new { controller = "Home", action = "ReservasEN" });
routes.MapRoute("Contato", "Contato", new { controller = "Home", action = "Contato" });
routes.MapRoute("Contact", "en/Contact", new { controller = "Home", action = "ContatoEN" });