首页>代码>Struts、spring、myBatis增删改查新闻后台管理案例>/共享代码/第四次备份/newsItem/src/controller/NewsController.java
package controller;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.struts2.ServletActionContext;



import net.sf.json.JSONObject;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import service.News_service;
import util.PageUtil;
import dto.News_Dto;


public class NewsController extends ActionSupport{
	  private List<News_Dto> list;
	  private String titleCon;	//新闻标题(模糊查询用)
	  private String pubManCon;	//发布人(模糊查询用)
	  private PageUtil page;
	  private News_service service;
	  private News_Dto news;  //查看新闻详细信息的news对象
	  private int id;	  
	  private News_Dto dto;   //添加新闻的dto对象
	  private File photo;
	  private String photoFileName;
	  private String photoContentType;
	  private JSONObject json;

	  	public JSONObject getJson()
    {
    	return json;
    }

	public void setJson(JSONObject json)
    {
    	this.json = json;
    }

		public int getId()
	    {
	    	return id;
	    }
      private String userid; //根据登录用户ID查询本记者发布的信息
		public void setId(int id)
	    {
	    	this.id = id;
	    }
 
	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public News_Dto getDto() {
		return dto;
	}

	public void setDto(News_Dto dto) {
		this.dto = dto;
	}


	public News_Dto getNews() {
		return news;
	}

	public void setNews(News_Dto news) {
		this.news = news;
	}

	public News_service getService() {
		return service;
	}

	public void setService(News_service service) {
		this.service = service;
	}

	public PageUtil getPage() {
		return page;
	}

	public void setPage(PageUtil page) {
		this.page = page;
	}

	public String getTitleCon() {
		return titleCon;
	}

	public void setTitleCon(String titleCon) {
		this.titleCon = titleCon;
	}

	public String getPubManCon() {
		return pubManCon;
	}

	public void setPubManCon(String pubManCon) {
		this.pubManCon = pubManCon;
	}

	public List<News_Dto> getList() {
		return list;
	}

	public void setList(List<News_Dto> list) {
		this.list = list;
	}
	/**
	 * @author 袁放
	 * 时间 2017-3-7
	 * 描述   根据标题和新闻发布人 查询所有新闻信息显示到页面上
	 */
	public String QueryAllNews(){
		try {
			 if(page==null){
				 page = new PageUtil();
			 }
			 page.setPageSize(15);
			 Map<String,String> paramap = new HashMap<String, String>();
			 paramap.put("pubManCon", pubManCon);
			 paramap.put("titleCon", titleCon);
			 if(userid!=null&&!"".equals(userid)){
		     paramap.put("userid", userid);
			 }
				 
			 list = service.service_allNews(page, paramap);
			 Map<String,Object> session= ActionContext.getContext().getSession();
			 session.put("list", list);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
		
	/**
	 * @author 袁放
	 * 时间 2017-3-7
	 * 描述 查看新闻详细信息
	 */
	public  String detailNews(){
		
		news=service.servicefindByIdNews(id);
		if(news	!= null){
	 Map<String,Object> newsdto= ActionContext.getContext().getSession();
	  newsdto.put("news", news);
		return "success";
		}else{
			return "error";
		}
	}
	
	/**
	 * @author 袁放
	 * 时间 2017-3-7
	 * 描述  增加新闻
	 */
	public String addNews(){
		try {
		InputStream  in  =  new FileInputStream(photo);  //读取封装后的前端phone
		String path=ServletActionContext.getRequest().getRealPath("/img"); //得到根目录下的文件夹
		OutputStream out =  new FileOutputStream(new File(path+"/"+photoFileName)); //实例写入流
		byte[] b =  new byte[1024];
		int res =0;
			while((res= in.read(b))!=-1){
				out.write(b);
			}
			out.close();
			in.close();
			dto.setPic_path(photoFileName);
			int flag=service.add_service_News(dto);
		 		
		if(flag==1){
		return "success";
		}else{
			return "error";
		}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
	}
	
 public String toUpdateNews(){
		news=service.servicefindByIdNews(id);
		if(news!=null){
	 Map<String,Object> newsdto= ActionContext.getContext().getSession();
	  newsdto.put("news", news);
		return "success";
		}else{
			return "error";
		}
	}
	
	/**
	 * @author 袁放
	 * 时间 2017-3-7
	 * 描述  修改信息传递到数据库更新
	 */
	public String updateNews(){
		try {
			dto.setPic_path(photoFileName);
			int flag=service.serviceUpdNews(dto);
			if(flag==1){
				return "success";
			}else{
				return "error";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
	}
	
	
	
	
	public String deleteNews()
	{
		Map<String, Object> par = new HashMap<String, Object>();
		try
		{ 			
			boolean flag = service.deleteNews(id);
			if (flag)
			{
				par.put("result", "success");
			} else
			{
				par.put("result", "fail");
			}
		} catch (Exception e)
		{
			e.printStackTrace();

		}
		json = JSONObject.fromObject(par);
		return "success";
	}

	 
	
}
最近下载更多
1029162867  LV13 2022年7月7日
52321641  LV1 2022年7月4日
阿布屋脊  LV7 2022年6月7日
猫巷^  LV1 2021年12月7日
543666826  LV33 2021年11月20日
chongmingsang  LV1 2021年6月30日
clz123456  LV6 2021年6月24日
panbiao  LV11 2021年3月31日
1435633469  LV1 2021年1月5日
江辞123456  LV1 2020年12月15日
最近浏览更多
zilatan 2023年11月25日
暂无贡献等级
飞呀飞呀飞不放  LV7 2023年8月9日
gecongkai  LV8 2023年6月22日
shuidajiao 2023年6月18日
暂无贡献等级
微信网友_6509705287716864  LV2 2023年6月8日
yangbulx  LV3 2023年6月7日
微信网友_6509053506113536  LV1 2023年6月7日
975525508  LV1 2023年6月5日
lpclpclpc  LV4 2023年5月30日
微信网友_6465435620184064  LV6 2023年5月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友