struts error- org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean

StackOverflow https://stackoverflow.com/questions/23152530

문제

I am trying out a simple struts application that will pass a String value from a java class file to a jsp.

Here the corresponding files of the struts application-

web.xml

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

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

<web-app>

    <display-name>Hello World Struts Application</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>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
             <param-name>detail</param-name>
             <param-value>3</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>view.jsp</welcome-file>
    </welcome-file-list>

</web-app>

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">
<struts-config>

    <form-beans>
        <form-bean name="myForm" type="com.myForm" />
    </form-beans>

    <action-mappings>
        <action path="/view" scope="request" name="myForm" type="com.myAction" validate="false">
                <forward name="success" path="/first" />
        </action>
        <action path="/view"
                forward="/view.jsp"/>
        <action path="/first"
                forward="/first.jsp" />
    </action-mappings>
</struts-config>

view.jsp

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <form action="first.do">
            Enter name :
            <input type="text" name="name"/>
            <input type="submit" value="Enter"/>
        </form>

    </body>
</html>

first.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        Welcome!!!!!!!!

        <bean:write name="myForm" property="st"/>
    </body>
</html>

myForm.java

package com;

import javax.servlet.http.HttpServletRequest;

public class myForm extends org.apache.struts.action.ActionForm {

    private String st;

    public String getSt()
    {
        return st;
    }

    public void setSt(String st)
    {
        this.st=st;
    }

    public myForm(String st)
    {
        this.st=st;
    }
}

myAction.java

package com;

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 myAction extends org.apache.struts.action.Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

        myForm form1=(myForm) form;

        String s="Karthikeyan";

        System.out.println("Hello "+s);

        form1.setSt(s);

        //RequestDispatcher reqDispatcher = getRequestDispatcher("view.jsp");
        //reqDispatcher.forward(request,response);




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

Even though I have set the classpath for the class files, I get the following error when I compile myAction.java-

myAction.java:18: cannot find symbol
symbol  : class myForm
location: class com.myAction
                myForm form1=(myForm) form;
                ^
myAction.java:18: cannot find symbol
symbol  : class myForm
location: class com.myAction
                myForm form1=(myForm) form;
                              ^ 

When I try to execute the application on a web browser, I get the following error-

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "myForm" in any scope

I have stuck on this issue for quite sometime without any solution. Please help me out.

도움이 되었습니까?

해결책 2

Create a new project, set classpaths,execute it and it will work :). It worked for me.

다른 팁

you have missed the getter and setter methods for myform class.please add getter and setter methods for myform class in myAction class and add the filters in web.xml.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top