【问题标题】:Is there something else to replace <mvc: annotation-driven>还有其他东西可以代替 <mvc: annotation-driven>
【发布时间】:2017-09-13 07:40:29
【问题描述】:

感谢您阅读本文。 我正在做一个使用多语言和自动完成的项目。 对于多语言,我使用的是 Spring MVC,而对于自动完成,我使用的是 JQuery。 两个此功能运行良好,但是当我尝试同时使用它们时。我有问题。

要使用自动完成功能,我需要在我的 xml 文件中,否则我会得到 ArrayList 错误。但是当我把那行放进去时,Multiple Language 不能再运行了,我不能改变语言,程序使用默认的。而且我没有收到任何错误。

那么你能告诉我是什么原因导致我的问题吗?有什么东西可以用来代替 mvc annotation-driven 吗?谢谢。

  • 自动完成功能:

控制器:

@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;
    }

WebMVC配置

@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>

【问题讨论】:

    标签: jquery spring spring-mvc


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多