controller接受参数并设置页面里的结果,接受的参数与页面的设置有关
//这里写注解,不然扫描不到.do
@Controller
public class WarehouseCheckController {
@Autowired
UserService ser;//要使用的service
@RequestMapping(value="Warehouse_Check_ProductShelfRelation.do",method={RequestMethod.GET,RequestMethod.POST})
//value里指明这个函数对应的.do
//页面里的action跳转时设置action="Warehouse_Check_ProductShelfRelation.do"就转到这个函数来处理
public ModelAndView linchao(HttpServletRequest request,Shelf sh){//这里加了HttpServletRequest参数,由页面传入,Shelf类由页面里的表单post而来
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("Warehouse_Check");
List<User> list=ser.findAll();
request.setAttribute("atrlist",list);//这里注入list类参数
request.setAttribute("amount",13);//这里注入单个参数
return modelAndView;
}
}
在jsp页面的开头第二行加
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
通过form提交给controller参数
<form class="form-inline"
action="${pageContext.request.contextPath}/Warehouse_Check_ProductShelfRelation.do"
method="post"><!post提交表单给controller里对应的.do-->
<div >
<label>货架名</label> <input type="text"
name="shel_name" ><!这里传给controller的参数是com.cqu.model包里的实体类的形式,shel_name在这里是Shelf类的属性-->
</div>
<div>
<label>货架位置</label> <input type="text"
name="shel_loc">
</div>
<div class="form-group filter-btn">
<button class="btn btn-default">查询</button>
</div>
</form>
table遍历一个list并动态添加显示出的行
<tbody>
<tr>
<td>数量</td>
<td>${requestScope.amount}</td><!--注入单个参数,controller中的request.setAttribut实现注入-->
</tr>
<c:forEach items="${requestScope.atrlist}" var="user" varStatus="status">
<tr>
<td>status.index</td> <!--varStatus.index提供从零开始的行号-->
<td>${user.user_email}</td><!--读取实体类的属性时直接写属性名而不是调用get-->
</tr>
</c:forEach>
</tbody>
网友评论