Hi All,
I am new to
struts, i am practicing using some examples. I was struck with a program. Please help me where i am wrong. I am pasting all the files here.
On
Tomcat console i am able to see only one statement from the ActionForm. Later the control is not going to Action class. Can any body help me to solving this?
----------------------------------------------
index.html
==========
<html>
<body>
<h1>
<form action='welcome.do' method='post'>
Enter your name:
<input type="text"
name="myname" value="">
<input type="submit"
name="submit" value="Proceed">
</form>
</h1>
</body>
</html>
----------------------------------------------
struts-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-beans>
<form-bean name="welcomeForm" type="deccan.WelcomeForm"/>
</form-beans>
<action-mappings>
<action
path="/welcome"
type="deccan.WelcomeAction"
name="welcomeForm"
scope="request"
input="/index.html">
<forward name="success" path="/welcome.jsp" />
<forward name="failure" path="/index.html" />
</action>
</action-mappings>
</struts-config>
----------------------------------------------
web.xml
=======
<!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>
<
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>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
----------------------------------------------
WelcomeAction.java
==================
package deccan;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class WelcomeAction extends Action {
public ActionForward execute(
ActionMapping mapping ,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws Exception {
ActionForward forward = null;
System.out.println("WelcomeAction1");
WelcomeForm welcomeForm = (WelcomeForm)form;
System.out.println("in WelcomeAction");
String v = welcomeForm.getMyname();
System.out.println("in Action name " + v);
if( v == null || v.trim().length() < 1 ) {
return mapping.findForward("failure");
} else {
request.setAttribute("myname", "You are " + v );
return mapping.findForward("success");
}
}
}
----------------------------------------------
WelcomeForm.java
================
package deccan;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
public class WelcomeForm extends ActionForm {
private String myname;
public String getMyname() {
System.out.println("in get " + myname);
return myname;
}
public void setMyname(String value) {
System.out.println("in set " + value);
myname = value;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
return errors;
}
}
----------------------------------------------
welcome.jsp
===========
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page import="code.WelcomeForm"%>
<html>
<body>
<h1>
You entered :
<bean:write name="welcomeForm" property="myname" scope="request"/>
</h1>
</body>
</html>
----------------------------------------------