struts例子上的问题

03-09-29 lyojbuilder

在struts-example这个例子中,它的EditRegistrationAction中有一段:

if (form == null) {
            if (log.isTraceEnabled()) {
                log.trace(
                    " Creating new RegistrationForm bean under key "
                        + mapping.getAttribute());
            }
            
            form = new RegistrationForm();
            if ("request".equals(mapping.getScope())) {
                request.setAttribute(mapping.getAttribute(), form);
            } else {
                session.setAttribute(mapping.getAttribute(), form);
            }
        }
<p>

form怎麽会有null的情况?他说可能有误删除attribute="registrationForm" from struts-config-registration.xml的情况,是这样吗?

在logonAction中有一段:

String username = (String) PropertyUtils.getSimpleProperty(form, "username");
        String password = (String) PropertyUtils.getSimpleProperty(form, "password");
<p>

它为什麽不直接从Form中获得username和password,一定要用PropertyUtils类?有什麽好处吗?

请指点

linxxtao
2003-09-29 09:24

// 在此输入java代码
 public static Object getSimpleProperty(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {

        if (bean == null) {
            throw new IllegalArgumentException("No bean specified");
        }
        if (name == null) {
            throw new IllegalArgumentException("No name specified");
        }

        // Validate the syntax of the property name
        if (name.indexOf(NESTED_DELIM) >= 0) {
            throw new IllegalArgumentException
                    ("Nested property names are not allowed");
        } else if (name.indexOf(INDEXED_DELIM) >= 0) {
            throw new IllegalArgumentException
                    ("Indexed property names are not allowed");
        } else if (name.indexOf(MAPPED_DELIM) >= 0) {
            throw new IllegalArgumentException
                    ("Mapped property names are not allowed");
        }

        // Handle DynaBean instances specially
        if (bean instanceof DynaBean) {
            DynaProperty descriptor =
                    ((DynaBean) bean).getDynaClass().getDynaProperty(name);
            if (descriptor == null) {
                throw new NoSuchMethodException("Unknown property '" +
                        name + "'");
            }
            return (((DynaBean) bean).get(name));
        }

        // Retrieve the property getter method for the specified property
        PropertyDescriptor descriptor =
                getPropertyDescriptor(bean, name);
        if (descriptor == null) {
            throw new NoSuchMethodException("Unknown property '" +
                    name + "'");
        }
        Method readMethod = getReadMethod(descriptor);
        if (readMethod == null) {
            throw new NoSuchMethodException("Property '" + name +
                    "' has no getter method");
        }

        // Call the property getter and return the value
        Object value = readMethod.invoke(bean, new Object[0]);
        return (value);

    }

<p>

因为LOGONFORM是动态BEAN所以要用这个方法!

linxxtao
2003-09-29 09:53

 <action    path="/editRegistration"
               type="org.apache.struts.webapp.example.EditRegistrationAction"
          attribute="registrationForm"
              scope="request"
           validate="false">
      <forward name="success"              path="/registration.jsp"/>
    </action>
<p>

因为配置文件中没有NAME属性所以对该ACTION系统不会自动产生实例BEAN,故要在ACTION

中手动生成BEAN.

lyojbuilder
2003-09-29 12:32

多谢,原来它和DynaActionForm的道理是一样的