jsp过程中遇到的困难
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、分页
用到的方法:
Public List findListNews(int page){
List list=new ArrayList();
Int rowBegin=0;
If(page>1){
rowBegin=5*(page-1);
}
String sql="select top 5 * from news where id not in (select top "+rowBegin+" id from news)";
try {
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
News news=new News();
news.setContent(rs.getString("content"));
news.setId(rs.getInt("id"));
news.setModifytime(rs.getString("modifyti me"));
news.setPulishtime(rs.getString("publisht
ime"));
news.setTitle(rs.getString("title"));
news.setUid(rs.getInt("uid"));
list.add(news);
注:对象中的所有列都必须要set出来,包括主键
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
一共多少条的方法:
public int findCountNews() {
int count=0;
try {
conn=this.getConn();
String sql="select count(*) from news ";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
count=rs.getInt(1);
注:上面这行是固定的。
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
一共多少页的方法:
public int findCountPage() {
int counts=0;
try {
conn=this.getConn();
String sql="select count(*) from news ";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
counts=rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
counts=counts%5==0?counts/5:counts/5+1;
return counts;
}
在JSP页面里写入:
if(pages==null){
pages="1";
}
int p=Integer.parseInt(pages);
int counts=newsdao.findCountPage();
int count=newsdao.findCountNews();
href="index.jsp?page=<%=p==counts?counts:p+ 1 %>">下一页
共<%=counts %>页
跳到
onBlur="aa()" />页
2、Onblur事件的用法
function aa(){
var
page=document.getElementById("tiao").value;
if(page.length==0){
alert("请输入页数");
page=1;
}else if(isNaN(page)){
alert("输入页数必须是数字");
page=1;
}
window.location.href="index.jsp?page="+page ;
}
3、截取字符串:新闻标题(要求最多显示6个字)
String title=news.getTitle();
<%if(title.length()<6){%> <%=title%>
<%}else{%> <%=title.substring(0,6)%> <%} %>
4、删除每条新闻功能,一次只能删一条,删除时弹出窗口“确定要删除吗?”删除成功后弹出窗口“删除成功”
function shan(){
if(confirm("确定删除吗?")){
location.href="doIndex.jsp?id="+<%=news.get Id()%>;
}else{
location.href="index.jsp?id="+<%=news.getId ()%>;
}
}