程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> spring mvc-springmvc 參數傳遞問題

spring mvc-springmvc 參數傳遞問題

編輯:編程綜合問答
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" />&nbsp;&nbsp;
<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>&nbsp;");
                 else
                  out.println("["+i+"]&nbsp;");
                 }

                 %> 
               </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" />&nbsp;&nbsp; <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("&nbsp;&nbsp;");
                   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>&nbsp;"); 
                 }
                 else{
                  %>
                </a> 


              <a
                    href="javascript:next(<%=pagenow-1%>)" style="text-decoration:none"> 
                    <%  out.println("["+i+"]&nbsp;"); }%>
                <%} %>
                </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;
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved