Using Struts 1.3
I have a login form with username, password and a drop-down with some values like English, Deutsh and so on. When a form is submitted, the last value need to be persisted from the drop-down.
Following the code sample.
Form Bean :
public class LoginForm extends ActionForm {
private String locale;
public void setLocale(String locale) {
this.locale = locale;
}
public String getLocale() {
return locale;
}
}
JSP Code :
<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>
Now this JSP code generates the following drop down:
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>
Question :
Now the problem is, when i enter the invalid username, password and select the language say Deutsch. the last language selected should be persisted and shown in the drop down as selected. But the drop down is initialized and the value is set to English.
When a form is submitted, the fields are validated. if they are not valid, then the error message is displayed and all the values are initialized to null. Now in my case i want to store the selected value of drop down and want to shown that value after validation (for invalid user).
Modified the code :
In Bean class
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;
}
}
In jsp :
<html:select property="locale" onchange="refreshPage(this.value)">
<html:optionsCollection property="dropDownList"/>
</html:select>
Okay!!.
First dont use Scriptlet tags thats bad practice.
Since you are using
Struts1.x
struts supports Select tag with their Tag library.Below is the example code fragments to use dorpdown.
Create a memeber element 'langType' type
List
orMap
in your Form Class. Set th list or map into thatlangType
using setters in form.Even when you redirect to same page it will hold the value for you in the dropdown that you selected previously. But You need to set the dropdown everytime whenever you wanted to show.
Check this question answer they have explained how to use list as dropdown in Stuts1.x Use List as Dropdown
Where to set the DropDownList
Dont set the dropdownlist in JSP using scriptlet tag.
Step1
Before you come to your Login Page (Assuming it should hit a action class) in that action class set your drop downlist.
Before you come to your login page (Assuming it wont hit action class) then keep another layer of Jsp or redirect action which will hit action class so in action class you set your dropdown list.