从前台上传图片以及内容到数据库中,然后从数据库中读取内容,因为涉及到手机网页,所以,需要根据手机屏幕大小设置图片的宽度,我这里设置的是100%。
后台controller代码如下:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Document doc = Jsoup.parse(notice.getContent()); //将notice里面的内容转换成jsoup字符串,相当于html页面的内容 打印出来的doc如下图1.
doc.getElementsByTag("img").attr("width","100%"); //获取img标签,并且设置属性width为100% 打印出来的doc如下图2.
notice.setContent(doc.toString()); //将doc放回notice的content中 打印出来的notice如下图3.
需要导入的jar包:
图1,doc如下:System.out.println(doc);
<html>
<head></head>
<body>
<img src="/xy/upload/info/teacherPic/591.jpg" alt="" />
</body>
</html>
图2,doc如下:System.out.println(doc);
<html>
<head></head>
<body>
<img src="/xy/upload/info/teacherPic/591.jpg" alt="" width="100%" />
</body>
</html>
图3,notice的content如下:System.out.println(notice.getContent);
<html>
<head></head>
<body>
<img src="/xy/upload/info/teacherPic/591.jpg" alt="" width="100%" />
</body>
</html>
完整controller方法如下:
/**
* 跳转到公共通知详细页面
* @param request
* @return
*/
@RequestMapping("/getNoticeById")
public String getNoticeById(HttpServletRequest request){
int noticeId = Integer.parseInt(request.getParameter("noticeId"));
//1教职工通知2教职工部门通知3学生公共通知4学生班级通知5学生系部通知
int flag = Integer.parseInt(request.getParameter("flag"));
Notice notice = null;
if(flag==1){
notice = publicNoticeService.findById(noticeId);
publicNoticeService.addHitTimes(noticeId);
request.setAttribute("title", "教职工公共通知");
}else if(flag==2){
notice = deptNoticeService.findById(noticeId);
deptNoticeService.addHitTimes(noticeId);
request.setAttribute("title", "教职工部门公共通知");
}else if(flag==3){
notice = stuPublicNoticeService.findById(noticeId);
stuPublicNoticeService.addHitTimes(noticeId);
request.setAttribute("title", "学生公共通知");
}else if(flag==4){
notice = stuClassNoticeService.findById(noticeId);
stuClassNoticeService.addHitTimes(noticeId);
request.setAttribute("title","学生班级通知");
}else if(flag==5){
notice = stuDeptNoticeService.findById(noticeId);
stuDeptNoticeService.addHitTimes(noticeId);
request.setAttribute("title","学生系部通知");
}
Document doc = Jsoup.parse(notice.getContent());
doc.getElementsByTag("img").attr("width","100%");
notice.setContent(doc.toString());
request.setAttribute("notice", notice);
return "/plugins/mobileweb/web/views/notice/noticeDetail";
}