英文:
A spring request give me only null
问题
问题是,在使用Spring和Thymeleaf进行基于Web的课程作业来管理仓库时,遇到了一个问题。问题是,当在Thymeleaf页面上放置一个带有动态选择的表单时,尝试检索信息只会连续给我返回null。我希望一些聪明人能够帮助我检测到确切的问题,因为我已经在这里花了大约3个小时,但仍无法解决。提前谢谢。
以下是控制器的代码:
@Controller
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
...
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@RequestParam(value = "idAlmacenes", required = false) Integer idAlmacenes, Model model) {
System.out.println("paso 1");
System.out.println(idAlmacenes);
List<Producto> productos = productoService.findByAlm(idAlmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");
return "productos.html";
}
在方法"verProductos"中,我想要查看特定仓库的产品。
以下是HTML代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
crossorigin="anonymous">
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
productos
<form th:action="@{/productos/selalm}" th:field="*{idAlmacenes}" method="post">
<select class="form-control" >
<option th:each="almacen: ${almacenes}" th:text="${almacen.nombre}" th:value="${almacen.idalmacenes}"></option>
</select>
<Input type="submit" th:value="Mostrar">
</form>
<table class="table">
<thead>
<tr>
<th scope="col">Id de producto</th>
<th scope="col">Nombre</th>
<th scope="col">Precio</th>
</tr>
</thead>
<tbody>
<tr th:each="producto: ${productos}">
<td th:text="${producto.idproductos}">...</td>
<td th:text="${producto.nombre}">...</td>
<td th:text="${producto.precioUnitario}">...</td>
</tr>
</tbody>
</table>
</body>
</html>
正如你在开头看到的,我在表单中放置了一个"select",用于选择我想要检查的仓库,并将其发送到控制器"/selalm",目的是从数据库中检索数据并将其发送回"products.html",以填充下面的表格。问题是,在打印"Paso 1"和"Integer idAlmacenes"时,后者始终打印为null,无论我做什么。
最后,这是我的"Almacen"类:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="almacenes")
public class Almacen {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int idalmacenes;
public String nombre;
public String ubicacion;
public Almacen() {
super();
}
public Almacen(int idalmacenes, String nombre, String ubicacion) {
super();
this.idalmacenes = idalmacenes;
this.nombre = nombre;
this.ubicacion = ubicacion;
}
public int getId() {
return idalmacenes;
}
public void setId(int id) {
this.idalmacenes = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getUbicacion() {
return ubicacion;
}
public void setUbicacion(String ubicacion) {
this.ubicacion = ubicacion;
}
@Override
public String toString() {
return "Almacen [idalmacenes=" + idalmacenes + ", nombre=" + nombre + ", ubicacion=" + ubicacion + "]";
}
}
提前感谢大家的帮助。
英文:
The thing is, I'm doing a web-based class work to manage a warehouse with spring and Thymeleaf. The problem is that when putting a form with a dynamic select on the page with Thymeleaf, trying to retrieve the information only gives me a null after another. I would like some genius to help me detect what the exact problem is, since I have been here for about 3 hours and I am not able to get it out. Thanks in advance.
This would be the controller:
@Controller
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
...
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@RequestParam(value = "idAlmacenes", required = false) Integer idAlmacenes, Model model) {
System.out.println("paso 1");
System.out.println(idAlmacenes);
List<Producto> productos = productoService.findByAlm(idAlmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");
return "productos.html";
}
In the method "verProductos" what I want is to see the products of a specific warehouse.
This would be the Html:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
crossorigin="anonymous">
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
productos
<form th:action="@{/productos/selalm}" th:field= "*{idAlmacenes}" method="post">
<select class="form-control" >
<option th:each= "almacen: ${almacenes}" th:text= "${almacen.nombre}" th:value= "${almacen.idalmacenes}"></option>
</select>
<Input type="submit" th:value= "Mostrar">
</form>
<table class="table">
<thead>
<tr>
<th scope="col">Id de producto</th>
<th scope="col">Nombre</th>
<th scope="col">Precio</th>
</tr>
</thead>
<tbody>
<tr th:each="producto: ${productos}">
<td th:text="${producto.idproductos}">...</td>
<td th:text="${producto.nombre}">...</td>
<td th:text="${producto.precioUnitario}">...</td>
</tr>
</tbody>
</table>
</body>
</html>
As you can see in the beginning, I put a form with a "select" to select the warehouse that I want to inspect, and I send it to the controller "/selalm" with the intention of recovering the data from the BDD and sending it back to "products.html "in order to fill in the table that I have put below.
The problem is that when it comes to printing "Paso 1" and even "Integer idAlmacenes", but the latter prints it to me as null, regardless of what I do.
Finally, this is my "Almacen" class:
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="almacenes")
public class Almacen {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int idalmacenes;
public String nombre;
public String ubicacion;
public Almacen() {
super();
}
public Almacen(int idalmacenes, String nombre, String ubicacion) {
super();
this.idalmacenes = idalmacenes;
this.nombre = nombre;
this.ubicacion = ubicacion;
}
public int getId() {
return idalmacenes;
}
public void setId(int id) {
this.idalmacenes = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getUbicacion() {
return ubicacion;
}
public void setUbicacion(String ubicacion) {
this.ubicacion = ubicacion;
}
@Override
public String toString() {
return "Almacen [idalmacenes=" + idalmacenes + ", nombre=" + nombre + ", ubicacion=" + ubicacion + "]";
}
}
Many thanks to everyone in advance.
答案1
得分: 0
没有表单备份对象。我使用相同的模型来获取所选的 ID。
在控制器中,
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
model.addAttribute("selectedOne", new Almacen()); // 新添加的行
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model) {//声明发生改变
// ...
}
在 .html 文件中,修改以下行以使用该模型。
<form th:action="@{/productos/selalm}" th:object="${selectedOne}" method="post">
<select class="form-control" th:field="*{idalmacenes}">
尝试并反馈。
英文:
There is no form backing object. I used the same model to get the selected id.
In the controller,
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
model.addAttribute("selectedOne", new Almacen()); // newly added line
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model) {//change in declaration
...
}
In the .html file, modified the following lines to use the model.
<form th:action="@{/productos/selalm}" th:object="${selectedOne}" method="post">
<select class="form-control" th:field= "*{idalmacenes}" >
Try and feedback.
答案2
得分: 0
控制器的实际状态如下:
@Controller
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
@RequestMapping("/")
public String indice() {
return "index";
}
@RequestMapping("/login")
public String login() {
return "login.html";
}
@RequestMapping("/almacenes")
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "almacenes.html";
}
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll()); // 新增的一行
model.addAttribute("selectedOne", new Almacen()); // 新增的一行
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println("paso 1");
System.out.println(selectedOne.idalmacenes);
/*List<Producto> productos = productoService.findByAlm(idalmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");*/
return "productos.html";
}
}
视图的实际状态如下:
@Controller
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
@RequestMapping("/")
public String indice() {
return "index";
}
@RequestMapping("/login")
public String login() {
return "login.html";
}
@RequestMapping("/almacenes")
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "almacenes.html";
}
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll()); // 新增的一行
model.addAttribute("selectedOne", new Almacen()); // 新增的一行
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println("paso 1");
System.out.println(selectedOne.idalmacenes);
/*List<Producto> productos = productoService.findByAlm(idalmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");*/
return "productos.html";
}
}
通过以上内容,出现以下错误:
"在模板解析期间发生错误(模板:"类路径资源[templates/productos.html]")
org.thymeleaf.exceptions.TemplateInputException: 在模板解析期间发生错误(模板:"类路径资源[templates/productos.html]")
造成原因:
"造成原因:java.lang.IllegalStateException: 请求属性中既没有BindingResult也没有名称为'selectedOne'的纯目标对象可用"
英文:
the actual state of the controller is this:
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
@RequestMapping("/")
public String indice() {
return "index";
}
@RequestMapping("/login")
public String login() {
return "login.html";
}
@RequestMapping("/almacenes")
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "almacenes.html";
}
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll()); // newly added line
model.addAttribute("selectedOne", new Almacen()); // newly added line
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println("paso 1");
System.out.println(selectedOne.idalmacenes);
/*List<Producto> productos = productoService.findByAlm(idalmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");*/
return "productos.html";
}
And the actual state of the view:
public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
@RequestMapping("/")
public String indice() {
return "index";
}
@RequestMapping("/login")
public String login() {
return "login.html";
}
@RequestMapping("/almacenes")
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll());
return "almacenes.html";
}
@RequestMapping("/productos")
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute("almacenes", almacenService.findAll()); // newly added line
model.addAttribute("selectedOne", new Almacen()); // newly added line
return "productos.html";
}
@RequestMapping("/productos/selalm")
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println("paso 1");
System.out.println(selectedOne.idalmacenes);
/*List<Producto> productos = productoService.findByAlm(idalmacenes);
System.out.println("Paso 2");
System.out.println(productos.toString());
model.addAttribute("productos", productos);
System.out.println("Paso 3");*/
return "productos.html";
}
And with this, throw the error:
"An error happened during template parsing (template: "class path resource [templates/productos.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/productos.html]")"
Caused by:
"Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'selectedOne' available as request attribute"
答案3
得分: 0
好的,我明白了!问题是我没有在@ModelAttribute中提供表单的名称。在许多论坛和Spring指南中,我读过的内容中似乎不需要,但在这种特定情况下,似乎是必要的。谢谢@Niyas。
英文:
Ok, i got it!!!
The problem is i was not giving the name of the form to ther @ModelAttribute. In a lot of forums and spring guides ive readed is not necesar but, it seems that in this specific case, it was necessary. Thnk u @Niyas
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论