解决方案:可以使用jstl的函数库
步骤:
(1)在jsp引入函数
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
(2)在input标签中添加
<c:if test="${fn:contains(requestScope.emp.hobby,‘sport’)}">checked</c:if>/>判断,其中emp是服务器中域对象request传参时的键,hobby是值emp类中的属性
<div class="field" style="padding-top:8px;">
运动 <input type="checkbox" name="hobby" value="sport"
<c:if test="${fn:contains(requestScope.emp.hobby,'sport')}">checked</c:if>/>
阅读 <input type="checkbox" name="hobby" value="reader"
<c:if test="${fn:contains(requestScope.emp.hobby,'reader')}">checked</c:if>
/>
写代码 <input type="checkbox" name="hobby" value="code"
<c:if test="${fn:contains(requestScope.emp.hobby,'code')}">checked</c:if>
/>
</div>
</div>
服务器中的代码
private void findOne(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//获取参数
String eid = request.getParameter("eid");
System.out.println(eid);
//调用service处理数据
EmpService empService=new EmpServiceImpl();
Emp emp = empService.findEmpById(Integer.valueOf(eid));
System.out.println("转换成功");
if(emp!=null){
//成功,发送到更新页面
request.setAttribute("emp",emp);
request.getRequestDispatcher("/updateUser.jsp").forward(request,response);
}else {
response.getWriter().write("data is empty");
}
}