javaWeb 分页&插件

本文介绍了JavaWeb中的分页实现,包括分页的SQL代码和DAO方法。同时,讲解了CKeditor的引入和使用步骤,以及SmartUpload组件的特点,如简单易用、上传文件控制等,并提及了环境准备和常用方法。
摘要由CSDN通过智能技术生成

分页

思路:2.统计总行数 3.设置每页的数据条数 4.用总行数除以每页的数据条数得到总页数

分页

sql代码:pageIndex:第几页=当前页码
pageSize:每页多少条记录 

select * from(
select a.*,rownum as rid from(
select * from news279 order by nid desc
) a
) b where b.rid between ? and ?
(pageIndex-1)*pageSize+1-----pageIndex*pageSize
分页的dao方法 代码展示

/**
	 * 分页
	 * @param pageIndex第几页 当前页码
	 * @param pageSize 几条数据
	 * @return 新闻集合
	 */
	
	public List<News> getAll( int pageIndex,int pageSize){
		List<News> ls=new ArrayList<>();
		
		int a=(pageIndex-1)*pageSize+1;
		int b=pageIndex*pageSize;
		try {
			con=DBHelper.getCon();
			String sql="select * from(\r\n" + 
					" select a.* ,rownum as rid from(\r\n" + 
					"  select nid,ntitle,nauthor from news0330 order by nid desc\r\n" + 
					"   ) a\r\n" + 
					" ) b where b.rid between ? and ?";
			ps=con.prepareStatement(sql);//获得执行对象
			//给占位符赋值
			ps.setInt(1, a);
			ps.setInt(2, b);
			rs=ps.executeQuery();//获得结果集
//			遍历结果集
			while(rs.next()) {
				//实例化一个新闻对象
				News n=new News();
				//给对象赋值
				n.setNid(rs.getInt(1));
				n.setNtitle(rs.getString(2));
				n.setNauthor(rs.getString(3));
				//加到集合中  重点
				ls.add(n);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myclose(con, ps, rs);
		}
		
		
		return ls;
		
	}

插件

一、CKeditor

1.引入CKeditor

2.在有需求的界面引入

<script  src="../ckeditor/ckeditor.js"></script>

3.在有需要的地方替换

 <script>
            CKEDITOR.replace("ncontent");
        </script>


二、smartupload 组件

专门用于实现文件上传及下载的免费组件

SmartUpload组件特点 使用简单:编写少量代码,完成上传下载功能 能够控制上传内容

能够控制上传文件的大小、类型 缺点:目前已停止更新服务

环境准备:

使用SmartUpload组件需要在项目中引入jspsmartupload.jar文件

将jspsmartupload.jar 添加到WEB-INF\lib目录下

需要设置表单的enctype属性:<form enctype="multipart/form-data" method="post">

常用方法

File类:

          方法名称

            说  明

saveAs(String destFilePathName)

将文件保存,参数destFilePathName是保存的文件名

isMissing( )

判断用户是否选择了文件,即对应表单项是否为空,返回值为boolean类型

public String getFieldName( )

获取表单中当前上传文件所对应的表单项的名称

public String getFileName( )

获取上传文件的文件名称,不包含路径

Files类

          属性名称

            说  明

public int getCount()

取得文件上传的数目

public File getFile(int index)

取得指定位置的File文件对象

public long getSize()

取得上传文件的总长度

public Collection getCollection()

将所有上传文件对象以Collection的形式返回

代码展示:

单文件自动上传
自动找Web项目的根目录
/* String path="images/";
//获取文件
File file= su.getFiles().getFile(0);
//判断
if(file.isMissing()==false){//上传了文件
	//设置文件的编码方式
	file.setCharset("utf-8");
	path+=file.getFileName();//拼接上原有的文件名
	file.saveAs(path, SmartUpload.SAVE_VIRTUAL);
}
out.print(path); */


手动上传
手动找Web项目的根目录
/* String path="images/";
//E:\JavaWeb\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Web06\
String webPath=this.getServletContext().getRealPath("/");
//获取文件
File file= su.getFiles().getFile(0);
//判断
if(file.isMissing()==false){//上传了文件
	//设置文件的编码方式
	file.setCharset("utf-8");
	path+=file.getFileName();//拼接上原有的文件名
	file.saveAs(webPath+path);//手动找根目录
}
out.print(webPath+path);

//将path放入数据库对应列即可

//拿表单的其他值
  Request req= su.getRequest();
String name=req.getParameter("iname");
out.print("用户名"+name); */



 多文件上传
 //先拿到所有的文件
 Files fs=su.getFiles();
 //拿到总数
 for(int i=0;i<fs.getCount();i++){
	 //依次拿到每个文件  依次进行上传
	File file= fs.getFile(i);
	String path="images/";
	
	
	//判断
	if(file.isMissing()==false){//上传了文件
		//设置文件的编码方式
		file.setCharset("utf-8");
		path+=file.getFileName();//拼接上原有的文件名
		file.saveAs(path, SmartUpload.SAVE_VIRTUAL);
	}
	out.print(path+"<br />");
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值