提交值后无法在表中获取值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了提交值后无法在表中获取值相关的知识,希望对你有一定的参考价值。
我正在开发一个Spring-MVC项目,它有一个页面product.jsp在这个页面中有一个表,我必须通过JSTL在该表上应用条件。条件是,如果值为null,则将打印未找到记录,如果我们输入值,则打印值。我也无法在表中获取值,控制器,服务和存储库类是否有任何问题?我曾经尝试过,但我没有得到答案。这是我的代码......
这是我的product.jsp
页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<title>Login page</title>
</head>
<body>
<form:form method="POST" modelAttribute="productData"
action="product.do">
<table border="1">
<tr>
<td>Product Id :</td>
<td><form:input path="productId" /></td>
</tr>
<tr>
<td>Product Name :</td>
<td><form:input path="productName" /></td>
</tr>
<tr>
<td>Product Desc :</td>
<td><form:input path="productDesc" /></td>
</tr>
<tr>
<td>Price :</td>
<td><form:input path="price" /></td>
</tr>
<tr>
<td>Quantity :</td>
<td><form:input path="quantity" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td><input type="reset" value="Reset" align="right"></td>
</tr>
</table>
</form:form>
<br>
<br>
<br>
<table id="example" class="display" border="2" align="center">
<tr>
<td><B>Product ID </td>
<td><B>Product Name </td>
<td><B>Product Description </td>
<td><B>Price </td>
<td><B>Quantity </td>
</tr>
<c:forEach items="${prod}" var="prodlist" />
<tr>
<td><c:out value="${prodlist.productId}" /></td>
<td><c:out value="${prodlist.productName}" /></td>
<td><c:out value="${prodlist.productDesc}" /></td>
<td><c:out value="${prodlist.price}" /></td>
<td><c:out value="${prodlist.quantity}" /></td>
</tr>
<c:if test="${prodlist = null}">
<c:out value="No records found!!" />
</c:if>
<c:if test="${prodlist != null }">
<c:out value="${example.prodlist}" />
</c:if>
</table>
</body>
</html>
这是ProductController
类。
@Controller
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping(value = "/product", method = { RequestMethod.GET, RequestMethod.POST })
public String showProduct(@ModelAttribute("productData") ProductDTO product, BindingResult result,
Map<String, Object> map, HttpServletRequest request) {
System.out.println("Inside productController");
map.put("prod", productService.getProductDetails(product));
if ("get".equals(request.getMethod())) {
map.put("productData", product);
return "product";
} else {
productService.checkProduct(product);
return "product";
}
}
}
ProductService.java
是
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public void checkProduct(ProductDTO product) {
System.out.println("Inside product repository");
System.out.println(product.getProductId());
System.out.println(product.getProductName());
System.out.println(product.getProductDesc());
System.out.println(product.getPrice());
System.out.println(product.getQuantity());
productRepository.checkProduct(product);
}
public List getProductDetails(ProductDTO prodlist) {
List list = productRepository.getProductDetails(prodlist);
Iterator itr = list.iterator();
while (itr.hasNext()) {
ProductDTO product = (ProductDTO) itr.next();
System.out.println(product.getProductId() + " " + product.getProductName() + " "
+ product.getProductDesc() + " " + product.getPrice() + " " + product.getQuantity());
}
return list;
}
}
ProductRepository.java
是
@Repository
public class ProductRepository {
@Autowired
private HibernateTemplate hibernateTemplate;
public void checkProduct(ProductDTO product) {
System.out.println("Inside product repository");
System.out.println(product.getProductId());
System.out.println(product.getProductName());
System.out.println(product.getProductDesc());
System.out.println(product.getPrice());
System.out.println(product.getQuantity());
hibernateTemplate.save(product);
}
public List getProductDetails(ProductDTO prod) {
List list = hibernateTemplate.find("from ProductDTO");
Iterator itr = list.iterator();
while (itr.hasNext()) {
ProductDTO product = (ProductDTO) itr.next();
System.out.println(product.getProductId() + " " + product.getProductName() + " "
+ product.getProductDesc() + " " + product.getPrice() + " " + product.getQuantity());
}
return list;
}
}
答案
看起来你太早关闭你的forEach
。尝试将表格行包装为每个像:
<c:forEach items="${prod}" var="prodlist">
<tr>
<td><c:out value="${prodlist.productId}" /></td>
<td><c:out value="${prodlist.productName}" /></td>
<td><c:out value="${prodlist.productDesc}" /></td>
<td><c:out value="${prodlist.price}" /></td>
<td><c:out value="${prodlist.quantity}" /></td>
</tr>
</c:forEach>
要检查空列表,您可以这样检查:
<c:if test="${prod.length == 0}">
<c:out value="No records found!!" />
</c:if>
您可能需要检查prod.size == 0
而不是prod.length
,但其中一个应该可以工作。
以上是关于提交值后无法在表中获取值的主要内容,如果未能解决你的问题,请参考以下文章