spring mvc 精简code 各种反对宏哥~~~

com.control/CategroyControl

com.xinhua.DAO/CategroyDAO

com.xinhua.entity/Categroy

 com.xinhua.execption/ExceptionClass

com.xinhua.filter/AppFilter

com.xinhua.fom/CategroyForm

com.xinhua.junit/CategroyJunit

com.xinhua.Resource/Resource

com.xinhua.service/CategroyService

com.xinhua.serviceimp/CategroyServiceImpl

package com.xinhua.DAO;

import java.util.List;

import com.xinhua.entity.NewsObject;

/**
 * 
 * @Description: TODO(焦点图管理)
 * @author 尹男男
 * @version V1.0  
 * @date 2013-11-20 下午3:45:40
 * @Package com.xinhua.DAO
 * @company 南京云恒瑞通
 */
public interface CategroyDAO {
	/**
	 * 
	
	* @Title: findPhotoByCategroyId
	
	* @Description: TODO(这里用一句话描述这个方法的作用)
	
	* @param @return    设定文件
	
	* @return List<NewsObject>    返回类型
	
	* @throws
	 */
	public NewsObject findPhotoByCategroyId(NewsObject o);

	/**
	 * 
	
	* @Title: updateCategroy
	
	* @Description: TODO(更新操作)
	
	* @param @param o    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public void updateCategroy(NewsObject o);
	/**
	 * 
	
	* @Title: updateCategroy
	
	* @Description: TODO(添加操作)
	
	* @param @param o    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public void insertCategroy(NewsObject o);
}




package com.xinhua.control;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xinhua.Resource.Tool;
import com.xinhua.Service.CategroyService;
import com.xinhua.ServiceImpl.CategroyServiceImpl;
import com.xinhua.form.CategroyForm;
public class Categroy extends NewsObject{
	/**
	 * 存放图片url地址
	 */
	private String imageUrl;
	/**
	 * 栏目名称
package com.xinhua.execption;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.alibaba.fastjson.JSON;
import com.xinhua.Resource.Json;
import com.xinhua.Resource.Tool;


public class ExceptionClass  extends SimpleMappingExceptionResolver implements Tool {

	/**
	 * 
	 * 处理异常类 根据ajax请求和跳转请求不同处理
	 */
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		// TODO Auto-generated method stub
		 String viewName = determineViewName(ex, request); 
		 
		 if (viewName != null) {// JSP格式返回  
			 
			 if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request  
	                    .getHeader("X-Requested-With")!= null && request  
	                    .getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1))) {  
				 
				    Integer statusCode = determineStatusCode(request, viewName);  
	                if (statusCode != null) {  
	                    applyStatusCodeIfPossible(request, response, statusCode);  
	                }  
	                request.setAttribute("msg", EXCEPTION_CODE);
	                
	                return new ModelAndView( "redirect:/error/404.jsp?msg=203" );
			 }else {// JSON格式返回  
	                try {  
	                	PrintWriter writer = response.getWriter();  
	                    Json json = new Json();
	                    json.setReturnNo(EXCEPTION_CODE);
	                  
	                    writer.write(JSON.toJSONString(json));  
	                    writer.flush();  
	                    writer.close();
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	                return null;  
	  
	            }  
		 }
		return super.doResolveException(request, response, handler, ex);
	}
}



public class AppFilter implements HandlerInterceptor ,Tool{
	static Logger logger = Logger.getLogger(CategroyServiceImpl.class.getName());
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		logger.debug("AppFilter:afterCompletion");
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		logger.debug("AppFilter:postHandle");
	}
	/**
	 * 比对请求是否正常 如果不正常不返回数据
	 */
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		String p = arg0.getParameter("key");
		Json json = new Json();//实例化返回对象
		Object p1 =	arg0.getAttribute("key");//获取参数
		logger.debug("AppFilter:preHandle into");
		if("".equals(Resource.getInstall().getKey()) || null == Resource.getInstall().getKey()){//如果key是null或者没有内容进行加载属性文件
			
			Config config = new Config(arg0.getSession().getServletContext()
					.getRealPath("\\WEB-INF\\")+"/Key.properties");//读取配置
			String key = config.getValue(config.getValue("frist"));
			logger.debug("key.properties="+key);
			Resource.getInstall().setKey(key);//设置key
			
		}
		if(p == null && p1 == null){
			
			json.setReturnNo(KEY_ERROR_CODE);//设置错误码
			
		}else{
		
			if(Resource.getInstall().getKey().equals(p) || Resource.getInstall().getKey().equals(p1)){
				
				return true;
			}else{
				json.setReturnNo(KEY_ERROR_CODE);//设置错误码
				
			}
			
		}
		PrintWriter print = arg1.getWriter();
		print.write(JSON.toJSONString(json));//转换 返回请求
		print.flush();//释放资源
		print.toString();
		logger.debug("AppFilter:preHandle out");
		return false;

	}

}



