2017-04-19 7 views
1

這裏我寫了三個名爲「fruit.jsp」,「fruitcontroller.java」和「fruitshopDAO.java」的文件, 。在fruit.jsp中,我從瀏覽器中獲得一些價值,其價值就像「水果商店 - Groth中心」。我想從我的數據中刪除「Groth Center」。並將其存儲到另一個變量中。最後將該變量傳遞給查詢。我的所有文件都列在下面,請幫我解決這個問題。如何從strind中刪除一些特定的單詞並將其存儲在臨時變量中並將該變量發送給我的查詢

/fruit.jsp 

    function loadDividendSchemes() 
    { 
     var scheme = $('#txt_growth_scheme').val(); /* Fruit Shop - Groth Center */ 

     $.ajaxSetup({async:true}); 
     $.post("/getFruitShop", {scheme : ""+scheme+""}, function(data) 
     { 
      var result1 = $.trim(data); 
      var obj = jQuery.parseJSON(result1); 

     }, "text"); 
    } 

/fruitcontroller.java

@RequestMapping(value="/getFruitShop") 
     public void getFruitShop(HttpServletRequest request, HttpServletResponse response) throws Exception 
     { 
      PrintWriter writer = response.getWriter(); 
      Gson gson = new Gson();; 

      try 
      { 
       String requestUrl = request.getRequestURL().toString(); 
       if(!AdvisorkhojUtils.isValidUrl(requestUrl, "getFruitShop")) 
       { 
        response.sendRedirect(request.getContextPath() + "/page-not-found?invalidUrl=" + requestUrl); 
        return; 
       } 
       String scheme = request.getParameter("scheme"); /* value "Fruit Shop - Groth Center" from the jsp file */ 

       if(scheme == null || StringHelper.isEmpty(scheme)){scheme = "fruit shop - Growth";} 

       scheme = scheme.trim(); 

       List<String> schemeList = mutualFundPerformanceDAO.getFruitShop(scheme); /* send scheme = "Fruit Shop - Groth Center" to fruitshopDAO.java */ 

       response.setContentType("application/json"); 
       writer.println(gson.toJson(schemeList)); 
       writer.close(); 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 
     } 

/fruitshopDAO.java

public List<String> getFruitShop(String scheme) 
     { 
      Session session = null; 
      Query query = null; 
      List<String> list = null; 

        /* How can I erase "Groth Center" from scheme and stored remaining string into a new variable called "findshop" and send it to below query */ 
     /* I want something like "findshop = Fruit Shop -" */ 
      try 
      { 
       session = HibernateUtils.getAdvisorkhojAmfiSession(); 
       session.getTransaction().begin(); 

       query = session.createSQLQuery("from scheme_mapping where fruitshop like 'findshop%'"); 
       query.setMaxResults(25); 
       list = query.list(); 

       session.getTransaction().commit(); 
      } 
      catch(Exception ex) 
      { 
       session.getTransaction().rollback(); 
       ex.printStackTrace(); 
      } 
      return list; 
     } 
    } 
+0

方案.substring(0,scheme.indexOf( 「 - 」)) –

回答

0

我想這是你所需要的。

String findShop = scheme.substring(0, scheme.indexOf("-")); 

此外它傳遞到查詢爲:

"from scheme_mapping where fruitshop like :findShop").setParameter("findShop", findShop+"%"); 
相關問題