Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts Error : Exception creating bean of class books.Book 1

Status
Not open for further replies.

bobotski

Programmer
Oct 20, 2003
3
US
Hi, I am just starting out to code using Struts framework and i cant move any further because of this error. here's my xml files :

struts-config.xml

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>

<!DOCTYPE struts-config PUBLIC
&quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&quot;
&quot;
<!--
This is the Struts configuration file for the example application,
using the proposed new syntax.
-->


<struts-config>
<!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<!-- bookForm -->
<form-bean name=&quot;bookForm&quot; type=&quot;books.Book&quot;/>
</form-beans>

<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<!-- bookForm -->
<forward name=&quot;bookCreated&quot; path=&quot;/BookView.jsp&quot;/>
</global-forwards>


<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<!-- bookForm -->
<action path=&quot;/createBook&quot;
type=&quot;books.BookAction&quot;
name=&quot;bookForm&quot;
scope=&quot;request&quot;
input=&quot;/CreateBook.jsp&quot;>
</action>
</action-mappings>


<!-- ========== Controller Configuration ================================ -->
<controller>
<!-- The &quot;input&quot; parameter on &quot;action&quot; elements is the name of a
local or global &quot;forward&quot; rather than a module-relative path -->
<set-property property=&quot;inputForward&quot; value=&quot;true&quot;/>
</controller>


<!-- ========== Plug Ins Configuration ================================== -->
<plug-in className=&quot;org.apache.struts.plugins.ModuleConfigVerifier&quot;/>

<plug-in className=&quot;org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn&quot;>
<set-property property=&quot;pathname&quot; value=&quot;/WEB-INF/database.xml&quot;/>
</plug-in>

<plug-in className=&quot;org.apache.struts.validator.ValidatorPlugIn&quot;>
<set-property property=&quot;pathnames&quot;
value=&quot;/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml&quot;/>
</plug-in>
</struts-config>


web.xml
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>

<!DOCTYPE web-app
PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN&quot;
&quot;
<web-app>
<display-name>Struts Example Application</display-name>

<!-- 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>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</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>

<!-- Application Tag Library Descriptor -->
<taglib>
<taglib-uri>/WEB-INF/app.tld</taglib-uri>
<taglib-location>/WEB-INF/app.tld</taglib-location>
</taglib>

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


CreateBook.jsp

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

<html:html locale=&quot;true&quot;>
<head>
<html:base/>
<title><bean:message key=&quot;index.title&quot;/></title>
</head>
<body bgcolor=&quot;white&quot;>
<h2>Create a book</h2>

<html:errors/>
<html:form action=&quot;createBook.do&quot; method=&quot;GET&quot;>
Title:<html:text property=&quot;title&quot; /> <br/>
<html:submit property=&quot;submit&quot;/>
</html:form>


</body>
</html:html>

BookAction.java
package books;

import javax.servlet.http.*;
import org.apache.struts.action.*;
/*
The action for the creation of a book.
@author stephan@stephanwiesner.de
*/
public final class BookAction extends Action
{
/**
@param mapping The ActionMapping used to select this instance
@param form The optional ActionForm bean for this request (if any)
@param req The non-HTTP request we are processing
@param res The non-HTTP response we are creating
@return Return an ActionForward instance describing where and how
control should be forwarded, or null if the response has already
been completed.
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
try{//xxpmnomr

System.out.println(&quot;Start perform(&quot; + form + &quot;) . . .&quot; );
String title = req.getParameter(&quot;title&quot;);
Book book = new Book();
book.setTitle( title );
System.out.println(&quot;After creation of book: &quot; + book.getTitle() );
req.setAttribute(&quot;BOOK&quot;, book);
return mapping.findForward(&quot;bookCreated&quot;);

}catch(Exception e){
System.out.println(&quot;ERROR HERE&quot;);
return mapping.findForward(&quot;bookCreated&quot;);
}
}
}

Book.java
/*
* Created on Oct 17, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package books;

/**
* @author xxpmnomr
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.util.Vector;

public class Book {
/*
A simple book.
@author stephan@stephanwiesner.de
*/
/** The title */
private String title = &quot;&quot;;
/** We can have more than one author */
private Vector authors = new Vector();
/** The number of pages the book has */
private int pages = 0;
/** Standard constructor. */
public Book()
{ }
/** @param title The new Title */
public void setTitle(String title)
{ this.title = title; }
/** @return The title. */
public String getTitle()
{ return this.title; }
/** @param pages The new number of pages. */
public void setPages(int pages)
{ this.pages = pages; }
/** @return The number of pages. */
public int getPages()
{ return this.pages; }
/**
We don't want to work with the Vector here, as it is
only a reference we would get!
@param author Add another author
*/
public void addAuthor(String author)
{ this.authors.add(author); }
/**
Pay attention not to use the wrong number.
@param position The number of the author to remove.
*/
public void removeAuthor(int position)
{ this.authors.remove(position); }
/** @return The number of authors the book has. */
public int getNumberOfAuthors()
{ return this.authors.size(); }
}


Any help would be appreciated.

Thanks!
 
If you want to use class books.Book as a formBeam, it must extends ActionForm.
 
hi byam, i am getting the same error

Regards,
bobotski
 
Also Change:

<html:form action=&quot;createBook.do&quot; method=&quot;GET&quot;>

To:

<html:form action=&quot;/createBook&quot; method=&quot;GET&quot;>

By the way, what error are you getting?

 
byam,

The trick was to extend the Book class with FormAction. Just wanted to let people know. This is a 'bug' in a Struts Tutorial people are using.
 
Actually, there is a typo. Instead of extending the Book class with "FormAction", it should be "ActionForm".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top