Solved

Struts - Error....Please help

Posted on 2006-07-17
2
652 Views
Last Modified: 2013-11-24
Hi Guys,

I'm getting the following EXCEPTION while running the basic struts applicaiton -
exception

javax.servlet.ServletException: Cannot retrieve mapping for action /login
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:113)
      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 retrieve mapping for action /login
      org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:810)
      org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
      org.apache.strutsel.taglib.html.ELFormTag.doStartTag(ELFormTag.java:324)
      org.apache.jsp.index_jsp._jspx_meth_html_form_0(index_jsp.java:194)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:103)
      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)

Here are my files

index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- JSTL tag libs --%>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<%-- Struts provided Taglibs --%>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html-el.tld" %>
<html:html locale="true"/>
<head>
      <fmt:setBundle basename="ApplicationResources" />
      <title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName">
      <table align="center">
              <tr align="center">
                <td><H1><fmt:message key="login.message"/></H1></td>
              </tr>
              <tr align="center">
                  <td>
                          <table align="center">
                                <tr>
                                  <td align="right">
                                          <fmt:message key="login.username"/>
                                    </td>
                                       <td align="left">
                                          <html:text       property="userName"
                                                          size="15"
                                                          maxlength="15" />
                                          <html:errors property="userName" />
                                    </td>
                                </tr>      
                                <tr>
                                       <td align="right">
                                          <fmt:message key="login.password"/>
                                    </td>
                                       <td align="left">
                                          <html:password       property="password"
                                                                size="15"
                                                                maxlength="15"
                                                                redisplay="false"/>
                                          <html:errors property="password" />
                                    </td>
                               </tr>      
                               <tr>
                                    <td colspan="2" align="center">
                                          <html:submit>
                                                <fmt:message key="login.button.signon"/>
                                          </html:submit>
                                    </td>
                                </tr>
                          </table>
                  </td>
              </tr>
      </table>
</html:form>
</body>
</html>

loggedin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%-- JSTL tag libs --%>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>

<%-- Struts provided Taglibs --%>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html" %>

<html:html locale="true"/>
<head>
      <fmt:setBundle basename="ApplicationResources" />
      <title><fmt:message key="loggedin.title"/></title>
</head>
<body>
      <H2>
      <fmt:message key="loggedin.msg">
            <fmt:param value='${requestScope.userName}' />
      </fmt:message>
      </H2>
</body>
</html>

LoginForm.java
/**
 *
 */
package dummies.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * @author Jagadeesh Motamarri
 * Jul 17, 2006
 */
public class LoginForm extends ActionForm {
      
      private String userName;
      private String password;
      
      public void reset(ActionMapping mapping, HttpServletRequest request){
            
            userName="";
            password="";
                        
      }
      
      public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
            
            ActionErrors errors = new ActionErrors();
            
            if((userName==null) || (userName.length()<1))
                  errors.add("userName",new ActionError("error.username.required"));
            if((password==null) || (password.length()<1))
                  errors.add("password",new ActionError("error.password.required"));
            
            
            return errors;
            
      }
      
      
      /**
       * @return Returns the password.
       */
      public String getPassword() {
            return password;
      }
      /**
       * @param password The password to set.
       */
      public void setPassword(String password) {
            this.password = password;
      }
      /**
       * @return Returns the userName.
       */
      public String getUserName() {
            return userName;
      }
      /**
       * @param userName The userName to set.
       */
      public void setUserName(String userName) {
            this.userName = userName;
      }
      
      

}


LoginBean.java
package dummies.struts;

import java.util.HashMap;

/**
 * @author Mike Robinson
 *
 */
public class LoginBean
{
      private HashMap validUsers = new HashMap();
      
      /**
       * Constructor for LoginBean
       * Initializes the list of usernames/passwords
       */
      public LoginBean()
      {
            validUsers.put("Twinkle Toes","tt");
            validUsers.put("administrator","admin");
            validUsers.put("Barbara Smith","smitty");
      }
      
      /**
       * determine if the username/password combination are
       * present in the validUsers repository.
       * @param userName
       * @param password
       * @return boolean true if valid, false otherwise
       */
      public boolean validateUser(String userName, String password)
      {
            if(validUsers.containsKey(userName))
            {
                  String thePassword = (String)validUsers.get(userName);
                  if(thePassword.equals(password))
                        return true;
            }
            return false;
      }
}


