0
如果任何用戶訪問我的網站可以查看我的網站使用不同的語言,所以我選擇了3種語言英語,法語,他加祿語使用Spring國際化,我編碼,但是當我點擊法語或他加祿語鏈接語言改爲兩者之一,但仍然保持英文。程序編譯和運行時沒有任何錯誤,但語言不會分別更改爲法語和他加祿語。 如果需要任何信息,我已準備好提供。如何使用spring的國際化?
messages_en.properties
student.title=Student List
student.id=Student ID
student.firstname=First name
student.lastname=Last name
student.year=Year
messages_fr.properties
student.title=Liste des étudiants
student.id=carte d'étudiant
student.firstname=prénom
student.lastname=nom de famille
student.yearLevel=Niveau année
messages_tl.properties
student.title=Listahan ng mga Magaaral
student.id=Numero ng magaaral
student.firstname=Pangalan
student.lastname=Apelyido
student.yearLevel=Antas
彈簧servlet.xml中
<!-- Spring Internationalizations -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
student.jsp
<title>Student Management</title>
</head>
<body>
Language: <a href="./?language=en">English</a> | <a href="./?language=tl">Tagalog</a> | <a href="./?language=fr">French</a>
<h1><spring:message code="student.title" /></h1>
<form:form action="student.do" method="POST" commandName="student">
<table>
<tr>
<td><spring:message code="student.id" /></td>
<td><form:input path="studentid" /></td>
</tr>
<tr>
<td><spring:message code="student.firstname" /></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><spring:message code="student.lastname" /></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><spring:message code="student.year" /></td>
<td><form:input path="year" /></td>
</tr>
<tr>
<td>Date</td>
<td><div class="col-md-12">
<input type="date" class="def-input" placeholder="Your Date of Birth!">
</div>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Edit" />
<input type="submit" name="action" value="Delete" />
<input type="submit" name="action" value="Search" />
</td>
</tr>
</table>
</form:form>
<br>
<table border="1">
<th><spring:message code="student.id" /></th>
<th><spring:message code="student.firstname" /></th>
<th><spring:message code="student.lastname" /></th>
<th><spring:message code="student.year" /></th>
<c:forEach items="${studentlist}" var="student">
<tr>
<td>${student.studentid}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.year}</td>
</tr>
</c:forEach>
</table>
我提供工作空間路徑InternBridge \ com \ internbridge \ resources。在資源文件夾中.properties文件存在。你在問這個還是其他的問題? –
是的,我在找什麼...所以嘗試創建一個消息文件夾到資源中,並通過value =「classpath:messages/messages」替換value =「classpath:messages」 – Skykaza
我編輯了答案,請一次點擊上面的項目視圖鏈接,在這個資源下,我創建了正常的文件夾命名消息,但它是作爲包視圖創建的,爲什麼會發生這種情況?我保存了.properties文件,它不工作,請幫助我。 –