组合中的语言选择

发布于 2024-10-17 10:22:24 字数 3280 浏览 1 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2024-10-24 10:22:24

在jsp中

<form:select path="language" onchange="">
<c:forEach var="lan" item="${languageList}">
   <form:option value="0" label="${lan}" />
   <form:options items="${lan}" />
<c:forEach />
</form:select>

添加javascript来监听其状态的变化。如果发生更改,则在会话中反映此参数语言

更新:

假设您有这样的组合框

<select>
  <option>Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option>X-Large</option>
</select>

,并且您希望默认选择 X-large,请像这样制作

<select>
  <option>Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option selected="true">X-Large</option>
</select>

HTML

<select name="lang" id="lang">
<option value="de" <c:if ${sessionBean.lan}=='de'>seelcted='true'</c:if>> <spring:message code="languageName.de"> </ spring: message> </ option>
<option value="en"  <c:if ${sessionBean.lan}=='en'>seelcted='true'</c:if>> <spring:message code="languageName.en"> </ spring: message> </ option>
</ select>

in jsp

<form:select path="language" onchange="">
<c:forEach var="lan" item="${languageList}">
   <form:option value="0" label="${lan}" />
   <form:options items="${lan}" />
<c:forEach />
</form:select>

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

<select>
  <option>Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option>X-Large</option>
</select>

and you want X-large to be selected by default, make HTML like this

<select>
  <option>Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option selected="true">X-Large</option>
</select>

make it

<select name="lang" id="lang">
<option value="de" <c:if ${sessionBean.lan}=='de'>seelcted='true'</c:if>> <spring:message code="languageName.de"> </ spring: message> </ option>
<option value="en"  <c:if ${sessionBean.lan}=='en'>seelcted='true'</c:if>> <spring:message code="languageName.en"> </ spring: message> </ option>
</ select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文