通过main.jsp点击超链接录入新商品,获取商品类型,并将产品类型数据req.setAttribute("typeList", typeList);传入到下一个prd_add.jsp页面,但是产品类型数据显示不出来。
以下说明该过程容易出现错误的地方:
一、业务逻辑
1.main.jsp
通过超链接转入商品录入界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul>
<li><a href="/mybatis_demo1/prdServlet?flag=list">产品列表</a></li>
<li><a href="/mybatis_demo1/prdServlet?flag=preAdd">录入新产品</a></li>
</ul>
</body>
</html>
2.prd_add.jsp
商品录入界面,通过jstl、el表达式获取req对象中的typeList的值。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/mybatis_demo1/prdServlet?flag=add" method="post">
商品名称:<input type="text" name="prdName" /><br/>
类别:<select name="typeId">
<option value="0">请选择商品类别</option>
<c:if test="${not empty typeList }">
<c:forEach items="${typeList } " var="type">
<option value="${type.typeId } ">${type.typeName }</option>
</c:forEach>
</c:if>
</select><br/>
描述:<textarea rows="3" cols="50" name ="desc"></textarea><br/>
价格:<input type=" text" name="price"/><br/>
<input type="submit" value="录入商品"/>
</form>
${msg}
</body>
</html>
servlet通过判断请求参数的值,获取产品类型列表,并将typeList传入req对象中,然后通过请求转发,将req,resp对象传入下一个jsp页面。
if("preAdd".equals(flag)){
//获取商品所有类别
List<ProductTypeInfo> typeList = this.productTypeService.getAllTypes();
req.setAttribute("typeList", typeList);
req.getRequestDispatcher("/prd/prd_add.jsp").forward(req, resp);
}
注意1:
如果只进行req.getRequestDispatcher("/prd/prd_add.jsp");这条语句,那么你转发到的jsp页面也没有typeList列表。正确的语句应该是加上forward(req, resp),才能保证下一个jsp页面的req和resp对象与上一个jsp页面的对象保持一致。
req.getRequestDispatcher("/prd/prd_add.jsp").forward(req, resp);
3.servlet
录入新商品。录入成功后,在转发到录入新商品界面。
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String flag = req.getParameter("flag");
if("add".equals(flag)){
String prdName = req.getParameter("prdName");
String typeId = req.getParameter("typeId");
String desc = req.getParameter("desc");
String price = req.getParameter("price");
ProductInfo info = new ProductInfo();
info.setPrdName(prdName);
if(typeId != null && typeId.length() >0){
Integer id = Integer.parseInt(typeId);
if(id>0){
info.setTypeId(id);
}
}
info.setDesc(desc);
if(price!=null && price.length()>0 ){
Float p = Float.parseFloat(price);
info.setPrice(p);
}
boolean result = this.productTypeService.saveInfo(info);
req.setAttribute("msg", result ? "success!" : "failure!");
List<ProductTypeInfo> typeList = this.productTypeService.getAllTypes();
req.setAttribute("typeList", typeList);
req.getRequestDispatcher("/prd/prd_add.jsp").forward(req, resp);
}
}
二、测试
1.在对应servlet服务类中进行测试,typeList列表打印正常。
if("preAdd".equals(flag)){
//获取商品所有类别
List<ProductTypeInfo> typeList = this.productTypeService.getAllTypes();
req.setAttribute("typeList", typeList);
//text
for (ProductTypeInfo productTypeInfo : typeList) {
System.out.println(productTypeInfo.getTypeId() + productTypeInfo.getTypeName());
}
req.getRequestDispatcher("/prd/prd_add.jsp").forward(req, resp);
}
注:在servlet中测试,typeList正常打印,说明从控制层<——>服务层<——>持久化层,数据typeList的获取没有问题。
image.png2.测试prd_add.jsp页面的typeList是否为空。< !-- text -->中为测试代码
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- text -->
<c:if test="${not empty typeList }">
typeList not empty
</c:if>
<!-- text -->
<form action="/mybatis_demo1/prdServlet?flag=add" method="post">
商品名称:<input type="text" name="prdName" /><br/>
类别:<select name="typeId">
<option value="0">请选择商品类别</option>
<c:if test="${not empty typeList }">
<c:forEach items="${typeList }" var="type">
<option value="${type.typeId } ">${type.typeName }</option>
</c:forEach>
</c:if>
</select><br/>
描述:<textarea rows="3" cols="50" name ="desc"></textarea><br/>
价格:<input type=" text" name="price"/><br/>
<input type="submit" value="录入商品"/>
</form>
${msg}
</body>
</html>
测试结果
image.png注:在表单外测试,typeList正常传递到jsp页面,到这里基本的数据传输错误已经排除了。
3.测试jstl的foreach语句,也没有发现问题。
4.测试jstl的foreach语句中的语句:
<option value="${type.typeId } ">${type.typeName }</option>
网友评论