ModelAndView
程序员文章站
2022-07-03 12:04:12
...
如:前台:
//查看公告
function look() {
var row = $('#dg').datagrid('getSelected');
if(row) {
var id = row.id;
window.location.href = "xxLook?id=" + id;
}
}
如果出现乱码问题需要加编码:
window.location.href = "look?dwlx=" +encodeURI(encodeURI(dwlx))+"&ssnd="+ssnd+"&sspc="+sspc;
或者后台接收时:
title= new String( title.getBytes("iso-8859-1"), "UTF-8");
后台:
// 查看
@GetMapping("/xxLook")
public ModelAndView look(String id) {
ModelAndView mv = new ModelAndView();
mv.setViewName("azcbdxxlook");
AzcBdxx azcBdxx = service.getById(id);
mv.addObject("azc", azcBdxx);
return mv;
}
返回的视图azcbdxxlook.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>安置处查看</title>
<script src="../assets/js/scripts/jquery.min.js" type="text/javascript"></script>
<script>
function fh() {
window.location.href = "azcbdxx";
}
</script>
</head>
<body>
<table id="declaretable" class="declaretable" border="1"
cellpadding="0" cellspacing="0">
<tr>
<td colspan="7" align="center">士兵基本信息</td>
</tr>
<tr>
<td>身份证号码</td>
<td>${azc.sfzhm }</td>
<td>姓名</td>
<td>${azc.xm }</td>
<td>籍贯</td>
<td colspan="2">${azc.jg }</td>
</tr>
<tr>
<td colspan="6"><input type="button" value="返回" onclick="fh()"></td>
</tr></table>
</body>
</html>
如果后台返回的为集合,前台可以用jstl遍历,如:
@GetMapping("/xxLook")
public ModelAndView noticeLook(String id){
ModelAndView andView=new ModelAndView();
andView.setViewName("msgfxblook");
MsgFxb msgFxb=service.getMsgFxbById(id);
andView.addObject("title",msgFxb.getXxbt() );
List<Appendix> list=service.getAppdixList(id);
List<String> imglist=new ArrayList<>();
List<String> imgid=new ArrayList<>();
for(Appendix a:list){
imglist.add("../assets/img/"+a.getFjmc());
imgid.add(a.getId());
}
andView.addObject("img", imglist);
andView.addObject("imgid", imgid);
return andView;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>法宣办查看</title>
<script src="../assets/js/scripts/jquery.min.js" type="text/javascript"></script>
<script>
function fh(){
window.location.href="msgfxb";
}
function download(id){
var dt="&id="+id;
$.post("download",dt,function(data){
alert(data.msg);
});
}
</script>
</head>
<body>
<table>
<tr>
<td>信息标题</td>
<td>${title}</td>
</tr>
<tr>
<td>附件</td>
</tr>
<tr>
<c:forEach items="${img}" var="li" varStatus="s">
<td><img alt="" src=<c:out value="${li}"></c:out>>
</td>
</c:forEach>
</tr>
<tr>
<c:forEach items="${imgid}" var="li" varStatus="s">
<td> <button onclick="download('${li}')">下载</button>
</td>
</c:forEach>
</tr>
<td><input type="button" value="返回" onclick="fh()"></td>
</table>
</body>
</html>
再如:
//返回商品展示视图
@GetMapping("/goodslook")
public ModelAndView goGoods(HttpServletRequest req) throws UnsupportedEncodingException{
req.setCharacterEncoding("utf-8");
ModelAndView mv=new ModelAndView();
String title=req.getParameter("title");
title= new String( title.getBytes("iso-8859-1"), "UTF-8");
System.out.println("分类"+title);
classfiy.setTitle(title);
mv.setViewName("goodslook");
List<Goodsinfo> list=service.getGoods(classfiy);
// System.out.println(list);
mv.addObject("gl", list);
return mv;
}
goodslook.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${gl}" var="li" varStatus="s">
<c:out value="${li.title }"></c:out>
</c:forEach>
</body>
</html>
上一篇: 只需做好这10点移动网站排名迅速上升
下一篇: 网站制作基础教程(3):WEB标准
推荐阅读