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

📄 topiclistaction.java

📁 java struts source code
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package  emptyprj;

import emptyprj.topicListDAO;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
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 org.apache.struts.action.ActionServlet;
import org.apache.struts.util.ModuleException;
import org.apache.struts.util.MessageResources;
import javawebstudio.struts_db.DbAction;
//import javawebstudio.struts_db.DbMySQLAction;  //如果是MySQL,请用这一句。

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
import java.util.*;
import java.text.*;


////////////
import org.apache.struts.upload.FormFile;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

//Struts.net支持类
import javawebstudio.struts_db.NetAction;
import javawebstudio.struts_db.NetForm;

//public final class topicListAction extends DbMySQLAction  //如果是MySQL,请用这一句。
public final class topicListAction extends DbAction
{
    // 变量定义:
    private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");
        
    //定义方法find,查找数据:
    public ActionForward find(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception 
    {
         // 首先定义属性和变量以及设置初值    	
    	  NetForm m_topicListForm = (NetForm) form;
         topicListDAO m_topicListDAO = new topicListDAO();  //定义DAO对象,用于实现数据库的各种操作

      	  String action=request.getParameter("action");  //动作类型    		
      	  String search=request.getParameter("search");  //数据库查找方式
      	  String expression=request.getParameter("expression");    //参数,这里用于保存记录(行)的ID数据
      		
      	  if(action==null)action="find";
         if(search==null)search="UNsearch";
         if(expression==null)expression="";
         //action=toChinese(action);  //处理中文问题,实现编码转换,如果是用于MySQL,请加上这之一句
         expression=toChinese(expression);  //处理中文问题,实现编码转换,如果是用于MySQL,请去掉之一句
      	          	    
      	  ///////////////查找数据库
      	  //<sqlstr>
      		String sql="SELECT * FROM bbs";
      		//</sqlstr>

         ////SQL字符处理
      	  if("search".equals(search))
      	  {
      		    //<search>
      		       sql+=" where (parentID=";
                     if("NULL".equals(expression)||"".equals(expression))
                     {
                          sql+=m_topicListForm .getInt("ID");
                           m_topicListForm.setInt("parentID",m_topicListForm .getInt("ID"));
                      }
      		          else
      		          {
      		             sql+=expression;
      		              m_topicListForm.setInt("parentID",java.lang.Integer.parseInt(expression,10));
      		          }	     
      		        sql+=")";
      		        sql+=" AND (child=0)";   
      		        sql+=" order by datetime DESC";   	
      		    //</search>
      	  }

         //<分页>
     	  m_topicListDAO.setLength(20);//设置每页显示记录数
     	  int ipage;//当前页
     	  try 
     	  {
     	      String page=request.getParameter("page");//分页参数,读取请求的当前页
     	      ipage=java.lang.Integer.parseInt(page,10);
     	  }
     	  catch (Exception e) 
         {
             ipage=m_topicListForm.getPage();
         } 
         if(ipage<1)ipage=1;
         Collection col = m_topicListDAO.findSQL(sql,ipage);//通过DAO对象查询数据
     	  request.setAttribute("topicList",col);  //保存数据	
     	 	
     	  String pagestr=m_topicListDAO.getPagestr(ipage);//分页字符串,分页内容
         if(!"search".equals(search))    //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"
      	  {
              pagestr=pagestr.replaceAll("search=search&","");
             
      	  }
      	  pagestr=pagestr.replace("?","?expression="+expression+"&");
      	  
     	  m_topicListForm.set("pagestr",pagestr);   
          m_topicListForm.set("action",action);	 
          m_topicListForm.setString("image","http://");	
     	  request.setAttribute("topicListForm",m_topicListForm);
         //</分页>

         m_topicListDAO.Close();
         //转发到输出页面,return mapping.findForward("success");
        
          java.util.HashMap newValues = new java.util.HashMap();
          newValues.put("returnID",m_topicListForm.getString("parentID"));
          request.setAttribute("returnID",newValues);

         return new ActionForward(mapping.getInput());
   }
    
     //定义方法remove,删除记录:
    public ActionForward remove(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception 
    {
	  // 首先定义属性和变量以及设置初值
	  NetForm m_topicListForm = (NetForm) form;	
      	  topicListDAO m_topicListDAO = new topicListDAO();  //定义DAO对象,用于实现数据库的各种操作

         String expression=request.getParameter("expression");    //参数,这里用于保存记录(行)的ID数据
         if(expression==null)expression="";
         expression=toChinese(expression);  //处理中文问题,实现编码转换
      	
         m_topicListDAO.removeID(expression);      
         m_topicListDAO.Close();	
         ///////////////查找数据库
         return find( mapping,form,request,response);
   }
    
     //定义方法update,更新记录:
    public ActionForward update(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception 
    {
    	  // 首先定义属性和变量以及设置初值
    	  NetForm m_topicListForm = (NetForm) form;	
         topicListDAO m_topicListDAO = new topicListDAO();  //定义DAO对象,用于实现数据库的各种操作

         String expression=request.getParameter("expression");    //参数,这里用于保存记录(行)的ID数据
         if(expression==null)expression="";
         expression=toChinese(expression);  //处理中文问题,实现编码转换
      	   
         m_topicListDAO.update(m_topicListForm ,expression);      	     
         m_topicListDAO.Close();	
        ///////////////查找数据库
        return find( mapping,form,request,response);
   }
    
     //定义方法insert,添加新主题:
    public ActionForward insert(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception 
    {
         // 首先定义属性和变量以及设置初值
    	  NetForm m_topicListForm = (NetForm) form;	
      	  topicListDAO m_topicListDAO = new topicListDAO();  //定义DAO对象,用于实现数据库的各种操作

          HttpSession session = request.getSession();
          String username=(String)session.getAttribute(Constants.USER_NAME);
          m_topicListForm.setString("username",username);
          
          SimpleDateFormat lformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
          Calendar now = Calendar.getInstance(); 
          String nowstr = lformat.format(now.getTime()); 
          m_topicListForm.setString("datetime",nowstr);
          m_topicListForm.setString("IP",request.getRemoteAddr());

         m_topicListDAO.create(m_topicListForm );      	     
         m_topicListDAO.Close(); 	
        ///////////////查找数据库
        return find( mapping,form,request,response);
   }

//topicList,列出指定主题下的所有回复
public ActionForward topicList(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception 
    {
         // 首先定义属性和变量以及设置初值    	
    	  NetForm m_topicListForm = (NetForm) form;
         topicListDAO m_topicListDAO = new topicListDAO();  //定义DAO对象,用于实现数据库的各种操作

      	  String action=request.getParameter("action");  //动作类型    		
      	  String search=request.getParameter("search");  //数据库查找方式
      	  String expression=request.getParameter("expression");    //参数,这里用于保存记录(行)的ID数据
      		
      	  if(action==null)action="find";
         if(search==null)search="UNsearch";
         if(expression==null)expression="";
         //action=toChinese(action);  //处理中文问题,实现编码转换,如果是用于MySQL,请加上这之一句
         expression=toChinese(expression);  //处理中文问题,实现编码转换,如果是用于MySQL,请去掉之一句
      	          	    
      	  ///////////////查找数据库
      	  //<sqlstr>
      		String sql="SELECT * FROM bbs as topic";
      		//</sqlstr>

         ////SQL字符处理
      	  if("search".equals(search))

⌨️ 快捷键说明

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