springmvc 参数传递问题
问题描述:
输入查询条件查询能够显示第一页查询结果,但是点击翻页出错,
Required String parameter 'filename' is not present
参数无法获取,不知什么原因,求大神
jsp页面代码
<script language="javascript" type="text/javascript" src="<%=request.getContextPath() %>/My97DatePicker/WdatePicker.js"></script>
<%
HttpSession s = request.getSession();
%>
<%String s1=s.getAttribute("pagenum").toString();
String s2=s.getAttribute("pagesize").toString();
String s3=s.getAttribute("pagenow").toString();
String s4=s.getAttribute("num").toString();
int pagenum=Integer.parseInt(s1);
int pagesize=Integer.parseInt(s2);
int pagenow=Integer.parseInt(s3);
int pagenow2=Integer.parseInt(s3)+1;
int listnum=Integer.parseInt(s4);
int liststep=5;//显示页码数
if (pagenum < pagenow) {
pagenow = pagenum;//如果分页变量大总页数,则将分页变量设计为总页数
}
if (pagenow <=0) {
pagenow = 1;//如果分页变量小于1,则将分页变量设为1
}
else if (pagenow >0) {
pagenow = pagenow+1;//如果分页变量小于1,则将分页变量设为1
}
int listbegin = (pagenow - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
if (listbegin < 1) { //当前页-(总显示的页列表数/2)
listbegin = 1;
}
int listend = (pagenow + liststep / 2)-1;//分页信息显示到第几页//当前页+(总显示的页列表数/2)
if (listend > pagenum) {
listend = pagenum + 1;
}
if(listnum<=10)
{
listend=1;}
if(listend-listbegin+1>pagenum)
{
listend=pagenum;}
if(listend>pagenum)
{
listend=pagenum;}
%>
<h4 align="center">上传任务管理</h4>
${pageaction}
<form action="upload/searchByFileName2/<%=0 %>" method="post">
<table border="1" cellspacing="0" align="center">
<tr>
<td colspan="2" align="center">查询</td>
</tr>
<tr>
<td width="450px" align="center">文件名称:<input type="text" name="filename" value=${filename} ></td>
<td width="450px" align="center">
文件类型:<select name="filetype" >
<option value="txt">txt</option>
<option value="jpg">jpg</option>
</select>
</td>
</tr>
<tr>
<td align="center" >
上传时间从:<input name="start" type="text" style="width: 180px;" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})"> </td>
<td align="center">
至:<input name="stop" type="text" style="width:180px;" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" /> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="查询" style="width:150px;height: 25px" />
<input type="reset" value="重置" style="width:150px;height: 25px" >
</td>
</tr>
</table>
<font color="red"> ${errormessage}</font>
<br>
<table border="1px" cellpadding="0" cellspacing="0" align="center">
<tr>
<th ><input type="checkbox" name="all" value="ds"/></th>
<th >id</th>
<th >文件名</th>
<th >文件大小</th>
<th >文件类型</th>
<th >上传时间</th>
<th >文件描述</th>
<th >执行时间</th>
<th >运行结果</th>
<th >操作</th>
</tr>
<c:forEach var="upload" items="${upload}">
<tr>
<td width="45px"><input type="checkbox" value="${upload.id}" name="single"/></td>
<td width="45px">${upload.id}</td>
<td width="235px">${upload.filename}</td>
<td>${upload.filesize}</td>
<td>${upload.filetype}</td>
<td>${upload.uploaddate}</td>
<td>${upload.des}</td>
<td>${upload.runtime}</td>
<td>${upload.result}</td>
<td> <a href="">编辑</a> <a href="">删除</a><br /> </td>
</tr>
</c:forEach>
<tr>
<td align="right" colspan="10">
当前第:<%=pagenow%>页/共<%=pagenum %>页 显示<%=pagesize %>条/共<%=listnum %>条
</td>
</tr>
<tr>
<td align="center" colspan="10">
<% if (pagenow > 1) {
%> <a
href="${pageContext.request.contextPath}/${pageaction}/<%=pagenow-2 %>.html"
style="text-decoration:none"> <%
out.println("上一页");
}%> </a> <%for(int i=listbegin;i<=listend;i++)
{
pagenow=i;
%>
<a href="${pageContext.request.contextPath}/${pageaction}/<%=pagenow-1%>.html"
style="text-decoration:none" >
<%
if(i==pagenow2)
out.println("<font color='red'>["+i+"]</font> ");
else
out.println("["+i+"] ");
}
%>
</a>
<% if (pagenow !=pagenum)
{ %> <a
href="${pageContext.request.contextPath}/${pageaction}/<%=pagenow2 %>.html"
style="text-decoration:none"> <%
if(listnum!=0)
out.println("下一页");
}%> </a>
</td>
</tr>
</table>
</form>
</body>
</html>
jsp页面
controller代码
@RequestMapping(value="/searchByFileName2/{pagenow}")
public ModelAndView searchByFilename(HttpServletRequest request,@RequestParam String filename,@RequestParam String filetype,@RequestParam String start,@RequestParam String stop, ModelMap mod) throws ParseException
{
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext application = webApplicationContext.getServletContext();
String n=(String) application.getAttribute("name");
System.out.println("sssssssssss "+n);
System.out.println("***************filename"+filename);
System.out.println("***************start"+start);
System.out.println("***************stop"+stop);
System.out.println("***************filetype"+filetype);
ModelAndView modelAndView=new ModelAndView("user/uploadWithPage");
if(start.equals("")&!stop.equals(""))
{
mod.addAttribute("errormessage", "上传日期不能为空");
return modelAndView;
}
if(!start.equals("")&stop.equals(""))
{
mod.addAttribute("errormessage", "上传日期不能为空");
return modelAndView;
}
Page<Upload> uploa=null;
if(filename.equals("")&start.equals("")&stop.equals(""))
{
uploa=uploadService.findUploadByUsernameAndFiletype(n, filetype, 0, pagesize);
}
else if(start.equals("")&stop.equals(""))
{
uploa=uploadService.findUploadByUsernameAndFilenameAndFiletype(n, filename, filetype, 0, pagesize);
}
else if(filename.equals("")&!start.equals("")&!stop.equals(""))
{
uploa=uploadService.findUploadByUsernameAndFileTypeAndUploaddate(n, filetype, start, stop, 0, pagesize);
}
else if(!filename.equals("")&!start.equals("")&!stop.equals(""))
{
uploa=uploadService.findUploadByAll(n, filename, filetype, start, stop, 0, pagesize);
}
else
{
mod.addAttribute("errormessage", "上传日期不能为空");
}
//当前页查询的记录数
pageitems=uploa.getNumberOfElements();
//查询出的总记录数
itemsnum=(int)uploa.getTotalElements();
//总页数
pagenum=uploa.getTotalPages();
HttpSession session = getSession();
session.setAttribute("pagenum", pagenum);
session.setAttribute("pagesize", pagesize);
session.setAttribute("num", itemsnum);
session.setAttribute("pagenow", pagenow);
session.setAttribute("filename", filename);
session.setAttribute("filetype", filetype);
session.setAttribute("start", start);
session.setAttribute("stop", stop);
mod.addAttribute("filename", filename);
List<Upload> u = uploa.getContent();
if(u.size()==0)
{
mod.addAttribute("errormessage", "该文件不存在");
return modelAndView;
}
mod.addAttribute("pageaction", "upload/searchByFileName2");
modelAndView.addObject("upload",u);
return modelAndView;
}
答
今天有时间把问题解决办法说一下,可能方法有些笨
第一:要保存查询的条件,也就是文本框中的值,查询的时候controller中用mod存,返回时jsp用${}取给value
第二:在上面已经完成的的条件下,点击翻页事件,调用 form的submit,并且修改form的action,主要是翻页的页码,由于查询条件已经保存,所 以能够翻页。
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'uploadWithPage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script language="javascript" type="text/javascript"
src="<%=request.getContextPath() %>/My97DatePicker/WdatePicker.js"></script>
<%
HttpSession s = request.getSession();
%>
<%String s1=s.getAttribute("pagenum").toString();
String s2=s.getAttribute("pagesize").toString();
String s3=s.getAttribute("pagenow").toString();
String s4=s.getAttribute("num").toString();
int pagenum=Integer.parseInt(s1);
int pagesize=Integer.parseInt(s2);
int pagenow=Integer.parseInt(s3);
int listnum=Integer.parseInt(s4);
int liststep=5;//显示页码数
if (pagenum < pagenow) {
pagenow = pagenum;//如果分页变量大总页数,则将分页变量设计为总页数
}
if (pagenow <=0)
{
pagenow = 1;//如果分页变量小于1,则将分页变量设为1
}
else
{
pagenow = pagenow+1;//如果分页变量小于1,则将分页变量设为1
}
int listbegin = (pagenow - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
if (listbegin < 1) { //当前页-(总显示的页列表数/2)
listbegin = 1;
}
int listend = (pagenow + liststep / 2)-1;//分页信息显示到第几页//当前页+(总显示的页列表数/2)
if (listend > pagenum) {
listend = pagenum + 1;
}
if(listnum<=10)
{
listend=1;}
if(listend-listbegin+1>pagenum)
{
listend=pagenum;}
if(listend>pagenum)
{
listend=pagenum;}
int pagenow2=pagenow;
%>
<h4 align="center">上传任务管理</h4>
<form action="upload/search/<%=0%>" method="post" id="search">
<table border="1" cellspacing="0" align="center" >
<tr>
<td colspan="2" align="center">查询</td>
</tr>
<tr>
<td width="450px" align="center">文件名称:<input type="text" value="${filename }"
name="filename" >
</td>
<td width="450px" align="center">文件类型:<select name="filetype">
<option placehoder="$filetype">${filetype}</option>
<option value="txt">txt</option>
<option value="jpg">jpg</option>
</select></td>
</tr>
<tr>
<td align="center">上传时间从:<input name="start" type="text" value="${start }"
style="width: 180px;"
onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})">
</td>
<td align="center">至:<input name="stop" type="text" value="${stop}"
style="width:180px;"
onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="查询"
style="width:150px;height: 25px" /> <input
type="reset" value="重置" style="width:150px;height: 25px">
</td>
</tr>
</table>
<font color="red"> ${errormessage}</font> <br>
<table border="1px" cellpadding="0" cellspacing="0" align="center">
<tr>
<th><input type="checkbox" name="all" value="ds" />
</th>
<th>id</th>
<th>文件名</th>
<th>文件大小</th>
<th>文件类型</th>
<th>上传时间</th>
<th>文件描述</th>
<th>执行时间</th>
<th>运行结果</th>
<th>操作</th>
</tr>
<c:forEach var="upload" items="${upload}">
<tr>
<td width="45px"><input type="checkbox" value="${upload.id}"
name="single" />
</td>
<td width="45px">${upload.id}</td>
<td width="235px">${upload.filename}</td>
<td>${upload.filesize}</td>
<td>${upload.filetype}</td>
<td>${upload.uploaddate}</td>
<td>${upload.des}</td>
<td>${upload.runtime}</td>
<td>${upload.result}</td>
<td><a href="">编辑</a> <a href="">删除</a><br /></td>
</tr>
</c:forEach>
<tr>
<td align="center" colspan="10">
<%
out.println("当前第"+pagenow2+"页/共"+pagenum+"页");
out.print(" ");
out.println("当前显示"+pagesize+"条/共"+listnum+"条");
if (pagenow > 1) {
%> <a
href="javascript:next(<%=pagenow-2 %>)"
style="text-decoration:none"> <%
out.println("上一页");
}%> </a>
<%for(int i=listbegin;i<=listend;i++)
{
pagenow=i;
%>
<% if(pagenow2==i)
{
%>
<a
href="javascript:next(<%=pagenow-1%>)"
style="text-decoration:none">
<% out.println("<font color='red'>["+i+"]</font> ");
}
else{
%>
</a>
<a
href="javascript:next(<%=pagenow-1%>)" style="text-decoration:none">
<% out.println("["+i+"] "); }%>
<%} %>
</a>
<% if (pagenow !=pagenum)
{ %> <a
href="javascript:next(<%=pagenow-1%>)" style="text-decoration:none"> <%
out.println("下一页");
}%> </a>
</td>
</tr>
</table>
<script language="javascript">
function next(pn){
//alert(pn);
//var filename= document.getElementById('search')[0].value;
document.getElementById("search").action="upload/search/"+pn;
document.getElementById('search').submit();
// alert(filename);
// location.href="${pageContext.request.contextPath}/upload/search/<%=1%>";
}
</script>
</form>
</body>
</html>
controller类
@RequestMapping(value="/search/{pagenow}")
public ModelAndView searchByFilename(@PathVariable Integer pagenow,@RequestParam(required=false) String start,@RequestParam(required=false) String stop,@RequestParam(required=false) String filetype,@RequestParam(required=false) String filename,ModelMap mod) throws ParseException
{
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext application = webApplicationContext.getServletContext();
String n=(String) application.getAttribute("name");
System.out.println("sssssssssss "+n);
System.out.println("***************filename"+filename);
System.out.println("***************start"+start);
System.out.println("***************stop"+stop);
System.out.println("***************filetype"+filetype);
System.out.println("***************pagenow"+pagenow);
System.out.println("***************pagesize"+pagesize);
ModelAndView modelAndView=new ModelAndView("user/uploadWithPage");
Page<Upload> upload=null;
if(filename==null&filetype==null&start==null&stop==null)
{
upload=uploadService.findUploadByUsername(n, pagenow, pagesize);
}
else
{
// 全空
if(filename.equals("")&filetype.equals("")&start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsername(n, pagenow, pagesize);
}
// 文件名不空
if(!(filename.equals(""))&filetype.equals("")&start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContaining(n,filename,pagenow,pagesize);
}
// 文件类型不空
if(filename.equals("")&!filetype.equals("")&start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFiletype(n, filetype, pagenow, pagesize);
}
// 开始上传时间不空
if(filename.equals("")&filetype.equals("")&!start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndUploaddateAfter(n,start,pagenow,pagesize);
}
// 结束上传时间不空
if(filename.equals("")&filetype.equals("")&start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndUploaddateBefore(n,stop,pagenow,pagesize);
}
// 文件名和文件类型不空
if(!filename.equals("")&!filetype.equals("")&start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndFiletype(n, filename, filetype, pagenow, pagesize);
}
// 文件名和开始上传时间不空
if(!filename.equals("")&filetype.equals("")&!start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndUploaddateAfter(n,filename,start,pagenow,pagesize);
}
// 文件名和结束上传时间不空
if(!filename.equals("")&filetype.equals("")&start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndUploaddateBefore(n,filename,stop,pagenow,pagesize);
}
// 文件名和上传时间 不空
if(!filename.equals("")&filetype.equals("")&!start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndUploaddateBetween(n,filename,start,stop,pagenow,pagesize);
}
// 文件类型和上传开始时间不空
if(filename.equals("")&!filetype.equals("")&!start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFiletypeAndUploaddateAfter(n,filetype,start,pagenow,pagesize);
}
// 文件类型和上传结束时间不空
if(filename.equals("")&!filetype.equals("")&start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFiletypeAndUploaddateBefore(n,filetype,stop,pagenow,pagesize);
}
// 文件名和上传时间不空
if(filename.equals("")&!filetype.equals("")&!start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFiletypeAndUploaddateBetween(n,filetype,start,stop,pagenow,pagesize);
}
// 文件名文件类型和上传开始时间
if(!filename.equals("")&!filetype.equals("")&!start.equals("")&stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndFiletypeAndUploaddateAfter(n,filename,filetype,start,pagenow,pagesize);
}
// 文件名和文件类型和上传结束时间
if(!filename.equals("")&!filetype.equals("")&start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndFiletypeAndUploaddateBefore(n,filename,filetype,stop,pagenow,pagesize);
}
// 文件名和文件类型和上传时间不空
if(!filename.equals("")&!filetype.equals("")&!start.equals("")&!stop.equals(""))
{
upload=uploadService.findUploadByUsernameAndFilenameContainingAndFiletypeAndUploaddateBetween(n,filename,filetype,start,stop,pagenow,pagesize);
}
mod.addAttribute("filename", filename);
mod.addAttribute("filetype", filetype);
mod.addAttribute("start", start);
mod.addAttribute("stop", stop);
}
//当前页查询的记录数
pageitems=upload.getNumberOfElements();
//查询出的总记录数
itemsnum=(int)upload.getTotalElements();
//总页数
pagenum=upload.getTotalPages();
HttpSession session = getSession();
session.setAttribute("pagenum", pagenum);
session.setAttribute("pagesize", pagesize);
session.setAttribute("num", itemsnum);
session.setAttribute("pagenow", pagenow);
session.setAttribute("filename", filename);
session.setAttribute("filetype", filetype);
session.setAttribute("start", start);
session.setAttribute("stop", stop);
List<Upload> u = upload.getContent();
if(u.size()==0)
{
mod.addAttribute("errormessage", "该文件不存在");
return modelAndView;
}
mod.addAttribute("pageaction", "upload/search2");
modelAndView.addObject("upload",u);
return modelAndView;
}
答
我也不知道求解 希望那位知道说下,拜托了
答
看下请求的url是不是把filename参数带上了,不然就用request取一下试试,
答
<form action="upload/searchByFileName2/<%=0 %>" method="post"> 查询调用的控制类
href="${pageContext.request.contextPath}/${pageaction}/<%=pagenow2 %>.html" 翻页调用的控制类
答
浏览器的F12开发者模式下看看你的翻页查询提交的action路径是什么呢?如果还是searchByFileName2这个Action的话,显然会报错的啊。
你的翻页请求参数里面没有filename的啊。