Link to home
Start Free TrialLog in
Avatar of benk-master-flash
benk-master-flash

asked on

STRUTS HELP -- cannot resolve symbol variable MESSAGES_KEY

IN ATTEMPTING TO COMPILE THE CODE BELOW THE FOLLOWING ERROR IS GENERATED PREVENTING THE SUCCESSFUL DEPLOYMENT OF THE WEBAPP

"cannot resolve symbol variable MESSAGES_KEY"

the following is the offending line of code and whole class

MessageResources resources = (MessageResources)req.getAttribute(ActionMapping.MESSAGES_KEY);

entire class below -->

package com.oreilly.struts.banking.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;


/**
 * This ActionForm is used by the online banking appliation to validate
 * that the user has entered an accessNumber and a pinNumber. If one or
 * both of the fields are empty when validate is called by the
 * ActionServlet, error messages are created.
 */
public class LoginForm extends ActionForm {
  // The user's private id number
  private String pinNumber;
  // The user's access number
  private String accessNumber;

  public LoginForm() {
    super();
    resetFields();
  }
  /**
   * Called by the framework to validate the user has entered the
   * accessNumber and pin fields.
   */
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest req ){
    ActionErrors errors = new ActionErrors();

    // Get access to the message resources for this application
    // There's not an easy way to access the resources from an ActionForm
    MessageResources resources = (MessageResources)req.getAttribute(ActionMapping.MESSAGES_KEY);
      
    // Check and see if the access number is missing
    if(accessNumber == null || accessNumber.length() == 0) {
      String accessNumberLabel = resources.getMessage( "label.accessnumber" );
      ActionError newError =
        new ActionError("global.error.login.requiredfield", accessNumberLabel );
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
    }

    // Check and see if the pin number is missing
    if(pinNumber == null || pinNumber.length() == 0) {
      String pinNumberLabel = resources.getMessage( "label.pinnumber" );
      ActionError newError =
        new ActionError("global.error.login.requiredfield", pinNumberLabel );
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
    }
    // Return the ActionErrors, in any.
    return errors;
  }

  /**
   * Called by the framework to reset the fields back to their default values.
   */
  public void reset(ActionMapping mapping, HttpServletRequest request) {
    // Clear out the access number and pin number fields
    resetFields();
  }
  /**
   * Reset the fields back to their defaults.
   */
  protected void resetFields() {
    this.accessNumber = "";
    this.pinNumber = "";
  }

  public void setAccessNumber(String nbr) {
    this.accessNumber = nbr;
  }

  public String getAccessNumber() {
    return this.accessNumber;
  }

  public String getPinNumber() {
    return this.pinNumber;
  }
  public void setPinNumber(String nbr) {
    this.pinNumber = nbr;
  }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Perhaps you mean:

MessageResources resources = (MessageResources)req.getAttribute("ActionMapping.MESSAGES_KEY");

since there's no such field in ActionMapping:


http://struts.apache.org/api/org/apache/struts/action/ActionMapping.html
Avatar of benk-master-flash
benk-master-flash

ASKER

actually i meant Action.MESSAGES_KEY
using the "" around Action.MESSAGES_KEY allows it to compile.
MessageResources resources = (MessageResources)req.getAttribute("Action.MESSAGES_KEY");
still can't install, but i think i should be able to fix it.  if not i'll post another comment...

SPOKE TOO SOON

compile generates these errors when putting Action.MESSAGES_KEY in quotes

C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:5: warning: org.apache.struts.action.ActionError in org.apache.struts.action has been deprecated
import org.apache.struts.action.ActionError;
                                ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:42: warning: org.apache.struts.action.ActionError in org.apache.struts.action has been deprecated
      ActionError newError =
      ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:43: warning: org.apache.struts.action.ActionError in org.apache.struts.action has been deprecated
        new ActionError("global.error.login.requiredfield", accessNumberLabel );
            ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:44: warning: GLOBAL_ERROR in org.apache.struts.action.ActionErrors has been deprecated
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
                             ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:44: warning: add(java.lang.String,org.apache.struts.action.ActionError) in org.apache.struts.action.ActionErrors has been deprecated
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
            ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:50: warning: org.apache.struts.action.ActionError in org.apache.struts.action has been deprecated
      ActionError newError =
      ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:51: warning: org.apache.struts.action.ActionError in org.apache.struts.action has been deprecated
        new ActionError("global.error.login.requiredfield", pinNumberLabel );
            ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:52: warning: GLOBAL_ERROR in org.apache.struts.action.ActionErrors has been deprecated
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
                             ^
C:\dev\banking\src\com\oreilly\struts\banking\form\LoginForm.java:52: warning: add(java.lang.String,org.apache.struts.action.ActionError) in org.apache.struts.action.ActionErrors has been deprecated
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
            ^
9 warnings
BUILD FAILED
C:\dev\banking\build.xml:343: FAIL - Encountered exception java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet

now also getting this error
>>Encountered exception java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet


Make sure servlet.jar is in your classpath
You should use ActionMessage instead of ActionError
With Struts 1.2.4, you can find MESSAGES_KEY in org.apache.struts.Globals class, and ActionError is deprecated, use ActionMessage instead.


and replace ActionError.GLOBAL_ERROR by ActionMessages.GLOBAL_MESSAGE
I am having a lot of problems making the changes b/c i am new to struts.  could somebody post the correctly modified code so I can test.   I would be glad to award points to all who have helped.
replacement to org.apache.struts.Globals.MESSAGES_KEY set me on the right path, the other replacements are causing problems for me now.
ASKER CERTIFIED SOLUTION
Avatar of lilian-arnaud
lilian-arnaud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lilan-arnaud --- thanks
that seemed to work, it compiles and installs, but now the only problem is this SERVER(500) error message.  I will find a way to award more points if you can get the app running for me.  FYI it is the banking app from Oreilly's Programming Jakarta Struts 2nd Ed.


Type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Failed to load or instantiate TagExtraInfo class: org.apache.struts.taglib.bean.CookieTei
      org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
      org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:411)
      org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:283)
      org.apache.jasper.compiler.TagLibraryInfoImpl.createTagInfo(TagLibraryInfoImpl.java:422)
      org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:248)
      org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:162)
      org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:418)
      org.apache.jasper.compiler.Parser.parseDirective(Parser.java:483)
      org.apache.jasper.compiler.Parser.parseElements(Parser.java:1539)
      org.apache.jasper.compiler.Parser.parse(Parser.java:126)
      org.apache.jasper.compiler.ParserController.doParse(ParserController.java:220)
      org.apache.jasper.compiler.ParserController.parse(ParserController.java:101)
      org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.ClassNotFoundException: org.apache.struts.taglib.bean.CookieTei
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
      org.apache.jasper.compiler.TagLibraryInfoImpl.createTagInfo(TagLibraryInfoImpl.java:419)
      org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:248)
      org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:162)
      org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:418)
      org.apache.jasper.compiler.Parser.parseDirective(Parser.java:483)
      org.apache.jasper.compiler.Parser.parseElements(Parser.java:1539)
      org.apache.jasper.compiler.Parser.parse(Parser.java:126)
      org.apache.jasper.compiler.ParserController.doParse(ParserController.java:220)
      org.apache.jasper.compiler.ParserController.parse(ParserController.java:101)
      org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
Apache Tomcat/5.0.28
- Did you add struts.jar and all required jars into your WEB-INF/lib directory ?
- Did you add *.tld into WEB-INF directory ?
- Did you install 2 versions of struts into your app server ?