Eclipse 3.4.x + Struts2 + Tomcat 6.x

 

Eclipse 3.4.x + Struts2 + Tomcat 6.x


There are many Struts2 tutorials on the web for the beginners, but it was really difficult for me to find an easy to understand tutorial to help me configure my local environment and start a simple struts project. In this post I show how to configure your local environment to use struts2 with eclipse IDE version 3.4.x.

Downloading struts2 files:
First you would need to download struts2 jar files from here. I’m personally using: struts-2.1.6-all.zip

Configuring Eclipse:
Start Eclipse and create a new project: File -> new -> Project. In the project type selection window choose: Dynamic Web Project I called my project “HelloWorld”.
Next you would need to copy the following .jar files to WEB-INF/lib folder in your project.
Please note that commons-fileupload.jar is not part of struts2 package. The reason that I have it is because not having this file causes apache to give me an error: “Unable to load configuration… struts2-core-2.1.6.jar!/struts-default.xml:46:178” To download this .jar file you would need to go to this website.

  • commons-logging-*.jar
  • freemarker-*.jar
  • ognl-*.jar
  • struts2-core-*.jar
  • xwork-*.jar
  • commons-fileupload.jar

The * is the version of the jar file. After copying these files to the WEB-INF/lib refresh the project in your IDE. You should now be able to see the .jar files.

Modifying the web.xml file:
Now you would need to modify your web.xml file. Replace the content of your web.xml file with the following:

