public class BaseAction extends ActionSupport {
//通用字段
public int pageSize = 2;
public int nowPage = 1;
public int allCount = 1;
public int allPage = 1;
//分页HTML
public String page;
/**
* 获取 HTTP SESSION
* @return
*/
protected HttpSession getSession()
{
return ServletActionContext.getRequest().getSession();
}
/**
* 获取 HTTP 请求
* @return
*/
protected HttpServletRequest getRequest()
{
return ServletActionContext.getRequest();
}
/**
* 获取 HTTP 响应
* @return
*/
protected HttpServletResponse getResponse()
{
return ServletActionContext.getResponse();
}
/**
* 获取GET POST 参数转整型
* @param param
* @return
*/
protected int getUrlParam2Int(String param)
{
int val = 0;
String str = ServletActionContext.getRequest().getParameter(param);
str = (null==str) ? "0" : str.trim();
str = (0==str.length()) ? "0" : str;
try{
val = Integer.parseInt(str);
}catch(NumberFormatException e)
{
val = 0;
}
return val;
}
/**
* 设置分页参数
*/
private void setPageParam()
{
nowPage = getUrlParam2Int("n");
nowPage = (0==nowPage) ? 1 : nowPage;
}
/**
* 计算总页数
*/
private void countAllPage()
{
allPage = (allCount-1)/pageSize + 1;
}
/**
* 显示分页HTML
*/
protected void showPage()
{
setPageParam();
countAllPage();
//分页URL
String url = "?"+getRequest().getQueryString();
int location = url.lastIndexOf("&n=");
if(0<location)
{
url = url.substring(0,location);
}
url+="&n=";
StringBuilder sb = new StringBuilder();
sb.append("<li>共"+allPage+"页</li>");
sb.append("<li>第"+nowPage+"页</li>");
sb.append("<li><a href='"+(1<nowPage ? url+(nowPage-1) : "" )+"'>上一页</a></li>");
sb.append("<li><a href='"+(allPage>nowPage ? url+(nowPage+1) : "" )+"'>下一页</a></li>");
page = sb.toString();
}
}
清如许工作室,JAVA外包,JAVA项目开发。