New to Struts
[nobr]Hello,
I am brand spanking new to struts.
I am trying to build a very simple application with struts. In fact I am trying to replicate one available here http://javaboutique.internet.com/tutorials/Struts/
This is mystruts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ================= -->
<form-beans>
<form-beanname="submitForm"
type="hansen.playground.SubmitForm"/>
</form-beans>
<!-- ========== Action Mapping Definitions ============ -->
<action-mappings>
<actionpath="/submit"
type="hansen.playground.SubmitAction"
name="submitForm"
input="/submit.jsp"
scope="request">
<forward name="success" path="/submit.jsp"/>
<forward name="failure" path="/submit.jsp"/>
</action>
</action-mappings>
</struts-config>
In web-inf directory I have 2 classes
WEB-INF\classes\hansen\playground\SubmitAction.java
and
WEB-INF\classes\hansen\playground\SubmitForm.java
In my application root (for luck of the better term) directory
I have submit.jsp file and this is the code
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head><title>Submit example</title></head>
<body>
<h3>Example Submit Page</h3>
<html:errors/>
<html:form action="submit.do">
Last Name: <html:text property="lastName"/><br>
Address:<html:textarea property="address"/><br>
Sex:<html:radio property="sex" value="M"/>Male
<html:radio property="sex" value="F"/>Female<br>
Married:<html:checkbox property="married"/><br>
Age:<html:select property="age">
<html:option value="a">0-19</html:option>
<html:option value="b">20-49</html:option>
<html:option value="c">50-</html:option>
</html:select><br>
<html:submit/>
</html:form>
</body>
</html>
When I tried to run the app I got exception
org.apache.jasper.JasperException: Exception creating bean of class hansen.playground.SubmitForm: {1}
Does it mean that I set something wrong in my struts-config.xml or does it mean that there is something wrong with the file ?
Thanks[/nobr]
[3825 byte] By [
difrad76a] at [2007-10-2 15:21:50]

have you compiled the java classes into .class files?Ie you needWEB-INF\classes\hansen\playground\SubmitAction.classWEB-INF\classes\hansen\playground\SubmitForm.class
If code is Ok, shouldn't it automatically compile all java files to .class files ?
may i know which tool r u using to create the application.?The files which which u have put in ur classes folder are not class files they are java source file.U have to put ur compiled classes there in the classes folder.
I use Eclipse. So in this case I'd have to add servlet API to classpath. Am I right ?
Ya in eclipse u have to add the servlet api in class path,
but at the same time also u have to specify the out put folder for classes.
Give the path of the classes folder there.
After doing this make sure the project gets compiled and the .class files are present in ur classes folder..
I think this will help u out.
I say u one easy step of doing this sample . Since u are new to Struts, i hope u are using Struts 1.0. but try struts 1.1 or the lates version , where there is no need of creating a bean class, u can just put an entry in the Struts config .xml file and just create the instance of the
formbean , I mean to say that struts1.1 or latest version uses DynaactionFrom class so it will be easy to create the bean class
Try the above example by putting this entry in the Struts config.xml file
<form-beans>
<form-bean name="sampleForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="informServer"
type="java.lang.String" initial=""/>
</form-bean>
</form-beans>
the entry in the action mappings arer the same . this may help u in learning quickly
The reason for this is that the fields you are creating in your jsp, you have to set proper getters and setters in your ActionForm class.If you given even single wrong value there then it will give you the error of BEAN , something like you are getting.
Can you paste your ActionForm java file so that we can check that what mistake you have made.
I have 2 java files
package hansen.playground;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public final class SubmitAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
SubmitForm f = (SubmitForm) form; // get the form bean
// and take the last name value
String lastName = f.getLastName();
// Translate the name to upper case
//and save it in the request object
request.setAttribute("lastName", lastName.toUpperCase());
// Forward control to the specified success target
return (mapping.findForward("success"));
}
}
and another
package hansen.playground;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class SubmitForm extends ActionForm {
/* Last Name */
private String lastName = "Hansen"; // default value
public String getLastName() {
return (this.lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* Address */
private String address = null;
public String getAddress() {
return (this.address);
}
public void setAddress(String address) {
this.address = address;
}
/* Sex */
private String sex = null;
public String getSex() {
return (this.sex);
}
public void setSex(String sex) {
this.sex = sex;
}
/* Married status */
private String married = null;
public String getMarried() {
return (this.married);
}
public void setMarried(String married) {
this.married = married;
}
/* Age */
private String age = null;
public String getAge() {
return (this.age);
}
public void setAge(String age) {
this.age = age;
}
}
i went through the same problem. you need to compile SubmitForm.java first but you need to put struts.jar on the classpath to do this (i unzipped it and added that to the classpath). then you need to edit SubmitAction.java to import SubmitForm.java. i just put everything above 'hansen' on the classpath then added 'import hansen.playground.*'. then you'll be able to compile SubmitAction.java
hope this helps.
Hi, when i compiled SubmitForm it compiled successfully but when i complied SubmitAction i get error saying
Cannot resolve symbol
Symbol: Class SubmitForm
location:Class hansen.playground.SubmitAction
SubmitForm f = (SubmitForm) form
^^
can anyone guide me where is problem.
This is my SubmitForm class
package hansen.playground;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class SubmitForm extends ActionForm {
/* Last Name */
private String lastName = "Hansen"; // default value
public String getLastName() {
return (this.lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* Address */
private String address = null;
public String getAddress() {
return (this.address);
}
public void setAddress(String address) {
this.address = address;
}
/* Sex */
private String sex = null;
public String getSex() {
return (this.sex);
}
public void setSex(String sex) {
this.sex = sex;
}
/* Married status */
private String married = null;
public String getMarried() {
return (this.married);
}
public void setMarried(String married) {
this.married = married;
}
/* Age */
private String age = null;
public String getAge() {
return (this.age);
}
public void setAge(String age) {
this.age = age;
}
}
This is my SubmitAction Class
package hansen.playground;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public final class SubmitAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
SubmitForm f = (SubmitForm) form; // get the form bean
// and take the last name value
String lastName = f.getLastName();
// Translate the name to upper case
//and save it in the request object
request.setAttribute("lastName", lastName.toUpperCase());
// Forward control to the specified success target
return (mapping.findForward("success"));
}
}
Thanks
> Hi, when i compiled SubmitForm it compiled> successfully but when i complied SubmitAction i get> error sayingCheck the message directly above you it states you have to make slight edits to make it work