LoginAction.java
package dummies.struts;

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

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.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Mike Robinson
 *
 */
public class LoginAction extends Action
{
      /**
       * Handles user's request for login
       * @param mapping
       * @param form
       * @param request
       * @param response
       * @throws Exception
       * @return ActionForward
       */
      public ActionForward execute(       ActionMapping mapping,
                                                      ActionForm form,
                                                      HttpServletRequest request,
                                                      HttpServletResponse response)
                                                      throws Exception
      {
            // create a new LoginBean with valid users in it
            LoginBean lb = new LoginBean();
            
            // check to see if this user/password combination are valid
            if(lb.validateUser(((LoginForm)form).getUserName(),((LoginForm)form).getPassword()))
            {
                  request.setAttribute("userName",((LoginForm)form).getUserName());
                  return (mapping.findForward("success"));
            }
            else      // username/password not validated
            {
                  // create ActionError and save in the request
                  ActionErrors errors = new ActionErrors();
                  ActionError error = new ActionError("error.login.invalid");
                  errors.add("login",error);
                  saveErrors(request,errors);
                  
                  return (mapping.findForward("failure"));
            }
      }
}


struts-config.xml

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

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<!-- This is the Struts configuration file for the Login example application -->


<struts-config>
      <!-- ========== Form Bean Definitions =================================== -->  
      <form-beans>
          <form-bean      name="LoginForm"
                            type="dummies.struts.LoginForm"/>
      </form-beans>
               
      <!-- ========== Action Mapping Definitions ============================== -->  
      <action-mappings>    
          <action      path="/Login"
                     type="dummies.struts.LoginAction"
                     name="LoginForm"
                        scope="request"
                    input="/index.jsp"
                    validate="true">
                  <forward name="failure" path="/index.jsp"/>
                    <forward name="success" path="/loggedin.jsp"/>
          </action>

       </action-mappings>
 

       <!-- ========== Message Resources Definitions =========================== -->
        <message-resources      null="false"
                                  parameter="ApplicationResources"/>
   
</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>

        <!-- Action Servlet Configuration -->
        <servlet>
          <servlet-name>action</servlet-name>
          <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
          <init-param>
                  <param-name>config</param-name>
                  <param-value>/WEB-INF/struts-config.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
        </servlet>

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

        <!-- The Welcome File List -->
        <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>

        <!-- JSTL Tag Library Descriptor -->
        <taglib>
          <taglib-uri>/WEB-INF/c.tld</taglib-uri>
          <taglib-location>/WEB-INF/c.tld</taglib-location>
        </taglib>

        <taglib>
          <taglib-uri>/WEB-INF/fmt.tld</taglib-uri>
          <taglib-location>/WEB-INF/fmt.tld</taglib-location>
        </taglib>

        <taglib>
          <taglib-uri>/WEB-INF/sql.tld</taglib-uri>
          <taglib-location>/WEB-INF/sql.tld</taglib-location>
        </taglib>

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

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

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

</web-app>


_____Urgent help required.

Thank You,
____Jags,.
0
Comment
Question by:jagadeesh_motamarri
2 Comments
 
LVL 4

Accepted Solution

by:
Meritor earned 250 total points
ID: 17128267
I think there is a little mistake in your JSP and struts-config.xml
Your Error Message shows that:
 javax.servlet.ServletException: Cannot retrieve mapping for action /login

Your Form tag is:
<html:form action="login.do" focus="userName">

Your Action mapping shows:
         <action     path="/Login"
                    type="dummies.struts.LoginAction" .......................

Which means that the struts is looking for /login path in your struts-config.xml file.
However, your struts-config.xml file contains path="/Login" and at this point it fails to map.

Please try to change <html:form action="Login.do" focus="userName"> in your JSP. or you can change struts-config.xml file.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17129463
When change that -->> <html:form action="Login.do" focus="userName">

i'm getting this exception now -

exception

javax.servlet.ServletException: Exception creating bean of class dummies.struts.LoginForm: {1}
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:113)
      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: Exception creating bean of class dummies.struts.LoginForm: {1}
      org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:563)
      org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:520)
      org.apache.strutsel.taglib.html.ELFormTag.doStartTag(ELFormTag.java:324)
      org.apache.jsp.index_jsp._jspx_meth_html_form_0(index_jsp.java:194)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:103)
      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)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question