you should be using the struts html tags if you want to use the form bean...or else 
you would have to create the form bean object and explicitly set the values for each 
input tag.
i suggest using the html tags...something like this:
 
YOUR EXAMPLE
<input type="text" name="UserName" value="" size="30"
maxlength="20">
 
 
STRUTS EXAMPLE
<html:text property="userName" size="30" maxlength="20" />
 
 
 
if you use the struts example for all your input fields the form bean will work 
properly. go to the struts site for more documentation on these tags.
 
HTH
amir

 

>>> [EMAIL PROTECTED] 9:00:06 AM 02/19/02 >>>
Hi,

I am quite new to Struts and have a problem with an Action and ActionForm. I
am running Struts 1.0.1 and Tomcat 4.0.1, my IDE is JBuilder 6 Enterprise.

I have the problem, that all the properties of a form are empty. When
running the program in the debugger I can see the following sequence of
operations being executed:

1. Constructor of my FormBean
2. Reset method
3. Validate method

All the setters are NOT called for whatever reason!

Here the simple Java Code of my Form:

public final class LogonForm extends ActionForm
{
private String mContainerName = "";
private String mUserName = "";
private String mPwd = "";
private String mAuthType = "";

    public LogonForm()
    {
        System.out.println("Form created.");
    }

public String getContainerName()
{
  return mContainerName;
}

public void setContainerName(String ContName)
{
  mContainerName = ContName;
}

public String getUserName()
{
     return mUserName;
}

public void setUserName(String UserName)
{
     mUserName = UserName;
}

public String getPassword()
{
     return mPwd;
}

public void setPassword(String Password)
{
     mPwd = Password;
}

public String getAuthType()
{
     return mAuthType;
}

public void setAuthType(String AuthType)
{
     mAuthType = AuthType;
}

    public void reset(ActionMapping mapping, HttpServletRequest request)
{
        mPwd = "";
        mUserName = "";
        mContainerName = "";
        mAuthType = "";
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
{
        System.out.println(mContainerName+ " " + mUserName);
        ActionErrors errors = new ActionErrors();
        if ((mUserName == null) || (mUserName.length() < 1))
            errors.add("username", new
ActionError("error.username.required"));
        if ((mPwd == null) || (mPwd.length() < 1))
            errors.add("password", new
ActionError("error.password.required"));
  if ((mContainerName == null) || (mContainerName.length() < 1))
   errors.add("containername", new
ActionError("error.containername.required"));

        return errors;
    }
}


The HTML looks as follows:

<form action=/Echnaton/authenticate.do METHOD=POST  >
<input type="Hidden" name="AuthType" value="WindowsNT" />
<table align="center" cellspacing="2" cellpadding="2" border="0">
<tr>
    <td rowspan="4"><img src="../Images/Logon.gif" width="192" height="146"
border="0" alt=""></td>
    <td width="120" id="FormFont">NT Domain:</td>
<td width="200" id="FormFont"><input type="text" name="ContainerName"
value="" size="30" maxlength="16"></td>
</tr>
<tr>
    <td id="FormFont">Your User ID:</td>
    <td id="FormFont"><input type="text" name="UserName" value="" size="30"
maxlength="20"></td>

</tr>
<tr>
    <td id="FormFont">Password:</td>
    <td id="FormFont"><input type="password" name="Password" value=""
size="30" maxlength="20"></td>

</tr>
<tr>
<td colspan="2" id="FormFont">
<input type="submit" value="Logon">
<input type="Reset" value="Clear">
</td>
</tr>
</table>

</form>

Here I must add, that this code is from a velocity macro, since I use
Velocity instead of JSP as Template for my 'View' layer in the MVC
architecture.

The action is implemented as follows:


public class authenticate extends EWCAction
{

    public authenticate()
    {
    }

