Question

I am a novice in struts and I have been trying to build a simple struts application. I have errors which I have been trying to resolve for the past couple of days but unable to solve it. The following are the files that I have used-

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>
        <bean name="myForm" type="com.myForm" />
    </form-beans>

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

view.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!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(Bean class)-

package com;

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(Action class)-

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;
import com.myForm;

public class myAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

        myForm form1=(myForm) form;

        String s="Karthikeyan";

        form1.setSt(s);

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




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

When I compile myAction.java, it gives the following error-

myAction.java:11: cannot find symbol
symbol  : class myForm
location: package com
import com.myForm;
          ^
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;
                              ^

Also, when I run the tomcat server, I get the following errors-

org.apache.commons.digester.Digester error
SEVERE: Parse Error at line 9 column 43: Element type "bean" must be declared.

and

org.apache.commons.digester.Digester error
SEVERE: Parse Error at line 10 column 15: The content of element type "form-beans" must match "(form-bean)*".

Please help me.

Was it helpful?

Solution

you wrote the wrong struts-config.xml, you should change form-beans as below format:

    <form-beans>
        <form-bean name="myForm" type="com.myForm"></form-bean>
    </form-beans>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top