感谢您阅读本文。 我正在使用多语言和自动完成功能完成项目。 使用多语言,我使用Spring MVC,使用自动完成我使用JQuery。 两个这个功能运行良好,但当我尝试在同一时间使用它们。我遇到了问题。
要使用自动完成功能,我需要在我的xml文件中或者我得到ArrayList错误。但是当我把这条线放进去时,多语言不能再运行,我无法改变语言,程序使用默认语言。而且我没有收到任何错误。
那么请你告诉我是什么导致了我的概率,是否有什么我可以用来代替mvc注释驱动?谢谢。
控制器:
@RequestMapping(value = "/motsach/find", method = RequestMethod.GET)
public @ResponseBody List<Book> findBook(@RequestParam("tensach") String tensach) {
return simulateSearchResult(tensach);
}
private List<Book> simulateSearchResult(String tensach) {
List<Book> result = new ArrayList<Book>();
data = (List<Book>) bookService.findAll();
for (Book book : data) {
if (book.getTensach().contains(tensach)) {
result.add(book);
}
}
return result;
}
使用Javascript:
<div>
<input type="text" id="book-search" placeholder="Add Book Name" style="width: 1050px;"> <span>
<button id="button-id" type="button">Search</button>
</span>
</div>
<script>
$(document).ready(function() {
$('#book-search').autocomplete({
serviceUrl: '${pageContext.request.contextPath}/motsach/find',
paramName: "tensach",
delimiter: ",",
transformResult: function(response) {
return {
suggestions: $.map($.parseJSON(response), function(item) {
val = item.tensach + " " + '-' + " " + item.tacgia
return { value: val};
})
};
}
});
});
</script>
(问题在这里) 弹簧的web-servlet.xml中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan the JavaConfig -->
<context:component-scan base-package="com.project.form.config" />
//THIS IS THE PROBLEM!
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
SpringWebConfig
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
rb.setBasenames(new String[] { "messages/messages", "messages/validation","messages/messages_vi","messages/messages_en" });
return rb;
}
@Bean(name="localeResolver")
public LocaleResolver getLocaleResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieDomain("myAppLocaleCookie");
// 60 minutes
resolver.setCookieMaxAge(60 * 60);
return resolver;
}
WebMVCConfig
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
}
JSP文件:
<div
style="text-align: right; padding: 5px; margin: 5px 0px; background: #aaa;">
<a href="${pageContext.request.contextPath}/motsach?lang=en">English</a>
<a href="${pageContext.request.contextPath}/motsach?lang=vi">Vietnamese</a>
</div>
<thead>
<tr>
<th><spring:message code="label.ID" /></th>
<th><spring:message code="label.name" /></th>
<th><spring:message code="label.author" /></th>
<th><spring:message code="label.genre" /></th>
<th><spring:message code="label.action" /></th>
</tr>
</thead>
答案 0 :(得分:0)
哦,我知道如何解决这个问题,我必须从xml文件中删除并将这些代码添加到注释java类中。
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter
.setSupportedMediaTypes(Arrays.asList(MediaType.ALL, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
stringConverter.setDefaultCharset(UTF8);
converters.add(stringConverter);