Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new to struts - problem in small struts example

 
Bala Raju Mandala
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
----------------------------------------------
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code snippets look exactly fine except for one thing.



This should actually be
 
Bala Raju Mandala
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Anand,

Thank you for your reply. I modified the package name but still problem exists like that only. I am able to see only one output on console 'in set'.

It is not at all entering Action class. Please help, i am running out of thoughts
[ March 03, 2008: Message edited by: Bala Raju Mandala ]
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally made a new project and tested the code. It works all fine.
Probably you are missing something somewhere. Well, it's really difficult to comment.
But, please specify the steps that you took to make the project. You have got on some other boat somewhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic