首页>代码>spring+spring mvc+hibernate+bootstrap开发小型物业管理系统>/物业管理系统/propertysystem/src/com/www/controller/NoticeController.java
package com.www.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.www.entity.Notice;
import com.www.service.NoticeService;

@Controller
@RequestMapping("/notice")
public class NoticeController {
	@Resource
	private NoticeService noticeService;
	@Autowired
	private HttpServletRequest request;
	List<String> imagePath = new ArrayList<String>();;

	// 跳转到增加通告的页面
	@RequestMapping(value = "/toadd")
	protected ModelAndView toadd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("进来了?。。。toaddtoaddtoadd。。");

		System.out.println("我到这里了。。。");
		return new ModelAndView("admin/noticeadd");
	}

	@RequestMapping(value = "/list")
	protected ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer hql = new StringBuffer(" from Notice where 1=1 ");
		System.out.println(hql + "hql================");
		List list = noticeService.findAll(hql.toString(), null);
		map.put("list", list);
		return new ModelAndView("/admin/noticelist", map);
	}
	@RequestMapping(value = "/show")
	protected ModelAndView show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer hql = new StringBuffer("select  *  from notice    where 1=1 order by id   desc  LIMIT  1");
		System.out.println(hql + "hql================");
		List list = noticeService.find(hql.toString());
		Notice	notice=new Notice();
		Object[] obj=(Object[]) list.get(0);
		notice.setId(Integer.parseInt(obj[0].toString()));
		notice.setPicurl1(obj[1] != null ? obj[1].toString() : null);
		notice.setPicurl2(obj[2] != null ? obj[2].toString() : null);
		notice.setPicurl3(obj[3] != null ? obj[3].toString() : null);
		notice.setPicurl4(obj[4] != null ? obj[4].toString() :null);
		notice.setPicurl5(obj[5] != null ? obj[5].toString() : null);
		notice.setPicurl6(obj[6] != null ? obj[6].toString() : null);
		
		notice.setTitle1(obj[7] != null ? obj[7].toString() : "");
		notice.setTitle2(obj[8] != null ? obj[8].toString() : "");
		notice.setTitle3(obj[9] != null ? obj[9].toString() : "");
		notice.setTitle4(obj[10] != null ? obj[10].toString() : "");
		notice.setTitle5(obj[11] != null ? obj[11].toString() : "");
		notice.setTitle6(obj[12] != null ? obj[12].toString() : "");
		
		notice.setContent1(obj[13] != null ? obj[13].toString() : "");
		notice.setContent2(obj[14] != null ? obj[14].toString() : "");
		notice.setContent3(obj[15] != null ? obj[15].toString() : "");
		notice.setContent4(obj[16] != null ? obj[16].toString() : "");
		notice.setContent5(obj[17] != null ? obj[17].toString() : "");
		notice.setContent6(obj[18] != null ? obj[18].toString() : "");
		
		System.out.println(notice.toString()+"/////////////////");
		
		map.put("notice", notice);
		return new ModelAndView("/owner/home", map);
	}
	// shan某条公告
	@RequestMapping(value = "/{id}/del")
	protected ModelAndView del(HttpServletRequest request,
			HttpServletResponse response, @PathVariable int id)
			throws Exception {
		System.out.println("..del.....进来没有?");
		noticeService.delete(id);
		return new ModelAndView("redirect:/notice/list");
	}

	/* 将接收到的页面的值修改过来--保存数据---》跳到显示页面 */
	@RequestMapping(value = "/add")
	public ModelAndView updateUser(HttpServletRequest request,
			HttpServletResponse response,

			@RequestParam MultipartFile[] cfile) throws Exception, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		String title1 = request.getParameter("title1");
		String title3 = request.getParameter("title3");
		String title4 = request.getParameter("title4");
		String title5 = request.getParameter("title5");
		String title6 = request.getParameter("title6");
		String title2 = request.getParameter("title2");
		String content1 = request.getParameter("content1");
		String content2 = request.getParameter("content2");
		String content3 = request.getParameter("content3");
		String content4 = request.getParameter("content4");
		String content5 = request.getParameter("content5");
		String content6 = request.getParameter("content6");
		
		System.out.println(content2 + "----------");
		System.out.println("进来没有");

		// 判断file数组不能为空并且长度大于0
		if (cfile != null && cfile.length > 0) {
			// 循环获取file数组中得文件
			for (int i = 0; i < cfile.length; i++) {
				MultipartFile file = cfile[i];
				// 保存文件
				saveFile(file);
			}
		}
		String picurl[] = new String[6];
		for (int i = 0; i < imagePath.size(); i++) {
			System.out.println(i + "=============i=============");
			System.out.println(imagePath.get(i).toString() + "========");
			picurl[i] = imagePath.get(i).toString();
			System.out.println(picurl[i] + "=======图片数组===");
		}
		Notice notice = new Notice();
		notice.setTitle1(title1);
		notice.setTitle2(title2);
		notice.setTitle3(title3);
		notice.setTitle4(title4);
		notice.setTitle5(title5);
		notice.setTitle6(title6);
		
		notice.setContent1(content1);
		notice.setContent2(content2);
		notice.setContent3(content3);
		notice.setContent4(content4);
		notice.setContent5(content5);
		notice.setContent6(content6);
		notice.setPicurl1(picurl[0] != null ? picurl[0].toString() : null);
		notice.setPicurl2(picurl[1] != null ? picurl[1].toString() : null);
		notice.setPicurl3(picurl[2] != null ? picurl[2].toString() : null);
		notice.setPicurl4(picurl[3] != null ? picurl[3].toString() : null);
		notice.setPicurl5(picurl[4] != null ? picurl[4].toString() : null);
		notice.setPicurl6(picurl[5] != null ? picurl[5].toString() : null);
		noticeService.add(notice);
		ModelAndView andView = new ModelAndView();
		// andView.addObject("file",fileName + suffix);
		andView.setViewName("redirect:/notice/list");
		return andView;
	}

	private boolean saveFile(MultipartFile file) {
		String fileName = "";
		String suffix = "";

		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyyMMddHHmmssSSS");
				fileName = format.format(new Date());

				Random random = new Random();
				for (int i = 0; i < 3; i++) {
					fileName = fileName + random.nextInt(9);
				}
				String origFileName = file.getOriginalFilename();

				suffix = origFileName.substring(origFileName.lastIndexOf("."));

				String path = request.getSession().getServletContext()
						.getRealPath("/");
				String pic_url = fileName + suffix;
				System.out.println(pic_url + "==\\\\\\\\\\\\\\");
				imagePath.add(pic_url);
				String path_url = path + "/upload/" + fileName + suffix;
				// 文件保存路径
				/*
				 * String filePath = request.getSession().getServletContext()
				 * .getRealPath("/") + "upload/" + file.getOriginalFilename();
				 */
				// 转存文件
				file.transferTo(new File(path_url));
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

}
最近下载更多
YhXyHx523  LV6 2024年4月13日
768881787  LV7 2024年4月10日
A-sunny-day  LV4 2023年6月11日
张真狗  LV9 2023年6月10日
wangjiedashuaige  LV1 2023年3月1日
pangxiangdong  LV4 2023年1月23日
2385649653  LV7 2022年11月2日
oulingqiao  LV13 2022年10月20日
LYLHYC  LV5 2022年10月12日
17683946472  LV9 2022年7月3日
最近浏览更多
597117933  LV9 4月24日
xianyu091012  LV5 2024年12月26日
qpzmla 2024年12月13日
暂无贡献等级
微信网友_6779541724286976 2024年11月15日
暂无贡献等级
WBelong  LV8 2024年9月19日
9631196311 2024年9月2日
暂无贡献等级
xiao小果  LV13 2024年8月29日
xuweisong2010  LV28 2024年6月5日
happySuperman  LV2 2024年6月4日
gidfsgs 2024年6月3日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友