    public ActionForward perform(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
        // Extract attributes we will need
  StringBuffer AuthError = new StringBuffer(256);
  Locale locale = getLocale(request);
  MessageResources messages = getResources();

  // Validate the request parameters specified by the user
  ActionErrors errors = new ActionErrors();
  String container = ((LogonForm)form).getContainerName();
  String username = ((LogonForm)form).getUserName();
  String password = ((LogonForm)form).getPassword();
  String authtype = ((LogonForm)form).getAuthType();
  EWCInit ei =
(EWCInit)request.getSession().getServletContext().getAttribute("EWCInit");
  String IOR = ei.getFactoryIOR();
  //
  // do authentication
  //
   // .... do some CORBA processing here

  // Report any errors we have discovered back to the original form
  if (!errors.empty())
  {
   saveErrors(request, errors);
     return (new ActionForward(mapping.getInput()));
  }

  // Forward control to the specified success URI
  return (new ActionForward("/startApp.jsp"));
    }

    boolean doNTAuthentication(HttpSession session, String IOR, String
Container,
                            String UserName, String Pwd, StringBuffer
ErrMsg)
    {
        // .. a lot of CORBA Calls
        return ret;
    }
}


I have derived Action with my own action class which basically should check
the sessionstatus whenever a call is made. It looks as follows:

public class EWCAction extends Action
{

    public EWCAction()
    {
  super();
    }

public ActionForward perform(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
HttpServletResponse response)
    throws java.io.IOException, javax.servlet.ServletException
{
     HttpSession session = request.getSession();
  ActionForward fwd = null;
  String AuthStatus = (String)session.getAttribute("AuthStatus");
  if (AuthStatus != null) // session is OK
  {
   if (AuthStatus.equalsIgnoreCase("SUCCESS")) // user is authenticated
    return null;
  }
  else // user needs to login
  {
   fwd = new ActionForward("/Templates/LogonForm.vm");
      return fwd;
  }

  return (super.perform(mapping, form, request, response));
}

}


In my web.xml file I do a mapping for all actions ending with do to the
action servlet as follows:

  <servlet>
    <servlet-name>action</servlet-name>
    <display-name>action</display-name>
    <description>Standard action servlet for MVC architecture</description>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


My struts-config.xml file looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd";>
<struts-config><data-sources />
  <!-- ========== Form Bean Definitions
=================================== -->
  <form-beans type="org.apache.struts.action.ActionFormBean">

    <form-bean name="LogonForm" type="ch.smartsol.EWC.forms.LogonForm" />

  <form-bean name="ShowNTUserPropsForm"
type="ch.smartsol.EWC.ShowNTUserPropsForm" /></form-beans>

  <!-- ========== Action Mapping Definitions
============================== -->
  <action-mappings type="org.apache.struts.action.ActionMapping">

    <action path="/authenticate" type="ch.smartsol.EWC.actions.authenticate"
name="LogonForm" scope="request" validate="true"
input="/Templates/LogonForm.vm">
    </action>

    <!-- The standard administrative actions available with Struts -->
    <!-- These would be either omitted or protected by security -->
    <!-- in a real application deployment -->

    <action path="/admin/addFormBean"
type="org.apache.struts.actions.AddFormBeanAction" name="&lt;none&gt;" />
    <action path="/admin/addForward"
type="org.apache.struts.actions.AddForwardAction" />
    <action path="/admin/addMapping"
type="org.apache.struts.actions.AddMappingAction" />
    <action path="/admin/reload"
type="org.apache.struts.actions.ReloadAction" />
    <action path="/admin/removeFormBean"
type="org.apache.struts.actions.RemoveFormBeanAction" />
    <action path="/admin/removeForward"
type="org.apache.struts.actions.RemoveForwardAction" />
    <action path="/admin/removeMapping"
type="org.apache.struts.actions.RemoveMappingAction" />

  </action-mappings>
</struts-config>


I have checked the names of my form tags with the names of my setter and
getter functions and they are correct. (I assume that the HTML Form name
UserName requires a setUserName() and getUserName() method in my Form Bean.)

However I do absolutely NOT understand, why the set methods are NOT called.
Can anybody give me a hint what I am doing wrong. In the debugger I can see
the request object. It has the correct URL with all the parameters and
correct values.

Any help would be greatly appreciated.


Thomas


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>



Reply via email to