I'm using enum for select options in thymeleaf and I can't see them or insert them in the database. There is nothing in the dropdown list.
<div class="form-group">
<label for="roomType">Rooms</label>
<select class="form-select selectpicker show-tick" th:field="*{property.roomType}" id="rooms">
<option value="">Nothing selected</option>
<option th:each="property.roomType : ${T(com.realestate.petfriendly.entity.RoomType).values()}"
th:value="${property.roomType}"
th:text="${property.roomType}">
</option>
</select>
</div>
class
#Enumerated(EnumType.STRING)
#Column(name = "RoomType")
private RoomType roomType;
and enum
public enum RoomType {
GARSONJERA("garsonjera"), JEDNOIPOSOBAN("jednoiposoban"), DVOSOBAN("dvosoban"),
DVOIPOSOBAN("dvoiposoban"), TROSOBAN("trosoban"), TROIPOSOBAN("troiposoban"),
CERVOROSOBAN("cetvorosoban"), CETVOROIPOSOBAN("cetvoroiposoban"), VISESOBAN("visesoban");
private String roomType;
private RoomType(String roomType) {
this.roomType = roomType;
}
public String getRoomType() {
return this.roomType;
}
}
I'm not sure why it doesn't show me anything
that is available for me.
class
#Setter
#Getter
public class Demo {
private RoomType roomType;
}
enum
public enum RoomType {
GARSONJERA("garsonjera"), JEDNOIPOSOBAN("jednoiposoban"), DVOSOBAN("dvosoban"),
DVOIPOSOBAN("dvoiposoban"), TROSOBAN("trosoban"), TROIPOSOBAN("troiposoban"),
CERVOROSOBAN("cetvorosoban"), CETVOROIPOSOBAN("cetvoroiposoban"), VISESOBAN("visesoban");
private final String roomType;
private RoomType(String roomType) {
this.roomType = roomType;
}
public String getRoomType() {
return this.roomType;
}
}
controller:
#Controller
#RequestMapping( "test")
public class TestController {
#GetMapping("demo")
public String demo(Model model){
Demo demo = new Demo();
demo.setRoomType(RoomType.CERVOROSOBAN);
model.addAttribute(demo);
return "test/demo";
}
}
HTML:
<div class="form-group">
<label for="rooms">Rooms</label>
<select class="form-select selectpicker show-tick" th:field="*{demo.roomType}" id="rooms">
<option value="">Nothing selected</option>
<option th:each="value : ${T(com.bluray.boot.business.test.RoomType).values()}"
th:value="${value}"
th:text="${value}">
</option>
</select>
</div>
I use spring-boot 2.3.6.release
Related
It's my first topic at this beautiful site;)
I cant fix this, I am trying to present names of states and products in drop down lists. Intelij is helping me by underlines these fields.
https://ibb.co/wKgV940 (cant add image)
When I am adding th:object to divs or form it doesn't help.
There are racords on my db, and mathod retrieves values properly.
<div class="row justify-content-center">
<div class="form-group col-md-8" >
<label for="firstName" class="col-form-label">Choose State</label>
<select class="form-control" th:field="*{state.id}" id="state">
<options items="${listStates}>"></options>
<option th:each="state : ${states}"
th:value="${state.id}"
th:utext="${state.stateName}"></option>
</select>
</div>
<div class="form-group col-md-8" >
<label for="firstName" class="col-form-label">Choose products</label>
<select class="form-control" th:field="*{product.id}" id="product">
<option th:each="product : ${products}"
th:value="${product.id}"
th:utext="${product.productName}"></option>
</select>
</div>
<div class="form-group col-md-8">
<label for="firstName" class="col-form-label">Start price</label>
<input id="firstName" type="number" value="1" min="0" max="1000" step="1"/>
</div>
<div class="form-group col-md-8">
<label for="firstName" class="col-form-label">Preferred final prize</label>
<input type="text" class="form-control"
id="lastName" placeholder=""/>
</div>
<div class="form-group col-md-8">
<label for="firstName" class="col-form-label">Logistic costs</label>
<input type="text" class="form-control"
id="email" placeholder=""/>
</div>
<div class="col-md-6">
<input type="submit" class="btn btn-primary" value=" Calculate ">
</div>
</div>
</form>
public class CalculateController {
#Autowired
private StateRepository stateService;
#Autowired
private ProductRepository productService;
#RequestMapping(value = { "/calculateForm" }, method = RequestMethod.GET)
public String selectState(Model model) {
List<Product> product = new ArrayList<>();
List<State> state = new ArrayList<>();
model.addAttribute("product", product);
model.addAttribute("state", state);
List<Product> products = productService.findAll();
List<State> states = stateService.findAll();
model.addAttribute("states", states);
model.addAttribute("products", products);
return "calculateForm";
}
}
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String productName;
private String category;
private Double wholePrice;
public Product() {
}
public Product(String productName, String category, Double wholePrice) {
this.productName = productName;
this.category = category;
this.wholePrice = wholePrice;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Double getWholePrice() {
return wholePrice;
}
public void setWholePrice(Double wholePrice) {
this.wholePrice = wholePrice;
}
}
I have replaced this:
List<Product> product = new ArrayList<>();
List<State> state = new ArrayList<>();
model.addAttribute("product", product);
model.addAttribute("state", state);
by this:
Product product = new Product();
State state = new State();
model.addAttribute("product", product);
model.addAttribute("state", state);```
now it works. Thank you.
In spring based web app using hibernate I have a lists.html file where are select options codes reused in many views, now options will be transferred to database and my task is to populate those lists from db. This code will be rarely updated.This is example of list and its usage.
lists.html
<select th:fragment="typeList">
<option selected value=""></option>
<option th:value="|1|" th:text="|Type #1|"></option>
<option th:value="|2|" th:text="|Type #2|"></option>
<option th:value="|3|" th:text="|Type #3|"></option>
<option th:value="|4|" th:text="|Type #4|"></option>
</select>
usage
<li>
<label for="type" th:text="#{type.label}">type</label>
<select th:field="*{type}" th:include="html/fragments/lists :: typeList"></select>
</li>
What is the best method to build this lists.html from database dictionary?
My DTO
#Entity
#Table(name = "TableName")
public class Test {
#Id
private String Code;
private String Name;
private int price;
public Test() {}
public Test(String Code, String Name, int price) {
this.Code = Code;
this.Name = Name;
this.price = price;
}
public String getCode() {
return Code;
}
public String getName() {
return Name;
}
public int getPrice() {
return price;
}
}
View
List<Test> test = new ArrayList<>();
model.addAttribute("test", test);
List<Test> tests = testRepository.findAll();
model.addAttribute("tests", tests);
HTML
<select class="form-control" id="Order" name="Order">
<option value="">Select Test Order</option>
<option th:each="test : ${tests}"
th:value="${test.Code}"
th:text="${test.Code}+' : '+${test.Name}"></option>
</select>
</div>
Reference
Reference link
My question is How can I use my getLabel, getX, getY from my NodeEntity class and display the results of my query on my web page instead of getting "org.neo4j.ogm.response.model". I do not know if these get methods should be used within the controller or repository class in order to display the node information on the web page table.
#NodeEntity
public class Room {
#Id #GeneratedValue
private Long id;
#Property(name = "label")
private String label;
#Property(name = "shape")
private String shape;
#Property(name = "colour")
private String colour;
#Property(name = "x")
private String x;
#Property(name = "y")
private String y;
#Relationship(type = "To")
private Collection<Room> roomTo = new ArrayList<>();
#Relationship(type = "Up")
private Collection<Room> roomUp = new ArrayList<>();
#Relationship(type = "Down")
private Collection<Room> roomDown = new ArrayList<>();
#JsonIgnoreProperties("room")
#Relationship(type = "To", direction = Relationship.INCOMING)
Collection<RelEnt> relEnts;
public Room(){
}
public String getLabel(){
return label;
}
public String getShape(){
return shape;
}
public String getColour(){
return colour;
}
public String getX() {
return x;
}
public String getY() {
return y;
}
}
Repository:
public interface NavigatorRepository extends Neo4jRepository<Room, String>{
#Query("match (r1: Room {label: {from}}), (r2: Room {label: {to}}), p = shortestPath((r1)-[*]-(r2)) unwind nodes(p) as room return room")
Iterable<Map<String, Object>> getShortestRoute(#Param("from") String from, #Param("to") String to);
}
Service:
#Service
public class NavigatorService {
#Autowired
NavigatorRepository navigatorRepository;
public Iterable<Map<String, Object>> shortestRoute(String from, String to){
return navigatorRepository.getShortestRoute(from, to);
}
}
Controller:
#Controller
public class AunController {
#RequestMapping(value = "/AUN") //this will allow you to access web page with just http://localhost:8081/
public String homePage(){
return "AUN-Web"; //uses the static html file created for the web page
}
#Autowired
NavigatorService navigatorService;
#GetMapping(value = "/navigate")
public String navigate(#RequestParam(name="from") String from, #RequestParam(name="to") String to, Model model) {
Iterable<Map<String, Object>> route = navigatorService.shortestRoute(from, to);
model.addAttribute("from", from)
.addAttribute("route", route)
.addAttribute("to", to);
return "AUN-Results";
}
#GetMapping(value = "/")
public String error(){
return "Error/404";
}
}
HTML:
<form action="/navigate" method="">
<strong><label for="ifrom">Current Location:</label></strong> <br>
<input id="ifrom" type="text" placeholder="MB" name="from"><br>
<strong><label for="ito">Destination:</label></strong> <br>
<input id="ito" type="text" placeholder="MB" name="to"><br>
<p style="line-height: 30px; width: 300px;"></p>
<button type="button" class="btn btn-success" onclick="result">Search</button>
<p style="line-height: 30px; width: 300px;"></p>
</form>
</div>
<div align="center" class="container-fluid">
<h5>Going from <span th:text="${from}">MB220</span> to <span th:text="${to}">MB265</span></h5>
<table class="table">
<tr>
<th>Label</th>
<th>X</th>
<th>Y</th>
</tr>
<tr th:each="route : ${route}">
<td th:text="${route}">Mb220</td>
</tr>
</table>
</div>
Try to change your foreach var name to something like "routpoint" so it differs to the complete route.
I have this exception andd i can't find a solution
COntroller :
#RequestMapping(value="/sujet")
public String detail(Model model, HttpServletRequest request, Long idSujet) {
Utilisateur user = (Utilisateur) request.getSession().getAttribute("user");
model.addAttribute("nbrMails", metierUtilisateur.listDesEmailsRecuNonLu(user.getIdUtilisateur()).size());
SujetForum sujet = metierSujetForum.findById(idSujet);
sujet.setMessagesForums(metierSujetForum.getListMessageForum(idSujet));
model.addAttribute("sujet", sujet);
model.addAttribute("messages", metierSujetForum.getListMessageForum(idSujet));
return "/coordinateur/detailSujetForum";
}
this is my Bean definition :
i defined all the getters and setters for all attributes but is till get the same exception
#Entity
public class MessagesForum implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idMessage;
private String message;
private Date dateDepotMessage;
private boolean messageCorrecte;
#ManyToOne
#JoinColumn(name = "idSujet")
private SujetForum sujetForum;
#ManyToOne
#JoinColumn(name = "idUtilisateur")
private Utilisateur utilisateur;
#OneToMany(mappedBy = "messageForum")
private Collection<PieceJointeForum> pieceJointeForums;
public MessagesForum(String message, Date dateDepotMessage, boolean messageCorrecte) {
super();
this.message = message;
this.dateDepotMessage = dateDepotMessage;
this.messageCorrecte = messageCorrecte;
}
public MessagesForum() {
super();
}
public Long getIdMessage() {
return idMessage;
}
public void setIdMessage(Long idMessage) {
this.idMessage = idMessage;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getDateDepotMessage() {
return dateDepotMessage;
}
public void setDateDepotMessage(Date dateDepotMessage) {
this.dateDepotMessage = dateDepotMessage;
}
public boolean isMessageCorrecte() {
return messageCorrecte;
}
public void setMessageCorrecte(boolean messageCorrecte) {
this.messageCorrecte = messageCorrecte;
}
public SujetForum getSujetForum() {
return sujetForum;
}
public void setSujetForum(SujetForum sujetForum) {
this.sujetForum = sujetForum;
}
public Utilisateur getUtilisateur() {
return utilisateur;
}
public void setUtilisateur(Utilisateur utilisateur) {
this.utilisateur = utilisateur;
}
public Collection<PieceJointeForum> getPieceJointeForums() {
return pieceJointeForums;
}
public void setPieceJointeForums(Collection<PieceJointeForum> pieceJointeForums) {
this.pieceJointeForums = pieceJointeForums;
}
}
this is the output of the exception
615: <div class="media-body">
616: <div class="media-text">
617: <h5 class="semibold mt0 mb5 text-accent"></h5>
618: <p class="mb5">${msg.getIdMessage() }.</p>
619: <!-- meta icon -->
620: <p class="mb0">
621: <span class="media-meta"></span> <span class="mr5 ml5 text-muted">*</span> <a href="javascript:void(0);" class="media-meta text-default" data-t
oggle="tooltip" title="" data-original-title="Reply"><i class="ico-reply"></i></a>
Stacktrace:] with root cause
javax.el.MethodNotFoundException: Method not found: class java.lang.String.getIdMessage()
at javax.el.Util.findWrapper(Util.java:352)
at javax.el.Util.findMethod(Util.java:214)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:174)
at org.apache.jasper.el.JasperELResolver.invoke(JasperELResolver.java:139)
at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
at org.apache.jsp.WEB_002dINF.views.coordinateur.detailSujetForum_jsp._jspx_meth_c_005fforEach_005f0(detailSujetForum_jsp.java:1242)
at org.apache.jsp.WEB_002dINF.views.coordinateur.detailSujetForum_jsp._jspService(detailSujetForum_jsp.java:832)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
<c:forEach items="=${messages }" var="msg">
<div class="media-list media-list-bubble">
<div class="media">
<a href="javascript:void(0);" class="media-object pull-left"> <img src="" class="img-circle" alt="">
</a>
<div class="media-body">
<div class="media-text">
<h5 class="semibold mt0 mb5 text-accent"></h5>
<p class="mb5">${msg.idMessage }.</p>
<!-- meta icon -->
<p class="mb0">
<span class="media-meta"></span> <span class="mr5 ml5 text-muted">*</span> <i class="ico-reply"></i>
</p>
<!--/ meta icon -->
</div>
</div>
</div>
</div>
</c:forEach>
msg in JSP here seems to be coming as String and You are expecting your bean Class type .
After you get the bean also
${msg.getIdMessage() }.
This seems to be issue in your JSP. just try with
${msg.idMessage() }.
send me your jsp and don't call with get method u should call directly with property values. like amit.rk3 said. and where you are using this msg in your jsp
use like this
<c:forEach var="msg" items="${messages}">
UPDATE:
This is my index.scala.html which displays a dropdown box:
#(helloForm: Form[Hello], dpts: List[Hello])
#import helper._
#main("InteractionService", "newsimulation") {
#form(action = routes.Application.sayHello(), args = 'id -> "helloform") {
<select class="selectpicker" data-size="auto" data-live-search="true" data-container="body" >
#for(dpt <- dpts){
<option value="#dpt.id">#dpt.name</option>
}
</select>
<p class="buttons">
<input type="submit">
<p>
}
}
This is my Hello object:
#Entity
public class Hello extends Model {
private static final long serialVersionUID = 1L;
#Id
#Constraints.Required
#Formats.NonEmpty
public Long id;
#Constraints.Required
public String name;
}
This is my controller:
public static Result sayHello() {
Form<Hello> dptForm = form(Hello.class).bindFromRequest();
DomainPracticeTemplate currentDPT = dptForm.get();
Logger.info("dptForm content = " + dptForm.toString());
}
When hitting the submit button, it returns:
[IllegalStateException: No value]
I need the id or the whole object, how?