下面这段代码中,老提示:hotelinfos cannot be resolved .好长时间了没解决,求帮助!!!
                 <%
    HotelInfo hotelinfoCondition = new HotelInfo();
    hotelinfoCondition.setHotelName(hotelName);
    hotelinfoCondition.setHotelType(hotelType);
    System.out.println(hotelName);
    System.out.println(flag); 
    HotelInfoService hotelis = new HotelInfoService();
   if(flag==1) {
   List<Map<String,Object>> hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition);
   }else {
    List<Map<String, Object>> hotelinfos = hotelis.listAllhotelinfos();}
for (int i = 0; i < hotelinfos.size(); i++) {
%>
<tr class="Label1_text_white">
<td><%=hotelinfos.get(i).get("HotelID")%></td>
<td><%=hotelinfos.get(i).get("HotelName")%></td>
<td><%=hotelinfos.get(i).get("HotelAddress")%></td>
<td align="center"><%=hotelinfos.get(i).get("HotelType")%></td>
<td align="center"><%=hotelinfos.get(i).get("IsAd")%></td>
<td align="center"><a href="Edit_hotel.jsp?HotelID=<%=hotelinfos.get(i).get("HotelID")%>">编辑</a></td>
</tr>
<%
}
%>

解决方案 »

  1.   

    初学者常见错误,把变量定义在块内,结果变成块内局部变量了;那么块结束后就没法使用了。关键是这段:
    if(flag==1) {
     List<Map<String,Object>> hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition);
     }else {
     List<Map<String, Object>> hotelinfos = hotelis.listAllhotelinfos();}
    请修改为:
    List<Map<String,Object>> hotelinfos = null;
    if(flag==1) {
       hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition);
    }else {
       hotelinfos = hotelis.listAllhotelinfos();
    }
      

  2.   

    type Exception reportmessage An exception occurred processing JSP page /hotel_type.jsp at line 98 95: HotelInfoService hotelis = new HotelInfoService(); 96: if(flag==1) { 97: List<Map<String,Object>> hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition); 98: for (int i = 0; i < hotelinfos.size(); i++) { 99: %> 100: <tr class="Label1_text_white"> 101: <td><%=hotelinfos.get(i).get("HotelID")%></td> Stacktrace:description The server encountered an internal error (An exception occurred processing JSP page /hotel_type.jsp at line 98 95: HotelInfoService hotelis = new HotelInfoService(); 96: if(flag==1) { 97: List<Map<String,Object>> hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition); 98: for (int i = 0; i < hotelinfos.size(); i++) { 99: %> 100: <tr class="Label1_text_white"> 101: <td><%=hotelinfos.get(i).get("HotelID")%></td> Stacktrace:) that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /hotel_type.jsp at line 9895:      HotelInfoService hotelis = new HotelInfoService();
    96:     if(flag==1) {
    97:     List<Map<String,Object>> hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition);
    98:   for (int i = 0; i < hotelinfos.size(); i++) {
    99:  %>
    100:  <tr class="Label1_text_white">
    101:  <td><%=hotelinfos.get(i).get("HotelID")%></td>
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    root cause java.lang.NullPointerException
    org.apache.jsp.hotel_005ftype_jsp._jspService(hotel_005ftype_jsp.java:170)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.
    --------------------------------------------------------------------------------
      

  3.   

    照着改过来了,可是还是有问题,是下面这个错误呢?
    Exception reportmessage java.lang.NullPointerExceptiondescription The server encountered an internal error (java.lang.NullPointerException) that prevented it from fulfilling this request.
      

  4.   

    空指针问题,估计是你if某个分支,没有返回值吧,可以自己检查看看:List<Map<String,Object>> hotelinfos = null;
    if(flag==1) {
       hotelinfos=hotelis.searchHotelInfos(hotelinfoCondition);
       System.out.println("flag==1 " + hotelinfos);
    }else {
       hotelinfos = hotelis.listAllhotelinfos();
       System.out.println("flag!=1 " + hotelinfos);
    }如果不能保证绝对有返回值的话,后面的所有语句要有 if 括起来:
    if (hotelinfos != null) {
      for (int i = 0; i < hotelinfos.size(); i++) {
    %>
     <tr class="Label1_text_white">
     <td><%=hotelinfos.get(i).get("HotelID")%></td>
     <td><%=hotelinfos.get(i).get("HotelName")%></td>
     <td><%=hotelinfos.get(i).get("HotelAddress")%></td>
     <td align="center"><%=hotelinfos.get(i).get("HotelType")%></td>
     <td align="center"><%=hotelinfos.get(i).get("IsAd")%></td>
     <td align="center"><a href="Edit_hotel.jsp?HotelID=<%=hotelinfos.get(i).get("HotelID")%>">编辑</a></td>
     </tr>
    <%
      }
    } else {
    %>
     <tr class="Label1_text_white"><td colspan="6">没有找到记录</td></tr>
    <%
    }
    %>
      

  5.   


    输出的是“没有找到记录”,控制台输出的是“flag==1 null”。
    酒店管理
                                               添加酒店
    酒店名:   分类:                  搜索
    酒店ID 酒店名 酒店地址 所属分类 允许插播广告 操作
    1 莫泰外滩店 XXXXXXXXXX 经济型莫泰 是 编辑
    2 莫泰张江店 XXXXXXXXXX 经济型莫泰 是 编辑
    我想做的是,按条件查询,条件是就点名和分类,要得到这两个参数,这个是在一个JSP页面里去参数
                <% 
        String hotelName=request.getParameter("HotelName");
        String hotelType=request.getParameter("HotelType");
        %>
    然后主要有一个实现类:public List searchHotelInfos(HotelInfo hotelinfoCondition)throws Exception{ boolean hotelNameCondition=false;
    boolean hotelTypeCondition=false;
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("select * from tbhotel where ");
    if(hotelinfoCondition.getHotelName()!=null&&!hotelinfoCondition.getHotelName().trim().equals("")){
    stringBuffer.append(" HotelName like ?");
    hotelNameCondition = true;
    }
    if(hotelinfoCondition.getHotelType()!=null&&!hotelinfoCondition.getHotelType().trim().equals("")){
    stringBuffer.append("and HotelType like ?");
    hotelTypeCondition= true;
    }
    PreparedStatement ps = connection.prepareStatement(stringBuffer.toString());
    int i=1;
    if(hotelNameCondition)
    ps.setString(i++,"%"+hotelinfoCondition.getHotelName().trim()+"%");
    if(hotelTypeCondition)
    ps.setString(i++,"%"+hotelinfoCondition.getHotelType().trim()+"%");
    ResultSet rs = ps.executeQuery();
    List list = Dto.convertList(rs);
    // System.out.println(rs);
    return list;
    请看哪有问题吗?想在没有条件查询时,下表显示所有的信息,但当条件查询就显示符合条件的?这样做可以吗?
      

  6.   

    那要检查你最后生成的条件对不对了。把这两个打印出来看看吧:
      hotelinfoCondition.getHotelName().trim()

      hotelinfoCondition.getHotelType().trim()
    P.S. 你组装查询的方式有问题,主要是如果HotelName没有 而 HotelType 有的时候,就会多出一个 and
      

  7.   

    <form method="post" id="form1" action="hotel_type.jsp">
    <table width="96%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="24%"><font size="2">酒店名:</font> <input
    id="elementID3" name="HotelName" type="text" value="yyyy" size="10" /></td>
    <td width="18%"><font size="2">分类:</font> 
    <select name="HotelType" id="HotelType" >
                        <option>经济型</option>
                        <option>三星级</option>
             </select>
    </td>
            <td valign="bottom" width="15%" align="center">
       <input type="submit" name="submit" value="搜索" >
      </td>
    </tr>
    </table>
    </form>
    上面是那个表单,在同一个jsp页面中的下面的代码,来取得参数,可是打印出来的都是空的,HotelName只能取得英文值,如果是汉字就取得乱码,好像一直取不到HotelType的值,什么原因呢?     
    <% String hotelName=request.getParameter("HotelName");
              String hotelType=request.getParameter("HotelType");
          HotelInfo hotelinfoCondition = new HotelInfo();
          hotelinfoCondition.setHotelName(hotelName);
          hotelinfoCondition.setHotelType(hotelType); %>
    下面这个是那个实现的java类,
    public List searchHotelInfos(HotelInfo hotelinfoCondition)throws Exception{ boolean hotelNameCondition=false;
    boolean hotelTypeCondition=false;
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("select * from tbhotel where ");
    if(hotelinfoCondition.getHotelName()!=null&&!hotelinfoCondition.getHotelName().trim().equals("")){
    stringBuffer.append(" HotelName like ?");
    hotelNameCondition = true;
    }
    if(hotelinfoCondition.getHotelType()!=null&&!hotelinfoCondition.getHotelType().trim().equals("")){
    stringBuffer.append("and HotelType like ?");
    hotelTypeCondition= true;
    }
    PreparedStatement ps = connection.prepareStatement(stringBuffer.toString());
    int i=1;
    if(hotelNameCondition)
    ps.setString(i++,"%"+hotelinfoCondition.getHotelName().trim()+"%");
    if(hotelTypeCondition)
    ps.setString(i++,"%"+hotelinfoCondition.getHotelType().trim()+"%");
    ResultSet rs = ps.executeQuery();
    List list = Dto.convertList(rs);
    System.out.println(hotelinfoCondition.getHotelName().trim());(这里打印出来的HotelName如果是字母正常打印,汉字打印出来时乱码,哪错了吗?)
    System.out.println(hotelinfoCondition.getHotelType().trim());(这里始终打印出来的都是“????”,好是取不到参数。)
    // System.out.println(rs);
    return list;
    }