⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newaction.java

📁 使用WEBWORK,SPRING,HIBERNATE编写的简单的添加
💻 JAVA
字号:
package com.tech.action;

import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.common.Config;
import com.common.action.BaseAction;
import com.common.dao.DaoHelper;
import com.common.hibernate.IdGenerator;
import com.common.util.PaginationSupport;
import com.tech.model.News;
import com.tech.service.NewService;

public class NewAction extends BaseAction {
	private Log log = LogFactory.getLog(NewAction.class);
	private News news;
	private List list;
	private PaginationSupport  page;
	private NewService newService;
	private DaoHelper daoHelper;
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public List getList() {
		return list;
	}
	public PaginationSupport getPage() {
		return page;
	}
	public void setNewService(NewService newService) {
		this.newService = newService;
	}
	public void setDaoHelper(DaoHelper daoHelper) {
		this.daoHelper = daoHelper;
	}
	/**
	 * @return
	 */
	public String add(){
		this.setActionName(Config.ADD);
		try {
			if (news == null) {
				news = new News();
			}
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams());
			return INPUT;
		} catch (Exception e) {
			log.error("add " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			return LIST;
		} 
	}
	
	
	public String modify(){
		this.setActionName(Config.MODIFY);
		try {
			if (news == null || news.getId() == null) {
				setErrMessage("修改对象的ID为空!");
				return ERROR;
			}
			
			setNews(newService.getEntity(getNews().getId()));
			
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams());
			return INPUT;
		} catch (Exception e) {
			log.error("modify " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			return LIST;
		} 
	}
	
	public String remove(){
		try {
			if (news == null || news.getId() == null) {
				setErrMessage("修改对象的ID为空!");
				return ERROR;
			}
			
			newService.removeEntity(news);
			setActionMessage(Config.DELETE_SUCCESS_MESSAGE);
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.DELETE_SUCCESS_MESSAGE,"GBK"));
			return LIST;
		} catch (Exception e) {
			log.error("remove " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			return LIST;
		} 
	}
	
	public String save(){
		try {
			if (getNews().getId() != null) {
				this.setAction(this.ACTION_UPDATE);
				this.setActionName(Config.MODIFY); 
				newService.updateEntity(news);
				setActionMessage(Config.MODIFY_SUCCESS_MESSAGE);
				page = new PaginationSupport(getMap());
				page.setContextPath(this.getContextPath());
				this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.MODIFY_SUCCESS_MESSAGE,"GBK"));				
				return LIST;
			} else {
				this.setAction(this.ACTION_ADD);
				this.setActionName(Config.ADD); 
				news.setId(new Long(IdGenerator.getId()));
				newService.saveEntity(news);
				setActionMessage(Config.ADD_SUCCESS_MESSAGE);
				page = new PaginationSupport(getMap());
				page.setContextPath(this.getContextPath());
				this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.ADD_SUCCESS_MESSAGE,"GBK"));
				return LIST;
			}
		} catch (Exception e) {
			log.error("save " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			if(this.getAction().equals(this.ACTION_ADD)){
				 //当前的操作异常时添加异常
				  news.setId(null);
			  }
			  return INPUT;
		} 
	}
	
	public String detail(){
		try {
			setNews(newService.getEntity(getNews().getId()));
			try {
				getNews().getId();
			} catch (Exception ex) {
				throw new Exception("此信息已经不存在,请刷新数据");
			}
			return INPUT;
		} catch (Exception e) {
			log.error("detail " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			return LIST;
		} 
	}
	
	public String list(){
		try {
						
			page = new PaginationSupport(getMap());
			page.setUrl(this.getUrl());
			page.setContextPath(this.getContextPath());
			page.setParam("pageSize","15");
			this.setUrlParams(page.getParams());
			
			String countSQL="select count(t.id) count from news t where 1=1";
			String countAlias="count";
			String sql="select {t.*} from news t where 1=1 order by t.id desc";
			String sqlAlias="t";
			page=daoHelper.findBySQL(page, countSQL, countAlias, sql, sqlAlias,null, News.class, false);
			list=page.getItems();
			this.setRecordCount(page.getTotalCount());	
			
			return this.SUCCESS;
		} catch (Exception e) {
			log.error("list : " + e.toString());
			if(e.getMessage() == null){
				setErrMessage(e.toString());
			} else {
				setErrMessage(e.getMessage());
			}
			return LIST;
		} 
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -