Web.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Smaple</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
struts-config.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources/>
<form-beans>
<form-bean name="OPSFormBean" type="com.test.OptionsSelectionFormBean"/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forward name="error" path="/error.jsp"/>
</global-forwards>
<action-mappings>
<action name="OPSFormBean" path="/optionsSelection" scope="request" type="com.test.OptionsSelectionExampleAction">
<forward name="success" path="/optionsCollectionExample.jsp"/>
</action>
</action-mappings>
<controller/>
<message-resources parameter="com/test/ApplicationResource"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
index.jsp :
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<jsp:forward page="optionsSelection.do"></jsp:forward>
optionsCollectionExample.jsp :
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<html>
<head>
<title>Options Collection Example</title>
</head>
<body>
<html:form action="/optionsSelection">
<h4>Simple use of <html:optionsCollection> Tag</h4>
<html:select property="selectedItem">
<html:option value="Test">Select</html:option>
<html:optionsCollection property="customers" label="name" value="id"></html:optionsCollection>
</html:select>
<html:submit />
</html:form>
</body>
</html>
OptionsSelectionExampleAction.java :
package com.test;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class OptionsSelectionExampleAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("This is Action Classs........................... ");
OptionsSelectionFormBean exampleForm = (OptionsSelectionFormBean) form;
// define a dummy collection
Collection customers = new ArrayList();
customers.add(new Customer(1, "Raju"));
customers.add(new Customer(2, "Rani"));
customers.add(new Customer(3, "Banu"));
exampleForm.setCustomers(customers);
// set the collection in the request
request.setAttribute("customers", customers);
return mapping.findForward("success");
}
}
OptionsSelectionFormBean.java :
package com.test;
import java.util.Collection;
import org.apache.struts.action.ActionForm;
public class OptionsSelectionFormBean extends ActionForm {
private String selectedItem;
private Collection customers;
public String getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}
public Collection getCustomers() {
return customers;
}
public void setCustomers(Collection customers) {
this.customers = customers;
}
}
Customer.java :
package com.test;
public class Customer {
private int id;
private String name;
public Customer() {
}
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLabel() {
return this.name;
}
public int getValue() {
return this.id;
}
}
1 comment:
Thanks for sharing information dude.
Thanks
Javin
Post a Comment