EDIT:
I want to show a spring-mvc dropdown box
out of a object list which is a property from another class.
I did this after searching:
<sf:form action="${pageContext.request.contextPath}/venta/save"
method="post" commandName="venta">
<table>
<tr>
<td>Número de factura</td>
<td><sf:input path="numero_factura" type="text" /></td>
<td><sf:errors path="numero_factura" cssclass="error" /></td>
</tr>
<tr>
<td>Producto:</td>
<td><sf:select path="${producto.nombreProducto}">
<sf:option value="" label="...." />
<sf:options items="${productos}" />
</sf:select></td>
<td><sf:errors path="${producto.nombreProducto}"
cssclass="error" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Guardar cambios"></td>
</tr>
<tr>
</table>
</sf:form>
The class I'm mapping is this:
Venta.class
@Entity
public class Venta implements Serializable {
@Id
@NotNull
@Digits(integer=12, fraction = 0)
private Integer numero_factura;
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany
@NotEmpty
private List<Producto> productos = new ArrayList<Producto>();
private Date fechaDeIngreso;
@ManyToOne(cascade = CascadeType.ALL)
private Empleado empleado;
public Venta() {
}
...
}
And this is the controller:
@Controller
public class VentaController {
@Autowired
public IServiceProducto serviceProducto;
@Autowired
public IServiceVenta serviceVenta;
@Autowired
public IServiceEmpleado serviceEmpleado;
@RequestMapping("/venta")
public String showVenta(Model model, HttpSession session) {
// Model es una interfaz que nos permite definir atributos en el modelo
init(model);
return "venta";
}
@RequestMapping(value = "/venta/save", method = RequestMethod.POST)
public String handleVenta(@Valid @ModelAttribute("venta") Venta ventaForm, BindingResult result, Model model,
HttpSession session, RedirectAttributes ra) {
try {
if (result.hasErrors()) {
model.addAttribute("productos", serviceProducto.getAll());
return "venta";
}
Empleado empleado = (Empleado) session.getAttribute("empleado");
empleado.setVenta(ventaForm);
serviceVenta.exist(ventaForm);
serviceEmpleado.addChild(empleado, ventaForm);
ra.addFlashAttribute("resultado", "La venta fué agregada exitosamente");
return "redirect:/venta";
} catch (ServicioException e) {
ra.addFlashAttribute("resultado", e.getMessage());
return "redirect:/venta";
}
}
public void init(Model model){
Venta venta = new Venta();
Producto producto = new Producto();
model.addAttribute("venta", venta);
model.addAttribute("producto", producto);
model.addAttribute("productos", serviceProducto.getAll());
}
}
It gives me the view I was looking for, but this code is doing nothing when it comes to select an item from the dropdown box and hitting the submit button. It gives me no error and stay static in http://localhost:8585/electronicaDonPepe/venta/save
Please please help me, I'm almost there !
OLD VERSION:
I want to show a combobox
in jsp
from a list. It gives me a dropdownlist
which I don't want.
I made a map with the list and it gives me the same result.
Venta.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<sf:form action="${pageContext.request.contextPath}/venta/save"
<td>Producto</td>
<td>
<sf:select path="productos">
<sf:option label="---select---" value="NONE"/>
<sf:options items="${productos}"/>
</sf:select>
</td>
<td><sf:errors path="productos" cssClass="error"/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Guardar cambios"></td>
</tr>
</table>
</sf:form>
<c:out value="${resultado}"></c:out>
</body>
</html>
Necessary VentaController.java code to understand
@Controller
public class VentaController {
@Autowired
public IServiceProducto serviceProducto;
@Autowired
public IServiceVenta serviceVenta;
@Autowired
public IServiceEmpleado serviceEmpleado;
@RequestMapping("/venta")
public String showVenta(Model model, HttpSession session) {
// Model es una interfaz que nos permite definir atributos en el modelo
Venta venta = new Venta();
model.addAttribute("venta", venta);
List<Producto> listaProductos = serviceProducto.getAll();
Map<String, String> mapaProductos = new LinkedHashMap<String, String>();
for (Producto producto : listaProductos) {
mapaProductos.put(producto.getNombreProducto(), producto.getNombreProducto());
}
model.addAttribute("productos", mapaProductos);
return "venta";
}
The result is a list with multiple selections. I'm aware that if I use the property multiple=false the multiple selection will be off but it still work in my case.
DropdownList with multiple selection
Still, I want a combobox function and not a dropdownlist without multiple selection.
This is my idea of what I want: