(Notes of things I have done recently, I will add them later using other methods)
The first part opens a new page to load static data
1 Model Map
Controller:
@RequestMapping(value = "show.do", method = RequestMethod.GET) public String show(HttpServletRequest request,HttpServletResponse response, ModelMap model) { //Call the addAttribute method of ModelMap return ""; }JSP:
<c:forEach items="${requestScope.models}" var="model"> <tr rowSelected()" onMouseOver="rowOver()" onMouseOut="rowRemove()"> <td>${model.data}</td> </tr> </c:forEach>
The second part dynamically loads page data
1 Ajax method
JavaScript:
function refresh(param) { $.ajax({ url : '...', data : { "param" : param }, dataType : 'json', success : function(result) { //process }, error : function(xhr, status, error) { console.error(error); } }) }Controller:
@RequestMapping(value="...",method= RequestMethod.GET,produces = {"text/html;charset=utf-8"}) public @ResponseBody String getData(HttpServletRequest request, HttpServletResponse response, ModelMap model) { String param=request.getParameter("param"); try { param = new String(param.getBytes("iso8859-1"), "utf-8"); //process Gson gson = new Gson (); return gson.toJson(result); } catch (UnsupportedEncodingException e) { e.printStackTrace (); return null; } }
Part III Frequently Asked Questions
1 The parameter received by the server is garbled
solution:
param = new String(param.getBytes("iso8859-1"), "utf-8");
2 The returned result is displayed as garbled characters
solution:
Add produces = {"text/html;charset=utf-8"} in RequestsMapping
Reference: Click to open the link