Dynamic select list in a Liferay MVCPortlet - drop-down-menu

You can create a select list with static options in Liferay MVCPortlet JSP page like this:
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:form>
<aui:select name="items">
<aui:option value="item1">Item1</aui:option>
<aui:option value="item2">Item2</aui:option>
</aui:select>
</aui:form>
What is the recommended way of creating the options dynamically for a list of objects stored in portlet session?

Use a foreach tag:
https://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:form>
<aui:select name="items">
<c:forEach items="<%=yourList%>" var="yourlistItem">
<aui:option value="${yourlistItem.value}">${yourlistItem.name}</aui:option>
</c:forEach>
</aui:select>
</aui:form>

Related

JSTL: c:if Not Found in Build Path

I recently migrated an ANT project to Maven. After doing all the dependecnies and getting the project error free. I see this warning on in Eclipse:
The tag handler class for "c:if" (org.apache.taglibs.standard.tag.rt.core.IfTag) was not found on the Java Build Path
I see the same warnings for c:import, c:out c:set as well.
I do have the tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
added in the JSP. And I have the JSTL 1.2 dependency added in the pom file. Can you tell me how to get rid of these warnings?
I had the same error. To fix it, I just added and saved the following in my pom.xml.
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
In some older Eclipse versions, you may need to modify the "<c:if..." line of code, remove a character or more, save it then put it back as it was and save the file again. This in some case get rid of the warning.
My code used <c:set and <c:if as the following example. No warning displays. Perhaps, you may need to update your Eclipse. My Eclipse is IDE for Enterprise Java Developers Version: 2019-09 R (4.13.0).
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# attribute name="normalPrice" fragment="true" %>
<%# attribute name="onSale" fragment="true" %>
<%# variable name-given="name" %>
<%# variable name-given="price" %>
<%# variable name-given="origPrice" %>
<%# variable name-given="salePrice" %>
<head>
<title>Tag Example</title>
</head>
<table border="1">
<tr>
<td>
<c:set var="name" value="Hand-held Color PDA"/>
<c:set var="price" value="$298.86"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="4-Pack 150 Watt Light Bulbs"/>
<c:set var="origPrice" value="$2.98"/>
<c:set var="salePrice" value="$2.32"/>
<jsp:invoke fragment="onSale"/>
</td>
<td>
<c:set var="name" value="Digital Cellular Phone"/>
<c:set var="price" value="$68.74"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Baby Grand Piano"/>
<c:set var="price" value="$10,800.00"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Luxury Car w/ Leather Seats"/>
<c:set var="origPrice" value="$23,980.00"/>
<c:set var="salePrice" value="$21,070.00"/>
<jsp:invoke fragment="onSale"/>
</td>
</tr>
</table>
<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
If the tag works actually fine, it is just a false negative from Eclipse. In that case you could force a new check editing the tag name and then setting it back to the right name.
So, for instance, click on your "c:if" tag, remove a single character and then re-enter it (exactly as it was before). Save it. In my case that was enough to get rid of the warning.

Spring MVC + DisplayTag + Checkbox

I have to integrate in a SpringMVC form a set of fields and a List handled by a Display:table .
In the display table I have to view a column of checkboxes where the information about if this is checked or not is passed by the controller. I have to manipulate this checkboxes and pass them to another controller to store that data inside a DB.
I'm simulating this situation creating a SpringMVC controller that set me inside the Model some data :
package it.test.displaytag.controller;
import java.util.ArrayList;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import it.test.displaytag.model.Bean;
import it.test.displaytag.model.Interno;
#org.springframework.stereotype.Controller
public class Controller {
#RequestMapping(value = "index.htm")
public String home(Model model) {
Bean bean = new Bean();
bean.setCognome("Cognome");
bean.setNome("Nome");
ArrayList<Interno> list = new ArrayList<Interno>();
for(int i=0;i<50;i++) {
Interno asd = new Interno();
asd.setIdCheck(i);
if (i%2==0) {
asd.setIsEnabled(Boolean.TRUE);
}
list.add(asd);
}
bean.setInterno(list);
model.addAttribute("displayTagForm", bean);
return "index";
}
}
The JSP is :
<%# taglib prefix="display" uri="http://displaytag.sf.net" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
org.displaytag.decorator.CheckboxTableDecorator decorator = new org.displaytag.decorator.CheckboxTableDecorator();
decorator.setId("idCheck");
decorator.setFieldName("_chk");
pageContext.setAttribute("checkboxDecorator", decorator);
%>
<html>
<body>
<form:form name="displayTagForm" action="/salva" modelAttribute="displayTagForm" method="POST">
Form di test per testare la paginazione nelle Displaytag.
<br><br>
Nome : <form:input path="nome" />
<br><form:input path = "cognome" />
<br><br>
Tabella
<br><center>
<display:table name="displayTagForm.interno" uid="bean" decorator = "checkboxDecorator"
pagesize="10" >
<display:column property="idCheck" />
<display:column property = "checkbox" />
</display:table>
</center>
</form:form>
</body>
</html>
The JSP is correctly showing the informations inside the fields "nome" and "cognome" but is not showing if the checkbox is selected or not ( passed with a flag isEnabled in the bean ) . I've not understood how to do this trick.
After that, i Have to handle the pagination and the sort of the display:table, because I think that if I write something in that 2 textboxes nome and cognome and I click one of the link to go to another page, I lose the informations that i've written in the textboxes and I lose the information about the value of the checkbox clicked or not.
How can I handle this situation ?

