2016-12-04 11 views
0

EDIT:リストからコンボボックスや地図コレクション

私は別のクラスからプロパティでオブジェクトリストのうち、spring-mvc dropdown boxを示したいと思います。

は、私が検索した後にこれをしなかった:

<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> 

私はマッピングだクラスをこのです:

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() { 

} 
... 

}

そして、これはtです彼は、コントローラ:

@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()); 
} 

}

それは私に私が探していたビューを提供しますが、このコードは、それがドロップダウンボックスから項目を選択するために来るとき何もしないとボタンを提出当たっています。それは私にエラーを与えずに静的に滞在するhttp://localhost:8585/electronicaDonPepe/venta/save

私はほとんどそこに助けてください!


OLD VERSION:

私はリストからjspcomboboxを示したいと思います。それは私に望ましくないdropdownlistを与えます。 私はリストを使って地図を作ったので、同じ結果が得られました。

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> 

@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"; 
} 

結果が複数選択でリストであるを理解する必要がVentaController.javaコード。 multiple = falseのプロパティを使用すると、複数の選択肢はオフになりますが、それでも私の場合は機能することは承知しています。

DropdownList with multiple selection

それでも、私は複数選択することなく、コンボボックス機能ではなくドロップダウンリストが欲しいです。

これは私が何をしたいの私の考えです:

select with options

答えて

0

私はこのアプローチを持っている:

<sf:form action="${pageContext.request.contextPath}/venta/save" 
    method="post" commandName="ventaDTO"> 
    <table> 
     <tr> 
      <td>Número de factura</td> 
      <td><sf:input path="numero_factura" type="text" /> 
      <td><sf:errors path="numero_factura" cssclass="error" /></td> 
     </tr> 
     <tr> 
      <td>Producto:</td> 
      <td><sf:select path="nombreProducto"> 
        <sf:option value="" label="...." /> 
        <sf:options items="${productos}" /> 
       </sf:select> <sf:errors path="nombreProducto" cssclass="error" 
        cssStyle="color: #ff0000;" /></td> 
      <td> 
      <td><sf:input path="cantidadProductos" type="text" /> 
       <sf:errors 
        path="cantidadProductos" cssclass="error" 
        cssStyle="color: #ff0000;" /></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td><input type="submit" value="Guardar cambios"></td> 
     </tr> 
    </table> 
</sf:form> 

が、まだ特定の製品を追加するために、別の全体tableを追加することはできません。量で。私はangularjsを使用することを考えていましたが、私はその技術に慣れていません。私はあなたの助けに非常に感謝しています。

関連する問題