一个春天的请求只给了我null

huangapple go评论61阅读模式
英文:

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(&quot;/productos&quot;)
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll());
return &quot;productos.html&quot;;
}
@RequestMapping(&quot;/productos/selalm&quot;)
public String verProductos(@RequestParam(value = &quot;idAlmacenes&quot;, required = false) Integer idAlmacenes, Model model) {
System.out.println(&quot;paso 1&quot;);
System.out.println(idAlmacenes);
List&lt;Producto&gt; productos = productoService.findByAlm(idAlmacenes);
System.out.println(&quot;Paso 2&quot;);
System.out.println(productos.toString());
model.addAttribute(&quot;productos&quot;, productos);
System.out.println(&quot;Paso 3&quot;);
return &quot;productos.html&quot;;
}

In the method "verProductos" what I want is to see the products of a specific warehouse.

This would be the Html:

&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot; 
integrity=&quot;sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z&quot; 
crossorigin=&quot;anonymous&quot;&gt;
&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
productos
&lt;form th:action=&quot;@{/productos/selalm}&quot; th:field= &quot;*{idAlmacenes}&quot; method=&quot;post&quot;&gt;
&lt;select class=&quot;form-control&quot; &gt;
&lt;option th:each= &quot;almacen: ${almacenes}&quot; th:text= &quot;${almacen.nombre}&quot; th:value= &quot;${almacen.idalmacenes}&quot;&gt;&lt;/option&gt;
&lt;/select&gt;
&lt;Input type=&quot;submit&quot; th:value= &quot;Mostrar&quot;&gt;
&lt;/form&gt;
&lt;table class=&quot;table&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot;&gt;Id de producto&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;Nombre&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;Precio&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr th:each=&quot;producto: ${productos}&quot;&gt;
&lt;td th:text=&quot;${producto.idproductos}&quot;&gt;...&lt;/td&gt;
&lt;td th:text=&quot;${producto.nombre}&quot;&gt;...&lt;/td&gt;
&lt;td th:text=&quot;${producto.precioUnitario}&quot;&gt;...&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

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=&quot;almacenes&quot;)
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 &quot;Almacen [idalmacenes=&quot; + idalmacenes + &quot;, nombre=&quot; + nombre + &quot;, ubicacion=&quot; + ubicacion + &quot;]&quot;;
}
}

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(&quot;/productos&quot;)
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll());
model.addAttribute(&quot;selectedOne&quot;, new Almacen()); // newly added line
return &quot;productos.html&quot;;
}
@RequestMapping(&quot;/productos/selalm&quot;)
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model) {//change in declaration
...
}

In the .html file, modified the following lines to use the model.

&lt;form th:action=&quot;@{/productos/selalm}&quot; th:object=&quot;${selectedOne}&quot; method=&quot;post&quot;&gt;
&lt;select class=&quot;form-control&quot;  th:field= &quot;*{idalmacenes}&quot; &gt;

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(&quot;/&quot;)
public String indice() {
return &quot;index&quot;;
}
@RequestMapping(&quot;/login&quot;)
public String login() {
return &quot;login.html&quot;;
}
@RequestMapping(&quot;/almacenes&quot;)
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll());
return &quot;almacenes.html&quot;;
}
@RequestMapping(&quot;/productos&quot;)
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll()); // newly added line
model.addAttribute(&quot;selectedOne&quot;, new Almacen()); // newly added line
return &quot;productos.html&quot;;
}
@RequestMapping(&quot;/productos/selalm&quot;)
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println(&quot;paso 1&quot;);
System.out.println(selectedOne.idalmacenes);
/*List&lt;Producto&gt; productos = productoService.findByAlm(idalmacenes);
System.out.println(&quot;Paso 2&quot;);
System.out.println(productos.toString());
model.addAttribute(&quot;productos&quot;, productos);
System.out.println(&quot;Paso 3&quot;);*/
return &quot;productos.html&quot;;
}

And the actual state of the view:

public class MiControlador {
@Autowired
IProductoSERVICE productoService;
@Autowired
IAlmacenSERVICE almacenService;
@Autowired
IVentaSERVICE ventaService;
@RequestMapping(&quot;/&quot;)
public String indice() {
return &quot;index&quot;;
}
@RequestMapping(&quot;/login&quot;)
public String login() {
return &quot;login.html&quot;;
}
@RequestMapping(&quot;/almacenes&quot;)
public String verAlmacenes(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll());
return &quot;almacenes.html&quot;;
}
@RequestMapping(&quot;/productos&quot;)
public String verProductos(Model model) {
System.out.println(almacenService.findAll());
model.addAttribute(&quot;almacenes&quot;, almacenService.findAll()); // newly added line
model.addAttribute(&quot;selectedOne&quot;, new Almacen()); // newly added line
return &quot;productos.html&quot;;
}
@RequestMapping(&quot;/productos/selalm&quot;)
public String verProductos(@ModelAttribute @NotNull Almacen selectedOne, Model model){
System.out.println(&quot;paso 1&quot;);
System.out.println(selectedOne.idalmacenes);
/*List&lt;Producto&gt; productos = productoService.findByAlm(idalmacenes);
System.out.println(&quot;Paso 2&quot;);
System.out.println(productos.toString());
model.addAttribute(&quot;productos&quot;, productos);
System.out.println(&quot;Paso 3&quot;);*/
return &quot;productos.html&quot;;
}

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

huangapple
  • 本文由 发表于 2020年9月28日 02:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64091714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定