Type [java.lang.String] is not valid for option items upon migrating from Spring 3.0.6 to 3.2.3

I am working on migrating a dynamic web project from Spring 3.0.6 to 3.2.3. Prior to this migration, we had no issue with our dropdowns. However, after migrating, we get the following error:
Exception created : com.ibm.websphere.servlet.error.ServletErrorReport: javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
I've removed all the code to isolate the issue, so below is the relevant code. Please let me know if any further information is needed. The thing that puzzles me is that the List isn't even String based. I realize that the JSP will treat the values as String for the options, but my understanding is that there is a built-in PropertyEditor that would do this translation.
Controller:
#RequestMapping("/reports-menu.html")
public String showReportsHome(#ModelAttribute("reportForm")ReportForm reportForm, Model model, HttpSession session, HttpServletResponse response, HttpServletRequest request) {
List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
model.addAttribute("intList", intList);
return "reports-home-int";
}
JSP:
<%# taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%# taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%# taglib uri="/WEB-INF/tld/sbp.tld" prefix="sbp" %>
<form:form name="report_form" method="POST" modelAttribute="reportForm" action="reports-menu.html" id="report_form">
<form:hidden path="download" id="form_download"/>
<form:hidden path="sortDirection" />
<form:hidden path="sortBy"/>
<input type="hidden" name="reset"/>
<div align="left">
<table border="0">
<tr>
<td><b>Mailer Name</b></td>
<td>
<form:select path="mailerCond">
<form:options items="${intList}" />
</form:select>
</td>
</tr>
</table>
</div>
</form:form>

JSTL tags don't evaluate if I import

I use dynamic include on a page:
<div class="top">
<jsp:include page="Header.jsp"/>
</div>
This is the important part in the Header.jsp:
<H4>
JSTL TAG Test: </br>
<c:if test="${sessionScope.username != null}" >
<c:out value="Hello, ${sessionScope.username}"/>
</c:if>
</h4>
The result on my main page source is:
<H4>
JSTL TAG Test: </br>
<c:if test=Swank != null >
<c:out value="Hello, Swank"/>
</c:if>
</h4>
I use this: <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> in the main page.
Does anybody know how can I use JSTL if I import it from another page?
Thanks, Zoltán
Add the same declaration
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
into your Header.jsp file.
I agree with the given answer, just with a slight modification:
<#include file="Header.jsp" %>
Notice the '%' to close the directive.
You can solve the problem by using JSP include directive
<%# include file="Header.jsp" %>
Because the content of the file given in the include directive is pasted as it is, in the place where the JSP include directive is used.
But in include action <jsp:include> At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page.
Updated:
In JSTL condition should be written inside double quotes.
<c:if test="${sessionScope.username} != null"></c:if>
Add double quotes in your condition.

Specifying the width of Spring 3 <form:select>

How do I specify the with of form:select tag in Spring 3?
I have options list that contains single characters, and by default it is too wide for them.
I have tried 'style="width:5px"' without any change.
form select {
border:1px solid #006;
background:#FFFFA0;
width:410px;
}
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
...
<form:select path="data" style="min-width:400px;"></form:select>
min-width worked for me

Resources