I'm new to
Struts and JSP-Programming. I'm trying to use a DynaActionForm which has a certain count of predefined form-properties (strfield1 ... strfield4). Additional I've another logic which is located in the form implementation which assigns a property-name, property-type to a field-name. It should be a simple form generator which uses the predefined fields and assigns them a custom label/name and a user defined type (like TextField, PasswordField, NumberField, ...). The aim is to evaluate the type-field in the jsp-page and to display an adequate html-element.
In the TestDynaAction the service is only used to get some
test data. The logging can be also neglected. In the struts-config.xml the "definitions" property is not defined explicitely as form-property, but it is implemented in the BaseDynaActionForm.
I'm using c
ut for output and not bean:write, because some book authors claim that it will be soon deprecated. The c
ut and html:text tags may be not fully correct, but I have never been coming to this place in the code.
The request is started with "..../testDyna.do". I'm using
Tomcat 5.5 with
Java 1.4.7 and Taglib Version 1.1.
The request is answered with the already well discussed error message, but the hints given have not solved my problem. I have a getter method and my dynTestForm is not replaced in the action form by sth.:
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: No getter method for property definitions of bean dynTestForm
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.pages.dyna.TestDyna_jsp._jspService(org.apache.jsp.pages.dyna.TestDyna_jsp:231)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.tiles.TilesRequestProcessor.internalModuleRelativeForward(TilesRequestProcessor.java:345)
org.apache.struts.action.RequestProcessor.processForward(RequestProcessor.java:582)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:260)
ixos.webclient.base.BaseRequestProcessor.process(BaseRequestProcessor.java:42)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
---------------------------------------------------------------------------
struts-config.xml:
<form-bean name="dynTestForm" type="ixos.webclient.base.BaseDynaActionForm">
<form-property initial="" name="strfield1" type="java.lang.String"/>
<form-property initial="0" name="strfield2" type="java.lang.String"/>
<form-property initial="0" name="strfield3" type="java.lang.String"/>
<form-property initial="false" name="strfield4" type="java.lang.String"/>
</form-bean>
<action path="/testDyna"
type="ixos.webclient.action.TestDynAction"
forward="/pages/dyna/TestDyna.jsp"
name="dynTestForm"
parameter="direction"
scope="session"/>
<action path="/testDyna_post"
type="ixos.webclient.action.TestDynaAction"
attribute="dynTestForm"
name="dynTestForm"
scope="session"
input="/pages/dyna/TestDyna.jsp"
validate="false"
>
<forward name="success" path="/pages/dyna/success.jsp" contextRelative="true" />
<forward name="error" path="/pages/dyna/error.jsp" contextRelative="true" />
</action>
--------------------------------------------------------------------------
TestDynaAction.java:
package ixos.webclient.action;
import ixos.base.log4j.Log;
import ixos.webclient.Const;
import ixos.webclient.base.BaseDynaActionForm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TestDynaAction extends AbstractAction {
/** Class identifier for logging */
private static final Log.Context LC = new Log.Context(TestDynaAction.class.getName());
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
final
String MN = "execute";
LC.info(MN, "Executing...");
// set the available values as session attributes
GutlService gutlService = new GutlService();
HttpSession session = request.getSession();
Collection orders = gutlService.getOrders();
Collection types = gutlService.getTypes();
Collection fields = gutlService.getFields();
session.setAttribute("orders", orders);
session.setAttribute("types", types);
session.setAttribute("fields", fields);
BaseDynaActionForm dynForm = (BaseDynaActionForm)form;
dynForm.setPropertyNames(orders);
dynForm.setPropertyTypes(types);
dynForm.setFieldNames(fields);
Iterator types_it = types.iterator();
Iterator fields_it = fields.iterator();
for(Iterator order_it = orders.iterator(); order_it.hasNext()
{
String key = (String)order_it.next();
String typ = (String)types_it.next();
String field = (String)fields_it.next();
processProperty(dynForm, key, typ, field);
}
return mapping.findForward(Const.SUCCESS);
}
private void processProperty(BaseDynaActionForm form, String key, String type, String field) {
final String MN = "processProperty";
if(key == null || key.equals("")) {
LC.error(MN, "Received property name is null or empty", null);
return;
}
if(type == null || type.equals("")) {
LC.error(MN, "Received property type for '" + key + "' is null or empty", null);
return;
}
if(field == null || field.equals("")) {
LC.error(MN, "Received field name for '" + key + "' is null or empty", null);
return;
}
Object val = form.get(field);
if(val == null) {
LC.error(MN, "Got for property '" + key + "' and field '" + field + "' null value", null);
return;
}
if(val instanceof String) {
String str = (String)val;
LC.info(MN, "Got property '" + key + "' = " + str + " (Type: " + type + ")");
} else if(val instanceof String[]) {
String[] arr = (String[])val;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < arr.length; i++) {
if(i > 0) {
sb.append(" ");
}
sb.append(arr[i]);
}
LC.info(MN, "Got string array property '" + key + "' = " + sb.toString());
} else {
LC.warn(MN, "Got for property '" + key + "' and field '" + field + "' not supported value");
return;
}
return;
}
}
class GutlService {
public GutlService() {
}
public Collection getOrders() {
ArrayList orders = new ArrayList();
orders.add("Archive name:");
orders.add("Pool name:");
orders.add("Execute now:");
return orders;
}
public Collection getTypes() {
ArrayList types = new ArrayList();
types.add("TextField");
types.add("TextField");
types.add("BooleanField");
return types;
}
public Collection getFields() {
ArrayList fields = new ArrayList();
fields.add("strfield1");
fields.add("strfield2");
fields.add("strfield3");
return fields;
}
}
---------------------------------------------------------------------------
BaseDynaActionForm.java:
package ixos.webclient.base;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.struts.action.DynaActionForm;
public class BaseDynaActionForm extends DynaActionForm {
private ArrayList _propertyNames;
private ArrayList _propertyTypes;
private ArrayList _fieldNames;
/** helper class to have only one iterator over the items */
public static class Definition {
private String _name;
private String _type;
private String _field;
public Definition(String name, String type, String field) {
_name = name;
_type = type;
_field = field;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public String getType() {
return _type;
}
public void setType(String type) {
_type = type;
}
public String getField() {
return _field;
}
public void setField(String field) {
_field = field;
}
}
/** This is the method called to get a list to iterate through from
JSP */
public List getDefinitions() {
int size = _propertyNames.size();
ArrayList definitions = new ArrayList(size);
for(int i = 0; i < size; i++) {
definitions.add(new Definition((String)_propertyNames.get(i),
(String)_propertyTypes.get(i),
(String)_fieldNames.get(i)));
}
return definitions;
}
public void setPropertyNames(Collection props) {
ArrayList arr = new ArrayList();
for(Iterator it = props.iterator(); it.hasNext()
{
String name = (String)it.next();
arr.add(name);
}
_propertyNames = arr;
}
public void setPropertyTypes(Collection types) {
ArrayList arr = new ArrayList();
for(Iterator it = types.iterator(); it.hasNext()
{
String type = (String)it.next();
arr.add(type);
}
_propertyTypes = arr;
}
public void setFieldNames(Collection names) {
ArrayList arr = new ArrayList();
for(Iterator it = names.iterator(); it.hasNext()
{
String field = (String)it.next();
arr.add(field);
}
_fieldNames = arr;
}
}
--------------------------------------------------------------------------
TestDyna.jsp:
<%@ page language="java"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>TestDyna.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is dyn action form test page">
</head>
<html:form action="/testDyna_post" focus="strfield1">
<body>
<H2>Testing dyna action form</H2>
<table>
<tr>
<td>
<c
ut value="Property" />
</td>
<td>
<c
ut value="Value" />
</td>
</tr>
<!-- iterate through properties -->
<logic:iterate id="def" name="dynTestForm" property="definitions" scope="session">
<tr>
<td>
<c
ut value="${def.name}" />
</td>
<td>
<html:text name="def" property="${field}" size="20"/>
</td>
</tr>
</logic:iterate>
</table>
<html:submit value="Ok" styleId="submit" />
<html:cancel value="Cancel" styleId="cancel" />
<br>
</body>
</html:form>
</html:html>
Thanks in advance
Chris