MVC controller層傳遞引數到前端頁面
阿新 • • 發佈:2019-01-04
方法一: 使用Model類,以key-value的形式傳遞一些簡單的引數(示例為springboot專案中的方法,springboot預設的前端頁面檔案型別為HTML,位於 /專案名/src/main/resources/templates 下)
注意:return的內容不要以“/”開頭,若以“/”開頭,在eclipse中能被檢視解析器正常解釋,但部署之後就不一定了,要注意一些細節規範。
//controller類中的方法 String bContent(Model model){ ...... model.addAttribute("name", getUser().getName()); model.addAttribute("username", getUser().getUsername()); return "yearvacation/user/list"; }
方法二:使用 ModelAndView類,與方法一類似
public ModelAndView save( ) throws IOException {
.....
ModelAndView mv = new ModelAndView();
mv.addObject("CorrectNum", correctNum);
mv.addObject("MisNum", misNum);
mv.setViewName("vacation/input/input");
return mv;
}
方法三:通過HttpServletRequest和HttpServletResponse,可以用來下載檔案(下面是我實際使用的示例,比較複雜。可以直接看方法前的註釋瞭解功能即可。)
注意:方法的返回值為null,因為如果跳轉到其他頁面會在eclipse上報錯OutputStream has already been called.雖然不影響正常使用但總歸是不太好的。
//根據map引數來生成合適的下載內容,並回傳 String download(HttpServletRequest req, HttpServletResponse rep, @RequestParam Map<String, Object> params) throws IOException { List<UserDO> warnlist = userService.warnlist(params); DownloadUtil.downloadAbuseInfo(req, rep, warnlist); return null; }
/根據前端傳遞過來的map引數獲得實體類user的list
public List<UserDO> warnlist(Map<String, Object> query) {
List<UserDO> list = userMapper.listAll(query);
List<UserDO> warnlist = new ArrayList<>();
for (UserDO user : list) {
// 獲取其到年底累積的年假時長
float vacation = CacuYearVacation.preaccum(user.getWorkstart());
// 獲取其有效特批假期時長
String username = user.getUsername();
List<SpecialyvDO> splist = SpecialyvMapper.selectByName(username);
Float sum = 0.0F;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (SpecialyvDO sp : splist) {
Date expire;
try {
expire = sdf.parse(sp.getExpiredate());
if (expire.after(new Date())) {
Float l = Float.parseFloat(sp.getSpelong());
sum += l;
}
//引入poi包中HSSF類將list型別的引數轉換為excel表格並返回給rep以達到下載的目的。
public static String downloadAbuseInfo(HttpServletRequest req, HttpServletResponse rep, List<UserDO> yvList)
throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("超額人員名單");
HSSFRow row1 = sheet.createRow(0);
String[] colname = { "工號", "姓名", "部門", "超額使用年假時長(小時)" };
for (int i = 0; i < colname.length; i++) {
HSSFCell cell = row1.createCell(i);
cell.setCellValue(colname[i]);
}
int index = 1;
for (UserDO yv : yvList) {
HSSFRow row = sheet.createRow(index);
index++;
String userid = yv.getUsername();
HSSFCell cell1 = row.createCell(0);
cell1.setCellValue(userid);
String name = yv.getName();
HSSFCell cell2 = row.createCell(1);
cell2.setCellValue(name);
String deptname = yv.getDeptName();
HSSFCell cell3 = row.createCell(2);
cell3.setCellValue(deptname);
Float startdate = yv.getYearvacationleft();
HSSFCell cell4 = row.createCell(3);
cell4.setCellValue(startdate);
}
String fileName = "超額年休人員名單.xls";
OutputStream os = getdownloadstream(req, rep, fileName);
try {
wb.write(os);
os.close();
wb.close();
} catch (Exception e) {
// TODO: handle exception
return "fail";
}
finally {
if (os != null)
os.close();
if (wb != null)
wb.close();
}
return "sucess";
}
//識別request,然後對response進行一些設定以使其可以用來下載檔案
public static OutputStream getdownloadstream(HttpServletRequest req, HttpServletResponse rep, String fileName)
throws IOException {
rep.setContentType("multipart/form-data");
//根據瀏覽器型別不同來對檔名進行編碼,因為火狐有點特別
String userAgent = req.getHeader("User-Agent");
if (userAgent.toLowerCase().indexOf("firefox") > 0) {
fileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
} else {
fileName = URLEncoder.encode(fileName, "UTF-8");
}
//System.out.println(fileName);
rep.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
rep.setHeader("Cache-control", "must-revalidate,post-check=0,pre-check=0");
rep.setHeader("Pragma", "public");
rep.setDateHeader("expires", (System.currentTimeMillis() + 1000));
OutputStream os = rep.getOutputStream();
return os;
}