【发布时间】:2019-03-25 07:08:12
【问题描述】:
我正在处理一个 Spring-MVC 项目,该项目有一个页面 product.jsp 在此页面中有一个表,我必须在该表上应用 JSTL 条件。 条件是如果值为 null,它将打印 No record found,如果我们输入值,它会打印值。我也无法在表中获取值,控制器、服务和存储库类有什么问题吗? 我试过了,但我没有得到答案。 这是我的代码...
这是我的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() + "\t" + product.getProductName() + "\t"
+ product.getProductDesc() + "\t" + product.getPrice() + "\t" + 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() + "\t" + product.getProductName() + "\t"
+ product.getProductDesc() + "\t" + product.getPrice() + "\t" + product.getQuantity());
}
return list;
}
}
【问题讨论】:
-
查看答案并告诉我们!
-
请先描述您的需求!我的意思是你的申请流程
-
实际上我正在开发一个 Spring MVC 项目,它有一个 登录页面,当我们点击 登录页面 的提交按钮时,然后 产品页面 将打开,因此在此产品页面中,我们有诸如 - productId、productName、productDesc、price、quantity 之类的属性,因此当我们提交所有值时,它将打印我们输入的那些值,如果我们提交空白它将打印未找到记录。所以当我点击 login page 时,它会打开 product page 并且消息也显示 no records found 而不点击提交按钮。
-
发生这种情况是因为您在一个 jsp 页面中完成所有工作。您将不得不为表单制作一个页面。提交表单后,您必须重定向到显示数据的不同页面。所以需要三个jsp页面。
LoginPage-> FormPage-> DetailsPage -
但这是我的要求,在我的产品中。 jsp 页面还有一个表格,所有详细信息都将在提交按钮后显示,如果详细信息不可用,它将给出消息 找不到记录
标签: maven spring-mvc jsp jstl spring-jdbc