【发布时间】:2015-08-26 18:04:37
【问题描述】:
使用 Struts 1.3
我有一个登录表单,其中包含用户名、密码和一个下拉菜单,其中包含一些值,如英语、德语等。提交表单时,需要从下拉列表中保留最后一个值。
按照代码示例。
表单豆:
public class LoginForm extends ActionForm {
private String locale;
public void setLocale(String locale) {
this.locale = locale;
}
public String getLocale() {
return locale;
}
}
JSP 代码:
<html:select property="locale" onchange="refreshPage(this.value)">
<%
Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
for (int i=0; i<availableLanguages.length; i++) {
String localeNameKey = "com.web.gui.localeName." + availableLanguages[i].toString();
%>
<html:option value="<%= availableLanguages[i].toString() %>"
key="<%= localeNameKey %>"/>
<%
}
%>
</html:select>
现在这个 JSP 代码生成以下下拉列表:
struts-config.xml
<action-mappings>
<action path="/login" type="com.web.gui.LoginAction" name="loginForm" scope="request" input="/loginForm.jsp">
<forward name="setLanguage" path="/loginForm.jsp"/>
</action>
</action-mappings>
问题:
现在的问题是,当我输入无效的用户名、密码并选择德语时。最后选择的语言应保留并显示在下拉菜单中。但是下拉菜单被初始化并且值被设置为英语。
提交表单时,会验证字段。如果它们无效,则显示错误消息并将所有值初始化为 null。现在在我的情况下,我想存储下拉的选定值并希望在验证后显示该值(对于无效用户)。
修改了代码:
在 Bean 类中
public class LoginForm extends ActionForm{
private Collection dropDownList = null;
public void setDropDownList(Collection dropDownList){
this.dropDownList = dropDownList;
}
public Collection getDropDownList(){
if(dropDownList == null){
dropDownList = new Vector(10);
Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
Properties properties = loadProperties(Locale.ENGLISH);
for(int i=0 ; i<availableLanguages.length ; i++){
String localeNameKey = "com.web.gui.localeName." + availableLanguages[i].toString();
String value = availableLanguages[i].toString();
String key = properties.getProperty(localeNameKey);
dropDownList.add(new LabelValueBean(key,value));
}
}
return dropDownList;
}
}
在jsp中:
<html:select property="locale" onchange="refreshPage(this.value)">
<html:optionsCollection property="dropDownList"/>
</html:select>
【问题讨论】:
-
显示你的
struts-config.xml -
添加了 struts-config.xml 文件。请检查。
-
@primitiveType 我已经添加了 struts-config.xml 文件代码。请检查并更新。有没有人可以帮助我解决这个问题。
-
是的,我们都在这里做同样的事情......你需要提供信息
-
你想说的是,如果表单提交并且名称/pass而不是语言有错误,那么语言必须是预先选择的?