组合中的语言选择
我将 servlet 配置为支持国际化,如下所示:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
在 JSP 页面上,我以如下方式添加了语言选择:
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<h3><spring:message code="label.title"/></h3>
<span style="float: right">
<a href="?lang=en">en</a>
|
<a href="?lang=de">de</a>
</span>
如何替换上面的 select(组合)代码组件?
(伪代码)
<td><form:select path="language" onchange="">
<form:option value="0" label="..." />
<form:options items="${languageList}" />
</form:select></td>
请帮助
编辑 我不想将任何内容设置为默认值。他想被选择这个选项,他选择了你。您将看到一个示例:
第一步 本地主机:8080/newUser -> 在控制器
@ RequestMapping (value = "/ newuser" method = RequestMethod.GET)
public ModelAndView showNewUser (ModelMap model) {
ModelAndView mav = new ModelAndView ();
mav.mav.setViewName ("newuser");
User user = new User ();
mav.getModel (). put ("user", user);
return mav;
我的 JSP 表单 中 ……
<form id="langForm" action="newUse" method="get">
<table>
<tr>
<td> <a href="#"> <spring:message code="label.helpLink" /> </ a> </ td>
<td> <select name="lang" id="lang">
<option value="de"> <spring:message code="languageName.de"> </ spring: message> </ option>
<option value="en"> <spring:message code="languageName.en"> </ spring: message> </ option>
</ select> </ td>
</ tr>
</ table>
</ form>
<script type="text/javascript">
$ ("# lang"). change (function () {
document.forms ["langForm"] submit ();
});
</ script>
第二步 - 最终用户从组合选项“languageName.en”中进行选择。然后重新加载页面 (localhost:8080/newUser?lang=en) 将语言更改为 EN,但组合仍被选为第一项。我想选择的是您在步骤 2 中选择的那个,因此,我将保存状态。
添加
<form>
<select name="lang" id="lang" onchange="this.form.submit();">
<option value="pl"><spring:message code="languageName.pl"></spring:message></option>
<option value="en"><spring:message code="languageName.en"></spring:message></option>
</select>
</form>
如何在没有模型的情况下保留选项?
I configured my servlet to support internationalization, just as I showed below:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
On the JSP page, I added the choice of language in such a way:
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<h3><spring:message code="label.title"/></h3>
<span style="float: right">
<a href="?lang=en">en</a>
|
<a href="?lang=de">de</a>
</span>
How do I replace the above code component of a select (combo)?
(pseudo code)
<td><form:select path="language" onchange="">
<form:option value="0" label="..." />
<form:options items="${languageList}" />
</form:select></td>
Please help
EDIT
I do not want anything to set as the default. He wants to be chosen this option, he chose you. You will see an example:
The first step
localhost: 8080/newUser
->
In controller
@ RequestMapping (value = "/ newuser" method = RequestMethod.GET)
public ModelAndView showNewUser (ModelMap model) {
ModelAndView mav = new ModelAndView ();
mav.mav.setViewName ("newuser");
User user = new User ();
mav.getModel (). put ("user", user);
return mav;
My JSP forms
...
<form id="langForm" action="newUse" method="get">
<table>
<tr>
<td> <a href="#"> <spring:message code="label.helpLink" /> </ a> </ td>
<td> <select name="lang" id="lang">
<option value="de"> <spring:message code="languageName.de"> </ spring: message> </ option>
<option value="en"> <spring:message code="languageName.en"> </ spring: message> </ option>
</ select> </ td>
</ tr>
</ table>
</ form>
...
<script type="text/javascript">
$ ("# lang"). change (function () {
document.forms ["langForm"] submit ();
});
</ script>
The second step
- End-user selects from the combo options "languageName.en. Followed by reloading the page
(localhost: 8080/newUser? lang = en) to change the language to EN but the combo is still selected as the first item. I would like to chose was the one you selected in step 2 So, I would save the state.
ADDED
<form>
<select name="lang" id="lang" onchange="this.form.submit();">
<option value="pl"><spring:message code="languageName.pl"></spring:message></option>
<option value="en"><spring:message code="languageName.en"></spring:message></option>
</select>
</form>
How to keep the option without a model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在jsp中
添加javascript来监听其状态的变化。如果发生更改,则在会话中反映此参数语言
更新:
假设您有这样的组合框
,并且您希望默认选择 X-large,请像这样制作
HTML
in jsp
add javascript that will listen to its change in state. if change occurs reflect this param language in session
Update:
Suppose you have combo box like this
and you want X-large to be selected by default, make HTML like this
make it