標籤:
1.讀取資料庫中的記錄List<HashMap<String,String>> attributes;
2.Controller構造Model如下:
@RequestMapping("display.do")
public ModelAndView display(HttpServletRequest request, ModelMap model) {
List<HashMap<String, String>> myList = disService.getAllRecords();
model.addAttribute("list", myList);
/*此處可追加Attribute*/
return new ModelAndView("display.jsp");
}
3.display.jsp使用內嵌java顯示記錄
頁面引入類
<%@ page import="java.util.List"%>
<%@ page import="java.util.HashMap"%>
擷取list
<%
List<HashMap<String,String>> list = (List<HashMap<String,String>>)request.getAttribute("list");
%>
以table形式顯示資料
<table style="width:100%">
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
<%for (int i=0;i<list.size();i++){%>
<tr>
<td><%out.print(list.get(i).get("key1")); %></button></td>
<td><%out.print(list.get(i).get("key2")); %></td>
<td><%out.print(list.get(i).get("key3")); %></td>
</tr>
<%} %>
</table>
springMVC筆記:jsp頁面擷取後台資料記錄列表