Saturday, June 9, 2012

Hello World with Struts 2 in Netbeans 7.1.2 and Tomcat 6

So I think I went over how to get tomcat up and running with Netbeans, so lets get started with Struts 2. First thing, go ahead and download the struts 2 framework here and go ahead and unzip those Jar files from the Struts 2 package somewhere they will be safe.

Then you will want to create a new project (a web project). After you get your project created go to properties->Add Library->Create Now add all of these Jar files into a new Library called Struts 2 (or whatever, who cares).

I started using Struts 2 in Action to get this running so I'm using their Hello World app. Inside of the source packages you need to make a file called struts.xml. This will be the main config for your struts project.

code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name="struts.devMode" value="true" />

  <package name="default" namespace="/" extends="struts-default"> 
   <action name="Menu">
    <result>/menu/Menu.jsp</result>
   </action>
  </package>  
</struts>

Now that that is taken care of you need to add a Filter and Filter-Mapping tag set to your web.xml file.
Code: (goes inside of the <web-app> tags!!!)
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
    
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>



It is fine to leave your welcome file as index.jsp. Now we are going to link to the action that we mapped earlier so inside of index.jsp add this:
        <P>
            <a href="Menu.action">menu</a>
        </p>
What this will do is simply trigger the Menu action that we defined in struts.xml. Remember that we defined the filter-mapping url pattern to pick up EVERYTHING ('/*'), so anything at all ending with .action will be intercepted.

So great, but we still need to make a Menu.jsp file for the action to point to. So the way I did this is inside of my 'Web Pages' area I made a folder called 'menu' and inside of it I created the .jsp file Menu.jsp. In Netbeans remember in terms of directories what you see is NOT what you get! The actual directory structure is like this:



So anyway, here is my Menu.jsp file:



<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts 2 in Action: Menu</title>
</head>

<body>

  <hr> 
  <ul>
     <li><a href="<s:url action='hello/Name'/>">HelloWorld</a></li>

 </ul>

  <hr>  

</html>



I will add the rest later, with the HelloWorld.java class, and the Name action. But for now I'm just going put what I have up on my server in a zip file so that you can download it and get up and running super fast with Struts 2. One note: the file is sort-of because I included the Hibernate libraries in there when I built my project, but that is OK, because I will use them soon and post those builds also!

Here is something you can deploy to Tomcat.

Here is the entire NetBeans project.

Edit:
So I opened the project on my laptop and there were three references that were broken, but these are easy fixes:
1)- Select a new server. I actually used Glassfish 3 on my laptop and it was fine.
2)- Point to struts 2. Go download struts 2:  http://struts.apache.org/download.cgi#struts234
Then add a custom libraray, point to the place where you extracted Struts 2 and you are good.
3)- Point to the MySQL Connector- download from here: http://dev.mysql.com/usingmysql/java/
Then fix the ref by pointing to the actual JAR file.


No comments:

Post a Comment