I am using DynaValidatorForm.please help me with inserting values in color drop down menu. I have tried belowwith arraylist but was not able to find solution.
add.jspx
<?xml version="1.0" encoding="UTF-8"?> <!-- Document : add Created on : Mar 4, 2012, 7:00:38 PM Author : Administrator --> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:html="http://struts.apache.org/tags-html" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:bean="http://struts.apache.org/tags-bean" version="2.0"> <jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/> <html:xhtml/> <html:html > <head> <title> <bean:message key="add.title"/> </title> </head> <body> <h1> <bean:message key="global.heading"/> <jsp:text></jsp:text> <bean:message key="add.heading"/> </h1> <html:errors/> <html:form action="/addSubmit"> <table class="addedit" > <tr> <td> <bean:message key="pointForm.prompt.x"/> </td> <td><html:text name="pointForm" property="x"/></td> </tr> <tr> <td> <bean:message key="pointForm.prompt.y"/> </td> <td><html:text name="pointForm" property="y"/></td> </tr> <tr> <td> <bean:message key="pointForm.prompt.color"/> </td> <td> <html:select property="color"> <html:options property="colors" /> </html:select> </td> </tr> <tr> <td> <html:submit> <bean:message key="pointForm.add.submit"/> </html:submit> </td> </tr> </table> </html:form> </body> </html:html> </jsp:root>
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="pointForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property type="int" name="id" initial="-1"/> <form-property type="double" name="x" initial="0"/> <form-property type="double" name="y" initial="0"/> <form-property type="java.lang.String" name="color" /> <form-property type="java.util.ArrayList" name="colors" /> </form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards > </global-forwards> <action-mappings> <action name="pointForm" path="/add" scope="request" type="com.myapp.sruts.NewAction" validate="false"> <forward name="success" path="/WEB-INF/add.jspx"/> </action> <action path="/addSubmit" name="pointForm" scope="request" inpu="/add.do" type="com.myapp.struts.AddAction"/> </action-mappings> <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="com/myapp/struts/messages"/> <!-- ========================= Tiles plugin ===============================--> <!-- This plugin initialize Tiles definition factory. This later can takes some parameters explained here after. The plugin first read parameters from web.xml, thenoverload them with parameters defined here. All parameters are optional. The plugin should be declared in each struts-config file. - definitions-config: (optional) Specify configuration file names. There can be several comma separated file names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if the Tiles definition factory is module aware. If true (default), there will be one factory for each Struts module. If false, there will be one common factory for all module. In this later case, it is still needed to declare one plugin per module. The factory will be initialized with parameters found in the first initialized plugin (generally the one associated with the default module). true : One factory per module. (default) false : one single shared factory for all modules - definitions-parser-validate: (optional) Specify if xml parser should validate the Tiles configuration file. true : validate. DTD should be specified in file header (default) false : no validation Paths found in Tiles definitions are relative to the main context. --> <plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> </plug-in> <!-- ========================= Validator plugin ================================= --> <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>
NewAction.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; import java.util.HashMap; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.*; import org.apache.struts.action.*; import java.util.ArrayList; /** * * @author Administrator */ public class NewAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private static final String SUCCESS = "success"; /** * This is the action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws java.lang.Exception * @return */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaValidatorForm pointForm = (DynaValidatorForm) form; ArrayList colorList = new ArrayList(); return mapping.findForward(SUCCESS); } }
messages.properties
global.heading=Points: global.title=Points: add.heading=Add add.title=Add add.submit=Add Point pointForm.prompt.x=X: pointForm.prompt.y=Y: pointForm.prompt.color=Color: pointForm.add.submit=add
AddAction.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.myapp.struts; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; /** * * @author Administrator */ public class AddAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private static final String SUCCESS = "success"; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaValidatorForm pointForm= (DynaValidatorForm) form; String x1=pointForm.get("x").toString(); double x=Double.parseDouble(BeanUtils.getProperty(pointForm,"x")); double y=Double.parseDouble(BeanUtils.getProperty(pointForm,"y")); String color=(BeanUtils.getProperty(pointForm,"color")); System.out.println("x is "+ x1); return mapping.findForward(SUCCESS); } }