简体   繁体   English

Spring LocaleResolver不要放入startPage(http:// localhost:8080 /)defaultLocale

[英]Spring LocaleResolver don't put to the startPage(http://localhost:8080/) defaultLocale

Good day, all. 大家好

I configure LocaleResolver for setting defaultLocale, but it's don't work for start page ( 我将LocaleResolver配置为设置defaultLocale,但不适用于起始页(

  <welcome-file-list>
        <welcome-file>/WEB-INF/view/index.jsp</welcome-file>
    </welcome-file-list>

). )。

web.xml: web.xml中:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>GetITFRee</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/WEB-INF/view/index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/view/error/errorpage.jsp</location>
    </error-page>
    </web-app>

application context: 应用程序上下文:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


    <context:property-placeholder location="classpath:util.properties" />
    <!--Activates various annotations to be detected in bean classes: Spring's @Required and @Autowired and so on-->
    <context:annotation-config/>

    <!-- Datasource.  -  MySQL -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!--Do not forget activate @Transactional JPA annotation with <annotation-driven/>-->
    <!-- JPA Persistence Context and EntityManager configuration -->
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <!--packagesToScan - search Entity and mapping them -->
        <property name="packagesToScan" value="by.GetItFree" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
    </bean>

    <!-- Automatic Transaction Participation-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <jpa:repositories base-package="by.GetItFree.orm.repository" entity-manager-factory-ref="entityManagerFactory"
                      transaction-manager-ref="transactionManager"/>


    <!-- REST template configuration -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

</beans>

mvc-config: MVC-配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- context:component-scan This tag will scan @Component, @Repository, @Service, @Controller
         and also resolves @Autowired and @Qualifier -->
    <context:component-scan base-package="by.GetItFree"/>

    <!--
        mvc:annotation-driven configures Spring MVC annotations
        Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
        HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values
        from @RequestMapping or @ExceptionHandler methods.
     -->
    <mvc:annotation-driven/>

    <!-- activate @Transactional JPA annotation -->
    <tx:annotation-driven/>

    <!-- ViewResolver bean config for mapping strings to jsp views -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="order" value="1"/>
        <property name="prefix" value="/WEB-INF/view"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <mvc:view-controller path="/about.html" view-name="/about/about"/>
    <mvc:view-controller path="/index.html" view-name="/index"/>


    <!-- Static Resources Configuration (get access to static sources such as CSS and JavaScript files) -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <mvc:interceptors>

        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="languageVar"/>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="ru"/>
        <!-- cookieMaxAge in seconds. if you set it to -1, the cookie will be deleted when browser is closed) -->
        <property name="cookieMaxAge" value="100000"/>
    </bean>

    <!-- MessageSource ReloadableResourceBundleMessageSource configuration -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames" value="classpath:/locales/messages,classpath:util"/>
        <property name="cacheSeconds" value="1"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

    <!--
     mvc:annotation-driven configures Spring MVC annotations
     Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
     HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values
     from @RequestMapping or @ExceptionHandler methods.
    -->
    <mvc:annotation-driven>
        <!--use int RestController to produce pretty json response-->
        <mvc:message-converters>
            <bean id="jacksonHttpMessageConverter"
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prettyPrint" value="true"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

If i will try to access to the http://localhost:8080/index.htlm directly - it's set default Locale. 如果我尝试直接访问http:// localhost:8080 / index.htlm ,则将其设置为默认语言环境。 For the SessionLocaleResolver - result the same. 对于SessionLocaleResolver-结果相同。

Thank your for attention. 感谢您的关注。

Update your Interceptor like this. 像这样更新您的拦截器。

  <mvc:interceptors>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="languageVar"/>
            </bean>
    </mvc:interceptors>

Thanks to mr. 多亏先生。 M. Deinum. M. Deinum。 The issue - 问题 -
Don't use a welcome page and let all pages be served by the DispatcherServlet. 不要使用欢迎页面,而让所有页面都由DispatcherServlet提供服务。 additionaly can use: 另外可以使用:

 <mvc:view-controller path="/" view-name="first-view page"> 

Thanks all. 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring中的LocaleResolver - LocaleResolver in Spring Spring Rest Template Client:拒绝连接到http:// localhost:8080 - Spring Rest Template Client : Connection to http://localhost:8080 refused Swagger + Spring 引导不显示 API 端点在 http://localhost:8080/home/swagger-ui.html - Swagger + Spring boot doesn't show API end points in http://localhost:8080/home/swagger-ui.html 我的 tomcat 正在运行,但无法连接到 http://localhost:8080 - My tomcat is running but I can't connect to http://localhost:8080 Spring LocaleResolver不对语言环境做出反应 - Spring LocaleResolver not reacting to locale Spring LocaleResolver链接 - Spring LocaleResolver chaining Spring security + LocaleResolver - Spring security + LocaleResolver 带 Angular 的弹簧靴。 CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:8080/”处的 XMLHttpRequest - Spring boot with Angular. Access to XMLHttpRequest at 'http://localhost:8080/' from origin 'http://localhost:4200' has been blocked by CORS policy 无法处理请求[GET http:// localhost:8080]:响应状态404 Spring,RESTfull,thymleaf - Failed to handle request [GET http://localhost:8080 ]: Response status 404 Spring, RESTfull ,thymleaf Spring Boot2 Oauth2隐式流 - http:// localhost:8080 / oauth / authorize获取访问被拒绝 - Spring Boot2 Oauth2 Implicit Flow - http://localhost:8080/oauth/authorize getting Access Denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM