还有其他东西可以替换<mvc:注释驱动吗>



谢谢您阅读此内容。我正在使用多种语言并自动完成项目。使用多种语言,我正在使用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>

(这里有问题)spring-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>

哦,我知道如何解决此问题,我必须从XML文件中删除并将这些代码添加到注释Java类中。

StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter
        .setSupportedMediaTypes(Arrays.asList(MediaType.ALL, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
stringConverter.setDefaultCharset(UTF8);
converters.add(stringConverter);

最新更新