Discussions

Web tier: servlets, JSP, Web frameworks: Problem with basic struts application.

  1. Problem with basic struts application. (12 messages)

    Hi,
    I am new to the Struts framework. I tried developing my first struts application.
    The flow that I would like to create is as follows.
    1. The user will start with http://localhost:8080/babu/index.jsp.
    2. index.jsp has a textbox and a submit button.
    3. if the user enter “1” in the text box and clicks on submit then “Value: One” should be displayed (using “sayone.jsp”)
    4. if the user enter any other text other than “1” in the text box and clicks on submit then “Value: two” should be displayed (using “saytwo.jsp”)

    I have created the necessary mappings in struts-config.xml and web.xml. The Action class file and ActionForm class files have been deployed into <tomcat-install-dir>\ webapps\Babu\WEB-INF\classes. But when I access the URL ( http://localhost:8080/babu/index.jsp ), I get following error message (first few lines included) in browser window

    org.apache.jasper.JasperException: Cannot retrieve definition for form bean IndexActionObj
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)


    Could someone point if there are any mistakes that most starters do?

    I use Tomcat 4.1.30. Have listed files below for your reference.

    TIA,
    Babu

    ------------------------
    struts-config.xml
    ------------------------

    <?xml version="1.0" encoding="ISO-8859-1" ?>

    <!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>

      <!-- ========== Form Bean Definitions ================= -->
      <form-beans>

        <form-bean name="IndexForm"
                        type="IndexForm"/>

      </form-beans>

      <!-- ========== Action Mapping Definitions ============ -->
      <action-mappings>

        <action path="/usersubmit"
                  type="IndexAction"
                  name="IndexActionObj">
    <forward name="sayone" path="/sayone.jsp"/>
    <forward name="saytwo" path="/saytwo.jsp"/>
        </action>

      </action-mappings>

    </struts-config>

    ------------------------
    Web.xml
    ------------------------

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
      "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

    <web-app>

      <!-- Standard Action Servlet Configuration (with debugging) -->
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
    org.apache.struts.action.ActionServlet
    </servlet-class>
        <init-param>
          <param-name>application</param-name>
          <param-value>ApplicationResources</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>debug</param-name>
          <param-value>2</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>2</param-value>
        </init-param>
        <init-param>
          <param-name>validate</param-name>
          <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
      </servlet>

     
      <!-- Standard Action Servlet Mapping -->
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>

     
      <!-- Struts Tag Library Descriptors -->
      <taglib>
        <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
      </taglib>

      <taglib>
        <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
      </taglib>

      <taglib>
        <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
      </taglib>

    </web-app>

    ------------------------
    Index.jsp
    ------------------------

    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    <html:form action="usersubmit">
    <html:text property="SourceValue" /><br>
    <html:submit/>
    </html:form>
    </body>
    </html>

    ------------------------
    sayone.jsp
    ------------------------

    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    Value: One
    </body>
    </html>


    ------------------------
    saytwo.jsp
    ------------------------

    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    Value: two
    </body>
    </html>

    Threaded Messages (12)

  2. action path="/usersubmit"
                  type="IndexAction"
                  name="IndexActionObj">
    <forward name="sayone" path="/sayone.jsp"/>
    <forward name="saytwo" path="/saytwo.jsp"/>
    </action>
    Your Action tag has name="IndexActionObj", but there is no such defenition in your form bean, If the IndexForm is the one you use then, give that name in the name element of your action tag.

    NOTE: You may need to give the complete path with package name in the type element of both action and form-bean tags.

    Hope this helps,
    Thanks,
    Senthil.
  3. Thanks for the clarification Senthil. That error is now resolved.

    But i am starting to get another error. The error (upon access of http://localhost:8080/babu/index.jsp) now is

    org.apache.jasper.JasperException: No getter method for property SourceValue of bean org.apache.struts.taglib.html.BEAN
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

    I have checked my ActionForm.java (listed below) and it has the getters. what could be causing this error?

    Apologies for repeated posting.

    TIA,
    Babu

    ---------------
    IndexForm.java
    ---------------

    import org.apache.struts.action.ActionForm;


    public final class IndexForm extends ActionForm {
    String SourceValue;
    public String getSourceValue() {
    return this.SourceValue;
    }
    public void setSourceValue(String ValueToSet) {
    this.SourceValue = ValueToSet;
    }
    }

    ----------------
    IndexAction.java
    ----------------

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;


    public class IndexAction extends Action {

    public ActionForward execute(
    ActionMapping Mapping,
    ActionForm Form,
    HttpServletRequest Request,
    HttpServletResponse Response)
    throws Exception {
    IndexForm MyIndexForm = (IndexForm)Form;
    String ValueEntered = MyIndexForm.getSourceValue();
    String Goto = null;
    if (ValueEntered.equals("1"))
    {
    Goto = new String("sayone");
    }
    else
    {
    Goto = new String("saytwo");
    }
    return Mapping.findForward(Goto);

    }

    }

    --------------------------------------------------
    struts-config.xml (after the suggested correction)
    --------------------------------------------------

    <?xml version="1.0" encoding="ISO-8859-1" ?>

    <!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>

      <!-- ========== Form Bean Definitions ================= -->
      <form-beans>

        <form-bean name="IndexForm"
                        type="IndexForm"/>

      </form-beans>

      <!-- ========== Action Mapping Definitions ============ -->
      <action-mappings>

        <action path="/usersubmit"
                  type="IndexAction"
                  name="IndexForm">
    <forward name="sayone" path="/sayone.jsp"/>
    <forward name="saytwo" path="/saytwo.jsp"/>
        </action>

      </action-mappings>

    </struts-config>
  4. Can you try changing the attribute name from SourceValue to sourceValue in your form class? And also in your jsp, refer sourceValue. I don't think it may resolve, but lets try.

    Please use proper Java Naming conventions for your variable names.

    Thanks,
    Senthil.
  5. Thanks a lot Senthil. It works perfect now.

    Babu.

    ------------------------------------------------------
    IndexForm.java (after the suggested change)
    ------------------------------------------------------


    import org.apache.struts.action.ActionForm;
    public final class IndexForm extends ActionForm {
    String sourceValue = "";

    public String getSourceValue() {
    return sourceValue;
    }

    public void setSourceValue(String valueToSet) {
    sourceValue = valueToSet;
    }
    }

    ------------------------------------------------------
    index.jsp (after the suggested change)
    ------------------------------------------------------


    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    hello there
    <html:form action="usersubmit">
    <html:text property="sourceValue" /><br>
    <html:submit/>
    </html:form>

    </body>
    </html>
  6. Cool
  7. Great help![ Go to top ]

    Senthil, your tip was of great help.
    I also spent my half day figuring out whay the problem could be and seacrched the web desparately for the solution.

    It just worked after I changed my property name from "Desc" to "desc".

    It's a bug!!!!!!!
  8. Not a "bug"[ Go to top ]

    Unless you mean it was a bug in your own code, this is not a bug. It is a feature.

    Struts uses reflection and requires you to follow standard JavaBeans naming conventions. The result of this is that if you want to do "setSomething()", you must have a variable exactly named "something".

    Read the rules, then follow them.
  9. Problem with basic struts application[ Go to top ]

    Hi

    I am new to Struts .
    While I have run the sample struts given below ,getting the following error.

    javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
    at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:798)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
    at _webapp._basic._index._jspService(_index.java:61)
    at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java, Compiled Code)
    at oracle.jsp.app.JspApplication.dispatchRequest(JspApplication.java, Compiled Code)
    at oracle.jsp.JspServlet.doDispatch(JspServlet.java, Compiled Code)
    at oracle.jsp.JspServlet.internalService(JspServlet.java, Compiled Code)
    at oracle.jsp.JspServlet.service(JspServlet.java, Compiled Code)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java, Compiled Code)
    at org.apache.jserv.JServConnection.processRequest(JServConnection.java, Compiled Code)
    at org.apache.jserv.JServConnection.run(JServConnection.java, Compiled Code)
    at java.lang.Thread.run(Thread.java, Compiled Code)

    Advance thanks to help.
    Pls help me to run the application


    index.jsp
    =========
    <%@ page language="java" %>
    <%@ taglib uri="/webapp/basic/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/webapp/basic/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/webapp/basic/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    hello there
    <html:form action="usersubmit">
    <html:text property="sourceValue" /><br>
    <html:submit/>
    </html:form>

    </body>
    </html>


    sayone.jsp
    ==========
    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    Value: One
    </body>
    </html>

    saytwo.jsp
    ==========

    <%@ page language="java" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    <html>
    <body>
    Value: two
    </body>
    </html>

    /WEB-INF
    ========
    struts-bean.tld
    struts-html.tld
    struts-logic.tld

    web.xml
    struts-config.xml


    web.xml
    =======

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
    <web-app>
    <!-- Standard Action Servlet Configuration (with debugging) -->
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>application</param-name>
          <param-value>ApplicationResources</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>debug</param-name>
          <param-value>2</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>2</param-value>
        </init-param>
        <init-param>
          <param-name>validate</param-name>
          <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
      </servlet>
      <!-- Standard Action Servlet Mapping -->
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <!-- Struts Tag Library Descriptors -->
      <taglib>
        <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
      </taglib>
      <taglib>
        <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
      </taglib>
      <taglib>
        <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
     </taglib>
    </web-app>


    struts-config.xml
    =================

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!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>
      <!-- ========== Form Bean Definitions ================= -->
      <form-beans>
        <form-bean name="IndexForm" type="IndexForm"/>
      </form-beans>
      <!-- ========== Action Mapping Definitions ============ -->
      <action-mappings>
        <action path="/usersubmit" type="IndexAction" name="IndexForm">
    <forward name="sayone" path="/sayone.jsp"/>
    <forward name="saytwo" path="/saytwo.jsp"/>
        </action>
      </action-mappings>
    </struts-config>

    /WEB-INF/classes
    ================

    IndexAction.class
    IndexForm.class

    /WEB-INF/lib
    ===========

    struts.jar
    jstl.jar
    standard.jar


    Thanks and Regards,
    P.Murukesan
  10. Problem with basic struts application[ Go to top ]

    Hi

    I have resolved the javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection problem
    by changing web.xml file config init param value

    Thanks and Regards,
    P.Murukesan
  11. I have started with simple strut application. I am facing the same problem can u help.
    What change u did in web xml.

    ----- Root Cause -----
    javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
    at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:741)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:443)
  12. I have started with simple strut application. I am facing the same problem can u help.
    What change u did in web xml.

    ----- Root Cause -----
    javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
    at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:741)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:443)
    I have started with simple strut application. I am facing the same problem can u help.
    What change u did in web xml.

    ----- Root Cause -----
    javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
    at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:741)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:443)

    javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans collection org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758) org.apache.jsp.first_jsp._jspService(first_jsp.java:83) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) 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

    javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:798) org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506) org.apache.jsp.first_jsp._jspx_meth_html_form_0(first_jsp.java:135) org.apache.jsp.first_jsp._jspx_meth_html_html_0(first_jsp.java:107) org.apache.jsp.first_jsp._jspService(first_jsp.java:74) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) 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) My JSP:

    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><html:html><head><title>My First Struts</title></head><body><html:form action="/first"><html:text property = "name"/><html:submit/><html:cancel/></html:form><html:errors /></body></html:html>Web.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "actionorg.apache.struts.action.ActionServletconfig/WEB-INF/struts-config.xmldebug3detail31action*.do/WEB-INF/struts-bean.tld/WEB-INF/struts-bean.tld/WEB-INF/struts-html.tld/WEB-INF/struts-html.tld/WEB-INF/struts-logic.tld/WEB-INF/struts-logic.tld/WEB-INF/struts-template.tld/WEB-INF/struts-template.tldstruts-config.xml" rel="nofollow">http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">actionorg.apache.struts.action.ActionServletconfig/WEB-INF/struts-config.xmldebug3detail31action*.do/WEB-INF/struts-bean.tld/WEB-INF/struts-bean.tld/WEB-INF/struts-html.tld/WEB-INF/struts-html.tld/WEB-INF/struts-logic.tld/WEB-INF/struts-logic.tld/WEB-INF/struts-template.tld/WEB-INF/struts-template.tldstruts-config.xml<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config> <form-beans> <form-bean name="formone" type="com.youcompany.struts.form.FirstForm" /> </form-beans>  <action-mappings> <action path="/first" type="com.youcompany.struts.action.FirstAction" name="formone" scope="request" input="/first.jsp"<forward name="success" path="/success.jsp" /> </action>   </action-mappings>  <message-resources parameter="com.youcompany.struts.ApplicationResources" /></struts-config>                          

    Action class:

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

      

     

     

     

     

     

     

    /*

    * Created on Feb 13, 2010

    *

    * To change the template for this generated file go to

    * Window>Preferences>Java>Code Generation>Code and Comments

    */

    package

     

    com.youcompany.struts.action;

    import

     

    javax.servlet.http.HttpServletRequest;

    import

     

    javax.servlet.http.HttpServletResponse;

    import

     

    org.apache.struts.action.Action;

    import

     

    org.apache.struts.action.ActionForm;

    import

     

    org.apache.struts.action.ActionForward;

    import

     

    org.apache.struts.action.ActionMapping;

    import

     

    com.youcompany.struts.form.FirstForm;

    /**

     

    * @author

    Bhuvaneswaran

     

    *

     

    * To change the template for this generated type comment go

    to

     

    * Window>Preferences>Java>Code Generation>Code and

    Comments

     

    */

    public

     class FirstAction extends Action{

     

    public ActionForward execute(ActionMapping mapping,

    ActionForm form,

    HttpServletRequest request,

    HttpServletResponse response)

     

    throws

    Exception {

    FirstForm formone = (FirstForm) form;

     

    String name = formone.getName();

     

    //request.setAttribute("name", name);

     

     

    // Forward control to the specified success target

     

    return (mapping.findForward("success"));

     
       

     

    }

    }

    Actionform class:

    /*

    * Created on Feb 13, 2010

    *

    * To change the template for this generated file go to

    * Window>Preferences>Java>Code Generation>Code and Comments

     public class FirstForm extends ActionForm {  

     

    private String name;

     

     

     

     

    /**

     

     

     

    * @author Bhuvaneswaran

     

     

     

     

    *

     

     

     

    * To change the template for this generated type comment go to

     

     

     

    * Window>Preferences>Java>Code Generation>Code and Comments

     

     

     

    */

     public String getName() {

    }

     

    /** 

     

    * @param string
     

     

    */
     

     

    public void setName(String name) {

     

    this.name = name;

    }

    }

    ?

    ?

     

    return name;

     

     

     

     

    */package com.youcompany.struts.form;

    import  org.apache.struts.action.ActionForm;

  13. It is very uselful.....