Struts Hello World Example in Eclipse

06.13.2012

·

FileSource:http://www.dzone.com/tutorials/java/struts/struts-tutorial/struts-tutorial-using-eclipse-1.html

In this tutorial you will learn howto create a Struts hello world application in eclipse. First create a newproject, go to File->New and select

DynamicWebProject.

Enter the project name and clickthe Finish button.

Add the following jar files to theWEB-INFlib directory.

Right click the src folder andselect New->Package.

Enter the package name ascom.vaannila.formand click Finish.

Now right click the newly createdpackage and select New->Class.

Enter the class name asHelloWorldFormand the superclass name asorg.apache.struts.action.ActionFormand click Finish.

In the HelloWorldForm class add thefollowing code.

01.packagecom.vaannila.form;

02.

03.importorg.apache.struts.action.ActionForm;

04.

05.publicclassHelloWorldFormextendsActionForm {

06.

07.privatestaticfinallongserialVersionUID = -473562596852452021L;

08.

09.privateString message;

10.

11.publicString getMessage() {

12.returnmessage;

13.}

14.

15.publicvoidsetMessage(String message) {

16.this.message = message;

17.}

18.}

In the same way create a newpackagecom.vaannila.actionand create aHelloWorldActionclass extendingorg.apache.struts.action.Action. Add the following code to theaction class and save it.

01.packagecom.vaannila.action;

02.

03.importjavax.servlet.http.HttpServletRequest;

04.importjavax.servlet.http.HttpServletResponse;

05.

06.importorg.apache.struts.action.Action;

07.importorg.apache.struts.action.ActionForm;

08.importorg.apache.struts.action.ActionForward;

09.importorg.apache.struts.action.ActionMapping;

10.

11.importcom.vaannila.form.HelloWorldForm;

12.

13.publicclassHelloWorldActionextendsAction {

14.

15.@Override

16.publicActionForward execute(ActionMapping mapping, ActionFormform, HttpServletRequest request, HttpServletResponse response)throwsException {

17.HelloWorldForm hwForm = (HelloWorldForm) form;

18.hwForm.setMessage("Hello World");

19.returnmapping.findForward("success");

20.}

21.}

Here we typecast the ActionForm toHelloWorldFormand set the message value.

Add the following entries in thestruts-config.xmlfile.

01.<?xmlversion="1.0"encoding="ISO-8859-1"?>

02.

03.<!DOCTYPE struts-config PUBLIC

04."-//Apache Software Foundation//DTD StrutsConfiguration 1.3//EN"

05."http://struts.apache.org/dtds/struts-config_1_3.dtd">

06.

07.<struts-config>

08.

09.<form-beans>

10.<form-beanname="helloWorldForm"type="com.vaannila.form.HelloWorldForm"/>

11.</form-beans>

12.

13.<global-forwards>

14.<forwardname="helloWorld"path="/helloWorld.do"/>

15.</global-forwards>

16.

17.<action-mappings>

18.<actionpath="/helloWorld"type="com.vaannila.action.HelloWorldAction"name="helloWorldForm">

19.<forwardname="success"path="/helloWorld.jsp"/>

20.</action>

21.</action-mappings>

22.

23.</struts-config>

Now configure the deploymentdescriptor. Add the following configuration information in theweb.xmlfile.

01.<?xmlversion="1.0"encoding="UTF-8"?>

02.<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">

03.<display-name>StrutsExample1</display-name>

04.

05.<servlet>

06.<servlet-name>action</servlet-name>

07.<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

08.<init-param>

09.<param-name>config</param-name>

10.<param-value>/WEB-INF/struts-config.xml</param-value>

11.</init-param>

12.<load-on-startup>2</load-on-startup>

13.</servlet>

14.

15.<servlet-mapping>

16.<servlet-name>action</servlet-name>

17.<url-pattern>*.do</url-pattern>

18.</servlet-mapping>

19.

20.<welcome-file-list>

21.<welcome-file>index.jsp</welcome-file>

22.</welcome-file-list>

23.</web-app>

When we run the application theindex.jsp page will be executed first. In theindex.jsppage we redirect the request to thehelloWorld.doURI, which inturn invokes theHelloWorldAction.

1.<%@ tagliburi="http://struts.apache.org/tags-logic" prefix="logic" %>

2.<logic:redirectforward="helloWorld"/>

In the action class we return theActionForward "success" which is mapped to thehelloWorld.jsppage. In thehelloWorld.jsppage we display the "HelloWorld" message.

01.<%@taglib uri="http://struts.apache.org/tags-bean"prefix="bean" %>

02.<html>

03.<head>

04.<metahttp-equiv="Content-Type"content="text/html;charset=ISO-8859-1">

05.<title>Hello World</title>

06.</head>

07.<body>

08.<bean:writename="helloWorldForm"property="message"/>

09.</body>

10.</html>

After creating all the files thedirectory structure of the application looks like this.

On executing the application the"Hello World" message gets displayed to the user.

You can download the source code ofthis example by clicking on the Download link below.

Source:DownloadWar:Download


推薦閱讀:
查看原文 >>
相关文章