JSP工作中输入标记的字符串在SQL查询中不正确

时间:2016-04-23 18:28:42

标签: java hibernate jsp

我在尝试搜索数据库中的某些数据时遇到了一些麻烦。 我有输入标签,我写了一些文本,然后它从JSP获取到控制器,服务,并在dao我有一个查询,其中把这个字符串放入查询,当这个字符串是英文 - 一切正常,但当我写乌克兰搜索中的字符串不起作用。有人可以帮忙吗?

JSP页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form"  
uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Головна сторінка</title>
<!-- Bootstrap core CSS -->
<link href="<c:url   
value="../../resources/bootstrap/css/bootstrap.min.css"/>"   
rel="stylesheet"/>

</head>

<body>

<c:url var="addUrl" value="/main/mainFrame/new"/>
<c:url var="searchResult" value="/main/resultOfSearching"/>



<a href="${addUrl}"><button type="button" class="btn btn-primary btn- 
lg btn-block" style="margin-left:auto;

margin-right:auto;

width:250px;

margin-top:50px;">Додати звернення</button></a>


<form:form method="GET" action="${searchResult}"     
modelAttribute="ListOfAppeals">

<div class="col-lg-6" style="width:800px;
                         margin-left:270px;
                         margin-top:200px;">

<div class="input-group">
    <input type="text" formenctype="text/plain" name="searchChar"   
class="form-control" placeholder="Введіть ідентифікатор для пошуку">
  <span class="input-group-btn">
    <button class="btn btn-primary" type="submit">Пошук</button></a>
  </span>
</div>

</div>

</form:form>

 </body>

</html>

searchingResult JSP page

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Bootstrap core CSS -->
<link href="<c:url value="styles/searchingResultPage.css" />" 
rel="stylesheet">
<link href="<c:url   
value="../../resources/bootstrap/css/bootstrap.min.css" />" 
rel="stylesheet">
<title>Результати пошуку</title>
</head>

<body>

<h1 style="text-align: center">Список звернень (запитів)</h1>


<table class="table table-bordered" style="width: 1200px;margin-right: 
auto;margin-left: auto">

<thead>
<tr>
    <th style="text-align:center">#</th>
    <th style="text-align:center">Номер звернення (запиту)</th>
    <th style="text-align:center">Вхідний номер</th>
    <th style="text-align:center">П.І.Б. народного депутата</th>
    <th style="text-align:center">Резолюція керівника</th>
    <th style="text-align:center">Тип документу</th>
    <th class="example" style="text-align:center">Дії з 
документом</th>
</tr>
</thead>

<tbody>
<c:forEach items="${ListOfAppeals}" var="Appeals">

    <tr>
        <td style="text-align:center"><c:out value="${Appeals.id}"/>   
</td>
        <td style="text-align:center"><c:out  
value="${Appeals.number}"/></td>
        <td style="text-align:center"><c:out 
value="${Appeals.incomingDate}"/></td>
        <td style="text-align:center"><c:out   
value="${Appeals.nameOfDepute}"/></td>
        <td style="text-align:center"><c:out 
value="${Appeals.resolutionOfChief}"/></td>
        <td style="text-align:center"><c:out 
value="${Appeals.typeOfAppeal}"/></td>
        <td style="text-align:center"><button type="button" class="btn   
btn-danger btn-sm">видалити</button>
            <button type="button" class="btn btn-warning btn-sm"  
style="margin-left: 5px;">редагувати</button></td>
    </tr>

</c:forEach>
</tbody>
</table>

</body>
</html>

控制器方法

@RequestMapping(value = "/resultOfSearching", method =  
RequestMethod.GET)
public String getSearchResult(Model model,     
@ModelAttribute("searchChar")String searchResult)  {
    List<DeputesAppeal> deputesAppeals =  
deputesAppealService.abstractSearch(searchResult);
    model.addAttribute("ListOfAppeals", deputesAppeals);
    return "searchingResultPage";
}

服务方法

public List<DeputesAppeal> abstractSearch(String searchingChar) {
    return deputesAppealDao.abstractSearch(searchingChar);
}

和DaoLayer

public List<DeputesAppeal> abstractSearch(String searchingChar) {
    Query query = sessionFactory.getCurrentSession().createQuery("from   
DeputesAppeal where concat(NumberOfAppeal, DateOfIncomingAppeal,  
NameOfDepute, ResolutionOfChief, TypeOfAppeal)  like '%" + 
searchingChar + "%'");
    return query.list();
}

当sql登录

  from DEPUTES_APPEAL deputesapp0_ where concat(NumberOfAppeal,    
    DateOfIncomingAppeal, deputesapp0_.NameOfDepute,  
    deputesapp0_.ResolutionOfChief, TypeOfAppeal) like '%[B@7c815681%'

1 个答案:

答案 0 :(得分:0)

我认为您可以像这样使用字符编码过滤器(在您的web.xml文件中):

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

并将此过滤器放在web.xml中的第一个过滤器