|
6. Let us compile this java file. First let us set a
parameter in NetBeans - to set target directory where the java
files will be compiled (classes will be stored). Go to tools -
options compiler types- external compilation (the external
compiler, javac is typically the default compiler. Set the target
directory to StrutsNetBeansSample /WEB-INF/classes. Click on the
file (TestFormBean.java) in the explorer window and click build -
> compile from main menu. The o/p window should show a message
like 'Finished TestFormBean. Now we have a form bean we need an
Action class that will take this form bean - use the ipString
entered by user to pass to the next page.
7. Lets create the TestActionHandler.java under
src/com/openstack/struts/testapp.
Here is the class definition:
package com.openstack.struts.testapp;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.openstack.struts.testapp.*;
public final class TestActionHandler extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
TestFormBean tform =
(com.openstack.struts.testapp.TestFormBean) form;
String tmpStr = "Welcome " + tform.getIpString() + " !";
tform.setIpString(tmpStr);
request.setAttribute("TestFormBean", tform);
return (mapping.findForward("success"));
}
}
As you can see all we are doing is taking the ipString (where user
will enter his / her name) and then generating a welcome message.
Then we reset the ipString with this complete message and set the
bean back in the request object. Finally we forward the control to
the 'success' handler of this action. Look at the 'perform'
method. It returns 'ActionForward' that forwards the action to the
next page (or handler).
The part 'return (mapping.findForward("success"))' will
instruct struts to go to next step. Let"s look at how we
specify this 'next step' in struts:
Open the struts.config from the WEB-INF directory. Look at the
action mapping:
<action path="/TestAction"
type="com.openstack.struts.testapp.TestActionHandler"
name="TestFormBean"
scope="request"
validate="false">
<forward name="success" path="/TestResult.jsp" />
</action>
The TestAction (from the TestFormPage.jsp) maps to
TestActionHandler class under com.openstack.struts.testapp.
In our TestFormPage.JSP we have specified the submit action in
the form as "TestAction". So when the form submits STRUTS will
read this file to evaluate 'TestAction' and call the perform
method in:
com.openstack.struts.testapp.TestActionHandler Class.
Finally the mapping.findForward("success")) will forward the
request to TestResult.jsp, based on the following line in
struts.config <forward name="success"
path="/TestResult.jsp" /> So, in summary STRUTS will
do the following tasks: - Copy user entered values into a
form bean.
- Call the action handler passing the form
bean.
- Facilitate screen flow using the action mapping.
[Next]
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|