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

📄 dlogtrackbackaction.java

📁 JSP开发的博客管理系统,使用struts+hibernate+AJAX技术
💻 JAVA
字号:
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package dlog4j.action;

import java.util.Date;

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

import net.sf.hibernate.Session;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dlog4j.formbean.TrackBackForm;

/**
 * 用于处理TrackBack的请求
 * @author Winter Lau
 */
public class DlogTrackBackAction extends DlogActionBase {

	/**
	 * 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception 
	{
		TrackBackForm tbf = (TrackBackForm)form;
		ActionErrors errors = new ActionErrors();
		Session ssn = null;
		String msg = validate(tbf);
		if(msg==null){
			try{
				ssn = getSession();			
				tbf.setRefTime(new Date());
				tbf.setRemoteAddr(req.getRemoteAddr());
				ssn.save(tbf);
			}catch(Exception e){
				getServlet().log("DlogTrackBackAction.doDefault", e);
				msg = e.getMessage();
			}finally{
				commitSession(ssn,true);
			}
		}
		String xml = getResponse(msg!=null, msg);
		res.getWriter().print(xml);
		return null;
	}
	

    /**
     * 验证输入的有效性
     */
    protected String validate(TrackBackForm form) {
        if(StringUtils.isEmpty(form.getUrl())) 
            return "url is empty";
        else
        if(form.getLog_id()<0)
            return "Illegal value of log_id";
        else
        if(StringUtils.isEmpty(form.getBlog_name()))
            return "Blog_name is empty";
        else
        if(StringUtils.isEmpty(form.getTitle()))
            return "Title is empty";
        return null;
    }
    
	
	/**
	 * 得到TrackBack的反馈信息
	 * @param error
	 * @param msg
	 * @return
	 */
	protected String getResponse(boolean error, String msg){
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		xml.append("<response>");
		if(error){
			xml.append("<error>1</error>");
			xml.append("<message>");
			xml.append(msg);
			xml.append("</message>");
		}
		else
			xml.append("<error>0</error>");
		xml.append("</response>");
		return xml.toString();
	}
	
}

⌨️ 快捷键说明

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