如何在Spring MVC中动态更改语言环境语言?

时间:2019-01-23 08:22:24

标签: java spring-mvc localization

我想在Spring MVC中使用本地化(我想在我的Web应用程序中完全支持UTF-8)和Eclipse Java EE IDE为Web开发人员创建一个虚拟项目,该版本使用4.7.0 Oxygen Release和Apache Tomcat Server版本- 9.0。一切工作正常,但是当我单击链接“印地语”时。它的编码方式不同。下面是我的代码。

这是我的dispatcher-servlet.xml:-

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" 
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/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
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:component-scan base-package="dummy" />
   <mvc:annotation-driven />

<bean id="viewResolver2" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    <property name="order" value="1" />
    </bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>


 <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="language" />
</bean>

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

<bean id="CookielocaleResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="language" />
<property name="defaultLocale" value="en" />
</bean>


 <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/messages/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
    <list>
        <ref bean="localeChangeInterceptor" />
    </list>
</property>
</bean>

这是我的Login.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<title>Login Form</title>
<html lang="en" dir="ltr">
 <head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-    validate/1.19.0/jquery.validate.min.js"></script>
 </head>
  <body>
<spring:message code="login.placeholder.username" var="login.placeholder.username"> </spring:message>
<spring:message code="login.placeholder.password" var="login.placeholder.password"> </spring:message>
<div class="container">
    <div class="rows">
        <div class="col-sm-6">
        Language : <a href="?language=en">English</a>|<a href="?language=hi">Hindi</a>
            <form name="formdemo" method="post" id="form" novalidate="novalidate">
                <div class="form-group">
                    <label for="username"><spring:message code="login.label.username" text="username" /></label>
                    <input type="text" name="username" id= "username" placeholder="${login.placeholder.username}" class="form-control" required />
                </div>

                <div class="form-group">
                    <label for="password" ><spring:message code="login.label.password" text="Password"/></label>
                    <input id="password" type="password"  name="password"  placeholder="${login.placeholder.password}" class="form-control" required />
                </div>
                <div id="result"></div>
                <button type="submit" id ="submit" class="btn btn-primary"><spring:message code="login.controls.submit" text="Submit" /></button>
                <button type="button" class="btn btn-primary" onclick="location.href='printAllUsers'"><spring:message code="login.controls.ShowAll" text="ShowAllUsers" /></button>
            </form>
        </div>
    </div>
</div>

下面是我的屏幕截图,可以帮助您进一步

这是我的项目布局:-

enter image description here

这是我用英语登录时的欢迎文件:-

enter image description here

当我单击“印地语”时,应将其翻译成印地语。但是以下图像显示异常:-

enter image description here

我的代码有什么问题?有人在这里指出我的错误吗?拜托,有人可以帮我解决这个问题吗?

0 个答案:

没有答案