2017-09-13 25 views
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> 

回答

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); 
相关问题