ublic class Resource implements Tool{
	/**
	 * 日志
	 */
	static Logger logger = Logger.getLogger(Resource.class.getName());
	/**
	 * 资源类
	 */
	private static Resource resource = null;
	
	/**
	 * 单例模式
	 */
	private Resource(){
		
	}
	/**
	 * 加密码
	 */
	private String key = "" ;
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * 初始化操作
	 */
	
	static{
		
		try {
			resource =(Resource) Class.forName("com.xinhua.Resource.Resource").newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.debug("resource exception:"+e.getMessage());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.debug("resource exception:"+e.getMessage());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.debug("resource exception:"+e.getMessage());
		}
		logger.debug("resource init:"+resource);
	}
	/**
	 * 
	
	* @Title: getInstall
	
	* @Description: TODO(返回资源类)
	
	* @param @return  返回单例
	
	* @return Resource    返回类型
	
	* @throws
	 */
	public static Resource getInstall(){
		
		return resource;
	}
	
	/**
	 * 
	
	* @Title: getOpenImage
	
	* @Description: TODO(获取启动插件图片)
	
	* @param @return    设定文件
	
	* @return NewsObject    返回类型
	
	* @throws
	 */
	public NewsObject getOpenImage(HttpSession request){
		logger.debug("com.xinhua.Resource.Resource.getOpenImage into");
		Json json = new Json();
		json.setRow(START_PATH);
		logger.debug("com.xinhua.Resource.Resource.getOpenImage out");
		return json;
		
	}
	
	/**
	 * 
	
	* @Title: getAll
	
	* @Description: TODO(获取文档)
	
	* @param @param content
	* @param @return    设定文件
	
	* @return List    返回类型
	
	* @throws
	 */
	public List getAll(String content){
		
		Document doc = Jsoup.parse(content); //html数据
		List<Node> list = doc.body().childNodes();
		return doNode(list,new ArrayList());
		
	}
	/**
	 * 
	
	* @Title: doNode
	
	* @Description: TODO(递归循环节点)
	
	* @param @param list    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public List  doNode(List<Node> list,List content ){
	
		for (Node node : list) {
			if(node.childNodeSize() != 0){
				doNode(node.childNodes(),content);//递归 如果还有子节点继续循环操作
				
			}else{
				Content  con = new Content();
				if(node.outerHtml().indexOf("<img")>=0){//如果是 img标签进行标识type
					con.setContent(node.attr("src"));//取出src 路径
					con.setType(1);//设置类型
				}else if(node.outerHtml().indexOf("<embed")>=0){
					con.setType(2);//设置图集类型
					con.setContent(node.attr("src"));//获取 视频的url地址
				}else{
					if(!("".equals(node.outerHtml().trim()))){//如果不是空的进行设置类型
						con.setType(3);//文本类型
						con.setContent(node.outerHtml().trim());
					}else{
						continue;
					}
					
				}
				content.add(con);
			}
		
		}
		return content;
		
	}
	/**
	 * 
	
	* @Title: doPage
	
	* @Description: TODO(处理分页操作)
	
	* @param @return    设定文件
	
	* @return Json    返回类型
	
	* @throws
	 */
	public Json doPage(Json json){
		int size = 0;
		int count = json.getTotal();
		
		if(count%json.getRows() > 0){
			size = count/json.getRows() + 1 ;
		}else{
			size = count/json.getRows();
		}
		if(json.getPage() - 1 > 0){
			json.setBack(json.getPage() - 1 );
		}else{
			json.setBack( 1 );
		}
		if(json.getPage()+1 > size){
			json.setNext(size);
		}else{
			json.setNext(json.getPage()+1);
		}
		return json;
	}
	
	/**
	 * 
	
	* @Title: doMap
	
	* @Description: TODO(参数处理方法到hashmap)
	
	* @param @param form
	* @param @param map    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public void doMap(NewForm form,ModelMap map){
		
		map.put("type",form.getType());
		map.put("page",form.getPage());
		map.put("rows",form.getRows());//防止重复刷新提交重定向
	}
	public static void main(String[] args) {

	}
}



public interface Tool {
	/**
	 * 系统异常
	 */
	public  final int EXCEPTION_CODE = 300;
	/**
	 * 没找到
	 */
	public  final int NOFIND_CODE=301;
	/**
	 * 更新失败
	 */
	public  final int UPDATE_CODE=302;
	/**
	 * 删除失败
	 */
	public  final int DETELE_CODE=303;
	/**
	 * 添加失败
	 */
	public  final int ADD_CODE =304;
	/**
	 * id不能唯恐
	 */
	public  final int ID_NULL=305;
	/**
	 * 栏目类别不能为空
	 */
	public  final int CATEGROY_NULL=306;
	/**
	 * 类型不能为空
	 */
	public  final int TYPE_NULL = 307;
	/**
	 * 更新成功
	 */
	public  final int UPDATE_OK_CODE = 308 ;
	/**
	 * 添加成功
	 */
	public  final int ADD_OK_CODE = 309;
	/**
	 * 删除成功
	 */
	public  final int DELTE_OK_CODE = 310;
	
	/**
	 * id传输格式不正确
	 */
	public  final int ID_ROUND = 311;
	/**
	 * key错误
	 */
	public  final int KEY_ERROR_CODE=312;
	/**
	 *用户名不能为空
	 */
	public  final int USER_NULL = 313;
	/**
	 * 密码不能为空
	 */
	public  final int PASSWORD_NULL = 314;
	/**
	 *帐号或者密码错误
	 */
	public  final int USER_PASSWROD_ERROR  = 315;
	/**
	 *登录成功
	 */
	public  final int SESSION_CODE = 316;
	
	/**
	 * 确认密码不能为空
	 */
	public  final int REPASSWORK_CODE = 317;
	/**
	 * 修改密码成功
	 */
	public final int RESET_OK_CODE = 318;
	
	/**
	 * 确认密码和密码不一致
	 */
	public final int REPASSWORD_ERROR = 319;
	/**
	 * app启动图片
	 */
	public  final String START_PATH ="/file/open/open.jpg";
	/**
	 * 公司新闻是0  媒体据聚焦是1 图集是2 其他是3  
	 * 公司新闻
	 */
	public  final int COMPNAY_TYPE = 0;
	/**
	 * 媒体聚焦
	 */
	public  final int MEDIA_TYPE = 1;
	/**
	 * 获取图集数据
	 */
	public  final int PHOTO_TYPE=2;
	
	
	
	/**
	 * 实体类标识字段
	 */
	public final String NEW_BEAN = "new_Bean";
	/**
	 * 实体类标识字段
	 */
	public final String NEW_BEAN_LIST = "new_Bean_List";
	/**
	 * session 用户参数
	 */
	public final String SESSION_USER = "user";
	
	/**
	 * 焦点图实体bean
	 * 
	 */
	public final String CATEGROY_BEAN_LIST = "categroyList";
	/**
	 * json返回数据
	 */
	public final String JSON_BEAN = "json";
	/**
	 * 普通文件存放目录
	 */
	public  final String COMMON_PATH ="/file/common/";



/**
	 * 
	
	* @Title: findPhotoByCategroyId
	
	* @Description: TODO(根据类型查找焦点图)
	
	* @param @return    设定文件
	
	* @return List<NewsObject>    返回类型
	
	* @throws
	 */
	public NewsObject findPhotoByCategroyId(NewsObject o) throws Exception;
	
	/**
	 * 
	
	* @Title: insertByCategroyId
	
	* @Description: TODO(如果不存在添加操作)
	
	* @param @param o
	* @param @return
	* @param @throws Exception    设定文件
	
	* @return NewsObject    返回类型
	
	* @throws
	 */
	public NewsObject insertByCategroyId(NewsObject o) throws Exception;



public class CategroyServiceImpl implements CategroyService {
	/**
	 * categroy 持久层操作
	 */
	@Autowired
	private CategroyDAO categroyMapper;
	static Logger logger = Logger.getLogger(CategroyServiceImpl.class.getName());
	public CategroyDAO getCategroyMapper() {
		return categroyMapper;
	}

	public void setCategroyMapper(CategroyDAO categroyMapper) {
		this.categroyMapper = categroyMapper;
	}
	/**
	 * 
	
	* @Title: findPhotoByCategroyId
	
	* @Description: TODO(根据类型查找焦点图)
	
	* @param @return    设定文件
	
	* @return List<NewsObject>    返回类型
	
	* @throws
	 */
	public NewsObject findPhotoByCategroyId(NewsObject o) {
		// TODO Auto-generated method stub
		logger.debug("com.xinhua.ServiceImpl.CategroyServiceImpl.findPhotoByCategroyId into");
		Json json = new  Json();
		NewsObject list = categroyMapper.findPhotoByCategroyId(o);
		json.setRow(list);
		logger.debug("com.xinhua.ServiceImpl.CategroyServiceImpl.findPhotoByCategroyId out");
		return json;
	}
	/**
	 * 添加操作
	 */
	public NewsObject insertByCategroyId(NewsObject o) throws Exception {
		// TODO Auto-generated method stub
		logger.debug("com.xinhua.ServiceImpl.CategroyServiceImpl.insertByCategroyId into");
		Json json = new  Json();
		NewsObject newsObject =  categroyMapper.findPhotoByCategroyId(json);
		if(newsObject == null){
			categroyMapper.insertCategroy(o);
		}else{
			o.setId(newsObject.getId());
			categroyMapper.updateCategroy(o);
		}
		logger.debug("com.xinhua.ServiceImpl.CategroyServiceImpl.insertByCategroyId out");
		return json;
	}
	
}



*/ private String name; /** * 字符串新闻id */ private String news_id; /** * 字符串type */ private String types; /** * 公司新闻是0 媒体据聚焦是1 图集是2 其他是3 */ private int type; public int getType() { return type; } public void setType(int type) { this.type = type; } public String getNews_id() { return news_id; } public void setNews_id(String news_id) { this.news_id = news_id; } public String getTypes() { return types; } public void setTypes(String types) { this.types = types; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getName() { return name; } public void setName(String name) { this.name = name; } }



@Controller @RequestMapping("/categroy") public class CategroyControl implements Tool { @Autowired private CategroyService categroyService; static Logger logger = Logger.getLogger(CategroyControl.class.getName()); public CategroyService getCategroyService() { return categroyService; } public void setCategroyService(CategroyService categroyService) { this.categroyService = categroyService; } /** * 2.3.8. 获取焦点图列表接口 * @throws Exception */ @ResponseBody @RequestMapping("/app/findPhotoByCategroyId.do") public String findPhotoByCategroyId(CategroyForm form) throws Exception{ logger.debug("com.xinhua.control.CategroyControl.findPhotoByCategroyId into"); String json = JSON.toJSONString(categroyService.findPhotoByCategroyId(form));//查询焦点图返回界面 logger.debug("com.xinhua.control.CategroyControl.findPhotoByCategroyId out"); return json; } /** * * @Title: findPhotoByCategroyIdWeb * @Description: TODO(根据类型不同加载不同焦点图进行操作) * @param @param form * @param @param map * @param @return * @param @throws Exception 设定文件 * @return String 返回类型 * @throws */ @RequestMapping("/web/findPhotoByCategroyId.do") public String findPhotoByCategroyIdWeb(CategroyForm form,ModelMap map) throws Exception{ logger.debug("com.xinhua.control.CategroyControl.findPhotoByCategroyId into"); map.put("CATEGROY_BEAN_LIST",categroyService.findPhotoByCategroyId(form));//根据类型查询数据 if(form.getType() == 1){//根据不同类型进行跳转 }else if(form.getType() == 2){ }else if(form.getType() == 3){ }else{ } logger.debug("com.xinhua.control.CategroyControl.findPhotoByCategroyId out"); return ""; } /** * * @Title: insertPhotoByCategroyId * @Description: TODO(添加焦点图分类) * @param @param form * @param @param map * @param @return * @param @throws Exception 设定文件 * @return String 返回类型 * @throws */ @RequestMapping("/web/insertPhotoByCategroyId.do") public String insertPhotoByCategroyId(CategroyForm form,ModelMap map) throws Exception{ return ""; } }




你可能感兴趣的:(spring mvc 精简code 各种反对宏哥~~~)