我在这里写了三个文件,分别叫做“fruit.jsp”、“fruitcontroller.java”和“fruitshopDAO.java”。在 fruit.jsp 中,我从浏览器中获取一些值,该值类似于“Fruit Shop - Groth Center”。我想从我的数据中删除“Groth Center”。并将其存储到另一个变量中。最后将该变量传递给查询。我所有的文件都列在下面,请帮我解决这个问题。
/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;
}
}
最佳答案
我想这就是您所需要的。
String findShop = scheme.substring(0, scheme.indexOf("-"));
进一步将其传递到查询中:
"from scheme_mapping where fruitshop like :findShop").setParameter("findShop", findShop+"%");
关于java - 如何从字符串中删除某些特定单词并将其存储在临时变量中并将该变量发送到我的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492120/