0
感謝您閱讀本文。 我在做一個使用多語言和自動完成的項目。 使用多語言,我使用Spring MVC,並使用自動完成我正在使用JQuery。 兩個此功能運行良好,但當我嘗試在同一時間使用它們。我遇到了問題。是否有其他要替換<mvc:註釋驅動>
要使用自動完成功能,我需要在我的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>
(問題在這裏) 彈簧網絡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>