1

I am new in Struts,I follow a tutorial on how to create a Struts web application. here are the files I created.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 <web-app>
 <display-name>Mon application Struts de tests</display-name>
 <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
          <param-name>config</param-name>
          <param-value>struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Struts-config.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config> 
<form-beans>
   <form-bean name="HelloWorldForm" type="com.hello.HelloWorldFrom" />
   <form-bean name="LoginForm" type="com.test.LoginForm.LoginForm"/>
</form-beans> 
<action-mappings type="org.apache.struts.action.ActionMapping">
   <action path="/helloWorld" type="com.hello"  name="HelloWorldForm">
    <forward name="success" path="/HelloWorld.jsp" />
   </action>
   <action path="login" parameter="" input="/index.jsp" scope="request"
      name="loginForm" type="com.test.controller.LoginAction">
      <forward name="succes" path="/accueil.jsp" redirect="false" />
      <forward name="echec" path="/index.jsp" redirect="false" />
   </action>
</action-mappings> 

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java" %> 
<%@ 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<html:html locale="true">
<head>
<title>authentication</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="login" focus="nameUserr">
  <table border="0" align="center">
    <tr>
      <td align="right">
        User:
      </td>
      <td align="left">
        <html:text property="nameUserr" size="20" maxlength="20"/>
      </td>
    </tr>
    <tr>
      <td align="right">
        Password :
      </td>
      <td align="left">
      <html:password property="psdUser" size="20" maxlength="20"
                      redisplay="false"/>
      </td>
    </tr>
    <tr>
      <td align="right">
        <html:submit property="submit" value="Submit"/>
      </td>
      <td align="left">
        <html:reset/>
      </td>
    </tr>
  </table>
</html:form>

LoginAction.java

package com.test.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import com.test.LoginForm.LoginForm;
public class LoginAction extends Action{

public ActionForward execute(ActionMapping mapping, 
        ActionForm form, 
        HttpServletRequest req, 
        HttpServletResponse res) throws Exception {

            String result = null;
            String nomUtilisateur = ((LoginForm) form).getNameUser();
            String mdpUtilisateur = ((LoginForm) form).getPsdUser();

            if (nameUser.equals("xyz") && psdUser.equals("xyz")) {
            result = "succes";
            } else {
            result = "failure";
            }

            return mapping.findForward(result);
            }
 }

LoginForm.java

package com.test.LoginForm;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm{
  String nameUser;

  String psdUser;

  public String getpsdUser() {
    return psdUser;
  }

  public void setpsdUser(String psdUser) {
    this.psdUser= psdUser;
  }

  public String getnameUser() {
    return nameUser;
  }

  public void setnameUser(String nameUser) {
    this.nameUser= nameUser;
  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    return errors;
  }

  public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.psdUser= null;
    this.nameUser= null;
  }

}

but when running, I got the following error

Etat HTTP 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection

EDIT: the complete stack trace

Report type Exception 

Message javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
description The server encountered an internal error that prevented it from fulfilling the request.

exception
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)

Parent cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException (PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException (PageContextImpl.java:845)
org.apache.jsp.index_jsp._jspService (index_jsp.java:112)
org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)

Parent cause

javax.servlet.jsp.JspException: Can not find ActionMappings gold 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.index_jsp._jspx_meth_html_005fform_005f0 (index_jsp.java:189)
org.apache.jsp.index_jsp._jspx_meth_html_005fhtml_005f0 (index_jsp.java:143)
org.apache.jsp.index_jsp._jspService (index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
Note The complete trace of the mother cause of this error is available in the log files for Apache Tomcat / 7.0.59.

I googled and I tried all the solutions proposed but the error persists. Any idea Please.

P.S : I use Struts 1.1

nayomi
  • 137
  • 1
  • 5
  • 20

1 Answers1

1

The struts-config.xml in your file ends with </action-mappings> instead of <struts-config>

Also there are many errors in each and every file. So it seems you have not made any attempt to verify if your code is correct or not.

1) In web.xml file

There is issue with DTD declaration, it should be like :

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

2) In struts-config.xml file:

You have declared the form bean name as LoginForm where as in the action you are using it as loginForm

Also there is no forward declaration that shows what you want to do when your action class returns failure.

So the code should be like this:

   <action path="/login" parameter="" input="/index.jsp" scope="request"
      name="LoginForm" type="com.test.controller.LoginAction">
      <forward name="succes" path="/accueil.jsp" redirect="false" />
      <forward name="echec" path="/index.jsp" redirect="false" />
      <forward name="failure" path="/loginFailed.jsp" redirect="false" />
   </action>

3) In index.jsp you have multiple issues:

a) The taglib declaration should be like this (for Struts 1.3.10):

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="logic" %>

b) The struts html custom tag do not have any attribute called locale also the index.jsp shows that you are not closing the struts html tag.

Check this:

<html:html>
......
</html:html>

c) The property declaration:

<html:text property="nameUserr" size="20" maxlength="20"/>

says the form bean has a property called nameUserr but the LoginForm has the property declared as nameUser so change the code in the jsp file to

<html:text property="nameUser" size="20" maxlength="20"/>

4) The LoginForm bean has wrong getter and setter methods. They should be like:

public String getNameUser() {
    return nameUser;
}

public void setNameUser(String nameUser) {
    this.nameUser = nameUser;
}

public String getPsdUser() {
    return psdUser;
}

public void setPsdUser(String psdUser) {
    this.psdUser = psdUser;
}

5) Finally in LoginAction action class you the comparison is not correct, it should be:

        String nomUtilisateur = ((LoginForm) form).getNameUser();
        String mdpUtilisateur = ((LoginForm) form).getPsdUser();

        if (nomUtilisateur.equals("xyz") && mdpUtilisateur.equals("xyz")) {
        result = "succes";
        } else {
        result = "failure";
        }
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • I do all the change you specify it above, and I have a compilation error in index.jsp in taglib <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> that **Can not find the tag library descriptor for "http://struts.apache.org/tags-bean"** – nayomi Apr 02 '15 at 11:18
  • @nayomi, what jar file you are using for struts-core, what is its version? The taglib URI mentioned in my answer is based on `Struts 1.3.10` – Chaitanya Apr 02 '15 at 13:37
  • I mentioned that I used struts 1.1 – nayomi Apr 02 '15 at 13:49
  • @nayomi, ok then for Struts 1.1 you need not change the taglib URI's, you can use the same old URI's. – Chaitanya Apr 02 '15 at 14:08
  • I got the same error **Etat HTTP 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection** :( – nayomi Apr 02 '15 at 14:37
  • @nayomi, update your question with complete contents of your `struts-config.xml` file – Chaitanya Apr 02 '15 at 16:37
  • I guess that the problem is not in struts-config.xml but it is related to the required files to run struts application or I have a false configuration because I verified many time my files and all looks fine – nayomi Apr 03 '15 at 11:39
  • @nayomi The struts-config.xml file and the Java code in your question shows that you are having wrong code. If that is the exact code you are using then it wont work. Also if you want to learn then I suggest you to learn Struts-2, because Struts 1 is outdated. – Chaitanya Apr 03 '15 at 12:29
  • I understand you but the choice to work with struts 1 required by the society where I work to complete an old project – nayomi Apr 03 '15 at 12:51