01<?xml version="1.0" encoding="UTF-8"?>
03    <display-name>Struts 2 : Hello World</display-name>
04    <filter>
05        <filter-name>struts2</filter-name>
06        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
07    </filter>
08    <filter-mapping>
09        <filter-name>struts2</filter-name>
10        <url-pattern>/*</url-pattern>
11    </filter-mapping>
12</web-app>

Creating HelloWorld.java:
Create a normal Java class inside tutorial package containing the following code. This is the class responsible for handling the struts actions:

01package tutorial;
02import com.opensymphony.xwork2.ActionSupport;
03  
04public class HelloWorld extends ActionSupport {
05  
06    private static final long serialVersionUID = 1L;
07    private String message;
08    public String execute()
09    {
10        setMessage("Hi there! This is a warm hello from Struts 2");
11        return SUCCESS;
12    }
13    public String getMessage() {
14        return message;
15    }
16    public void setMessage(String message) {
17        this.message = message;
18    }
19}

Creating HelloWorld.jsp:
Create a .jsp file inside the WebContent folder called HelloWorld.jsp containing the following code:

01<%@ page contentType="text/html; charset=UTF-8" %>
02<%@ taglib prefix="s" uri="/struts-tags" %>
03<html>
04<head>
05    <title>Struts 2 - Hello World tutorial</title>
06</head>
07  
08<body>
09<h2><s:property value="message"/></h2>
10If you can see above message, Congrats! You have successfully created your first Struts 2 application.
11</body>
12</html>

Creating struts.xml file:
This is the most important file in the struts application. This is the file used to configure struts actions. Struts2 requires this file to be available in the classes folder, but if you create it there it will be removed every time you clean your project. So the best place for this file is to place it inside your “src” or source folder. Here is the content of the struts.xml file:

01<?xml version="1.0" encoding="UTF-8" ?>
02<!DOCTYPE struts PUBLIC
03       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
05<struts>
06    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
07    <constant name="struts.devMode" value="false" />
08    <package name="tutorial" namespace="/" extends="struts-default">
09        <action name="HelloWorld" class="tutorial.HelloWorld">
10            <result>/HelloWorld.jsp</result>
11        </action>
12    </package>
13</struts>

After Adding these files your project structure should look something like this:

Changing the Java Build path:
Now you would need to change your build path. Change your buildpath to: “HelloWorld/WebContent/WEB-INF/classes

Please note: Based on a comment by Raza Gill You MUST not modify the build path for Eclipse 3.5 Galileo.

Installing the server:
I’m using tomcat 6.X. To download you need to go to this website and download tomcats core. To add the server go to: File -> New -> other and choose “server”. In the next window choose Tomcat v6.0 server and click next.
Assuming that you have unzipped the tomcat server you have just downloaded. you would need to browse to your tomcat folder and click next. Now you would need to add “HelloWorld” project to the server and click finish.

Testing the project:
It’s done! Now you would need to run your project on the added server and go to the following URL: http://localhost:8080/HelloWorld/HelloWorld.action you should see the following page:

23 Comments

  1. You MUST not modify the build path for Eclipse 3.5 Galileo. I spent hours working out this example only to find out that the my mistake was in setting the path to the one specified in the tutorial. However, this is only true for Eclipse 3.5.

  2. Thanks for the comment Raza, I have added a note in the post.

  3. Thnks..

    Very good Example.

    I have wasted my 1.5 day before reading this one .
    But now i able to run this successfully.

  4. i dint succeed in executing ur eg.
    in web.xml the welcome file list is not there and how the action takes place? being a starter i couln’t follow this simple eg

  5. Hi Vinoth,
    What you need to know is that inside the web.xml file we are Sending all actions “/*” to struts2. Inside the Struts.xml file we define the action and the class that handles the action, and the JSP file that its forwarded to in case of “SUCCESS”. If you have these component setup as it is in the example you shouldn’t have any problems. Make sure that you are using the same file structure…

  6. hey raza can u give me the path for the version galileo 3.5

  7. Hi Im using struts2 to develop a tool.I need to place all the logs in a file…
    The log4j.poperties code i used is..

    log4j.rootLogger=DEBUG, A3

    ## This appender writes to the layway1.log file.
    log4j.appender.A3=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.A3.File=./layway2.log
    log4j.appender.A3.ImmediateFlush=true
    log4j.appender.A3.layout=org.apache.log4j.PatternLayout
    log4j.appender.A3.layout.ConversionPattern=%d{MM/dd/yy HH:mm:ss,SSS}[ Thread :%t] [%C{1}:%L] %-5p – %m%n
    log4j.category.org.springframework=WARN

    Other than the logs even the spring framework logs are also getting displayed which i dont want…Can someone help me in logging only the logs i added in my files..

  8. hey i am getting this error can you pls help me

    HTTP Status 404 – /StrutsHelloWorld/HelloWorld.jsp

    ——————————————————————————–

    type Status report

    message /StrutsHelloWorld/HelloWorld.jsp

    description The requested resource (/StrutsHelloWorld/HelloWorld.jsp) is not available.

  9. Thanks for the tutorial. After wasting my morning with a different tutorial, I found yours! Now I’m up and running.

  10. My friend referred me to your blog, so I thought I’d come have a read. Very interesting material, will be back for more!

  11. Really very useful article..
    Whether struts2 cant work in eclipse 3.3??
    Any one reply for me..

  12. Hi when i click submit button in the form im got the errror that action class not found and struts properties missing
    what is that????

  13. Hi Divya,
    You are having a configuration problem. The problem you are having is caused because of an error in struts configuration file (XML). It basically cant find a class to map the action to….

  14. Hu, luckily I found this post after my long research. You made my day. ;)

  15. You would be crazy to not use more Twitter marketing

  16. hey guys,

    Got an error on the browser :

    Cannot establish a connection to the server at localhost:8080

    Pls help…….

  17. hey,just found your Post when i google something and wonder what hosting do you use for your wordpress,the speed is more faster than my blog, i really want to know it.will back to check it out,thank you!

  18. Hi, it’s a bit offtopic but may I ask you where did you get this blog template? I’m going to start bloggin as well, I’m a bit noob though but I really like it ;) Let me know… Anyway, nice website! ;)

  19. I would like to say “wow” what a inspiring post. This is really great. Keep doing what you’re doing!!

  20. hello your post is really informative! I just bookkmarked this site :)

  21. Hai friend , Why dont u put facebook badge on your blog? Because I want to add your fan page. Thank Regards Admin

  22. Hi

    it was a very good example to create an application in Struts2.
    My directory structure is same as above but i am facing an error

    HTTP Status 404 – There is no Action mapped for namespace / and action name .

    i m not able to resolve this error..Can u please help me???

    Thanks

  23. Hi Kavita,
    You would need to check your struts.xml file to see if you have HelloWorld action defined there if you do have it then you would need to check to see if you have the class handing the action defined in the package: tutorial

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值