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

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>

Related

How to get and display data on HTML when using Spring Json

I'm using Spring to get json data, and then i want to display data on HTML page.
This is my class:
#RequestMapping(value = "/selectCountNotification.do")
public ResponseEntity<List<EgovMap>> selectCountNotification(#RequestParam Map<String, Object> params, ModelMap model) throws Exception {
System.out.println("########################################");
System.out.println("####################check log####################");
System.out.println("########################################");
List<EgovMap> countNotification = orderDetailService.selectCountNotification(params);
model.addAttribute("countNotification", countNotification);
return ResponseEntity.ok(countNotification);
}
This is my header.jsp
<li>
<div class="notification" style="top: -5px;left: -40px;height: 36px;">
<i class='far fa-bell' style='font-size:20px'>
<div id="not">
<%# include file="/WEB-INF/jsp/sales/order/countNotify.jsp"%>
</div>
</i>
</div>
</li>
this is my countNotify.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<p>data: </p>
<c:forEach items="${countNotification}" var="comm">
<p><c:out value="${comm.countNotify}"/></p>
</c:forEach>
when i call by url:
http://localhost:8080/sales/order/selectCountNotification.do
I get data value from my database following as:
12
but when i run my jsp is countNotify.jsp, it cannot get any value, So how i can fix the problem ?

Dynamic select list in a Liferay MVCPortlet

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>

Why do am I getting error "java.lang.IllegalStateException" after putting <form:form> tag in jsp file of spring?

I have 2 tables, city and hotel_details in my database. I am trying to fetch the data from these tables and populating inside a form for registering the customer. But I am getting "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute" as error.
JSP file
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<head>
<title>Search Hotels</title>
</head>
<body>
<h4>Search Hotels</h4>
<form:form action="search">
<table>
<tr>
<td>City:</td>
<td>
<form:select path="cities">
<form:options items="${cities}" />
</form:select>
</td>
</tr>
<tr>
<td>Hotel:</td>
<td>
<form:select path="hotels">
<form:options items="${hotels}" />
</form:select>
</td>
</tr>
<tr>
<td>Date:</td>
<td>
<input type="date" id="date" name="date">
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="Check Availability">
</td>
</tr>
</table>
</form:form>
Controller
#Controller
public class HomeController {
//need a controller method to show the initial HTML form
#Autowired(required=true)
private CityDAO cityDAO;
#Autowired(required=true)
private HotelDetailsDAO hotelDetailsDAO;
#RequestMapping("/")
public String showCheckAvailablityForm(Model theModel) {
// get customers from the dao
//List<City> theCities = cityDAO.getCities();
List<String> theCities = cityDAO.getCities();
Set<String> theHotels = hotelDetailsDAO.getHotels();
// add the customers to the model
theModel.addAttribute("cities", theCities);
theModel.addAttribute("hotels", theHotels);
//printing the data fetched
System.out.println("In HomeController showCheckAvailability method where city name is being fetched from city table");
theCities.forEach((n) -> System.out.println(n));
System.out.println("printing hotels");
for (String temp : theHotels) {
System.out.print(temp + " ");
}
return "checkAvailability-form";
}
#RequestMapping("/search")
public String searchResult(#RequestParam("cityName") String theCityName, #RequestParam("hotelName") String theHotelName,Model model) {
System.out.println("processed successfully");
return null;
}
}
When you use <form:form> attribute, it requires you to specify model object that should be bound to form tag. If you don't specify any model attribute default name is used as command.
Following is the description of form:form tag from spring-form.tld -
<attribute>
<description>Name of the model attribute under which the form object is exposed.
Defaults to 'command'.</description>
<name>modelAttribute</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Name of the model attribute under which the form object is exposed.
Defaults to 'command'.</description>
<name>commandName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
As you don't have any model object bound to form, try removing form:form tag and use HTML form tag and also make sure you match input parameter names with method parameter names. i.e -
<form action="search">
...
</form>

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 ?

Resources