开发框架:springmvc+mybaties+spring
页面:jsp+js+jquery
开发工具eclipse
数据库:oracle11g
springmvc中jsp页面checkbox如何动态从后台获得
1.实体TItmpCode
public class TItmpCode { //要情信息code:1,2,3 private String dmz; //要情信息text:涉警,涉军,校车 private String dmsm1; }
2.service
@Service public class TEventsServiceImpl implements TEventsService{ @Autowired private TItmpCodeMapper codeMapper; /** * 查询要情信息(重点关注) [email protected]: [email protected] */ public List<TItmpCode> selectHighLightList(){ return codeMapper.selectHighLightList(); } }
3:首先controller
@Controller @RequestMapping(value="/events") public class TEventsController { @Autowired private TEventsServiceImpl eventsService; /** * [email protected]: 初始化页面 [email protected] req [email protected] */ @RequestMapping(value="/loadevents",produces = { "application/json;charset=UTF-8" }) public String loadArtificalRecive(HttpServletRequest req,Model model,HttpServletResponse resp){ //要情信息 List<TItmpCode> hightlightsList=this.eventsService.selectHighLightList(); //返回model() model.addAttribute("hightlightsList", hightlightsList); return "tcs/findEvents"; } }
2:jsp页面
首先在jsp中引入c标签:<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
然后利用c标签进行循环得到:
<div style="width:200px; margin-top:15px;"> <c:forEach items="${hightlightsList}" var="light"> <span > <input type="checkbox" name="zdgz" value="${light.dmz}" />${light.dmsm1} </c:forEach> </div>
3.页面效果:
注意:
1.springmvc中要想把获得的数据返回给前台,那么把获得的list数据或者map数据封装到model中,前台从model中获得
2.checkbox也可以像select一样存储key和value,如:
<input type="checkbox" name="zdgz" value="${light.dmz}" />${light.dmsm1}
${light.dmz}就是checkbox的值,传递到后台存储至数据库,数据库获得的数据就是1,2,3
${light.dmsm1}就是checkbox的text,即是在前台要展示的文本。
因为后台hightlightsList封装的是实体TItmpCode,所以直接取对应的属性名。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11203.html