Hi please check this example. I could not
test this. Please change if there is any syntax mistakes.
Basically when you submit a jsp with html form element, all html form elements' values are saved inside the FormBean instance, then strut passes this instance to Action class, so you can access this and manipulate. Usually we do this to get values from user and save inside the database.
you want to do it other way around. In this case, first we populate the FormBean with values retrieved from db. Then forward the request to jsp with html form defined. When jsp is rendered, the struts checks the FormBean, if it has values, assign them to html element based on the "property" attribute. This is how you initialize the jsp.
//Action form class.
public class MyForm extends ActionForm {
private
String lastName = "";
public String getLastName() {
return (this.lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
//Action class.
public class MyAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response) {
MyForm f = (MyForm ) form;
//Retrieve db value and assign it.
f.setLastName = "my_DB_value";
return (mapping.findForward("forwardMe"));
}
}
//my.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head><title>Sample</title></head>
<body>
<html:form action="my.do">
<html:select property="lastName"> //Notice the property name; It is same as //FormBean property
<html

ption value="a">value1</html

ption>
<html

ption value="a">value2</html

ption>
<html

ption value="a">value3</html

ption>
</html:select>
<html:submit/>
</html:form>
</body>
</html>
//Struts-config
<action-mappings>
<action path="/my"
type="MyAction"
name="MyForm"
<forward name="forwardMe" path="/my.jsp"/>
</action>
</action-mappings>