X

Geertjan's Blog

  • November 11, 2005

Struts for the Complete Beginner (Part 1)

Geertjan Wielenga
Product Manager
NetBeans IDE 5.0 simplifies Struts. By selecting one single checkbox in the New Web Application wizard, all the Struts libraries are added to your project. In addition, you get the magical struts-config.xml file, which is where you register all the Struts functionality that you want to work with. Wizards are provided for the creation of the two categories of Struts classes—actionform beans and actions. Wizards are also accessible from within struts-config.xml, for the generation of the code used to register the Struts functionality.

Here's a really simple procedure for the creation of a Struts application in NetBeans IDE 5.0. You'll use the Struts framework to create a very simple semi-functioning login form:

All the processing will be done by Struts. (What 'processing' consists of is something you'll discover in the steps below.) You won't learn much conceptually, but through following the steps below you'll familiarize yourself with the Struts framework and the way in which support for this framework has been implemented in NetBeans IDE 5.0.

  1. Set Up the Project. Click Ctrl-Shift-N, in the Web category choose web application, click Next, and call it MyVideoStoreApplication. Click Next. Select Struts 1.2.7 and click Add Struts TLDs. Click Finish.

    Here's what you should see, with the most important file (the struts-config.xml file) selected (click to enlarge):


  2. Create the JSP Pages. Right-click the MyVideoStoreApplication project node, choose New > JSP, and call it loginForm. Click Finish. Create another JSP and call it loginSuccessful. In the Source Editor, change the content of the H1 tags in both new JSP files to something meaningful. For example, change the text JSP Page to Login Form and Login Successful!, respectively. Do the same for the text in the TITLE tags.

    Now copy the first two taglib directives from the welcomeStruts.jsp file to the top of your new loginForm.jsp. In loginForm.jsp, below the H1 tags, add the following:

    <html:form action="login">
    <html:submit value="Login" />
    </html:form>

    It's handy to know that everything between the <html:form> tags is handled by Struts. Notice that while typing, the IDE helps you by suggesting different ways of completing the code that you're typing. It also provides access to Struts Javadoc (click to enlarge):


    In the Component Palette on the right side of the Source Editor, expand the HTML section and drag the Table item to just above the <html:submit value="Login" /> line. The Insert Table dialog box pops up. Here, make sure that Rows and Columns are both set to 1, so that we'll create a table consisting of one row and one column. Click OK. In between the TH tags, type the following:

    <bean:message key="login.name" />

    In between the TD tags, type the following:

    <html:text property="name" />

    Add login.name as a key to the ApplicationResource.properties file and add a meaningful message. For example:

    login.name=Name

    The body of loginForm.jsp is now as follows:

  3. Create the Struts ActionForm Bean. Right-click the MyVideoStoreApplication project node and choose New > File/Folder. In the Web category choose Struts ActionForm Bean and click Next. Select com.myapp.struts in the Package drop-down list and press Finish. The actionform bean opens in the Source Editor. Open struts-config.xml in the Source Editor and note that it contains, among other things, the following:

    <form-beans>
    <form-bean name="NewStrutsActionForm" type="com.myapp.struts.NewStrutsActionForm"/>
    </form-beans>

    Now browse through the actionform bean in the Source Editor and look at the validate method. Notice that a field called name is validated by default. If validation fails, which happens when no name is entered in the JSP page, a message that is identified by error.name.required is returned. Add error.name.required as a key to the ApplicationResource.properties file and add a meaningful message. For example:

    error.name.required=Enter a name, banana brain!

    Now add the following in loginForm.jsp, right above the closing </html:form> tag:

    <html:errors />
  4. Create the Struts Action. Right-click the MyVideoStoreApplication project node and choose New > File/Folder. In the Web category choose Struts Action and click Next. Select com.myapp.struts in the Package drop-down list. Type login in the Action Path (the content of Action Path is therefore now /login. Click Next. Notice that the IDE suggests that you associate the action with the actionform bean created in the previous step. In Input Resource browse to your loginForm.jsp page. Click Finish.

    Notice that struts-config.xml contains, among other things, the following:

    <action-mappings>
    <action input="/loginForm.jsp"
    name="NewStrutsActionForm"
    path="/login"
    scope="session"
    type="com.myapp.struts.NewStrutsAction"/>
    <action path="/Welcome"
    forward="/welcomeStruts.jsp"/>
    </action-mappings>

    Browse through the action and look at the execute method. Notice that this action forwards to the output view called success. You need to define the loginSuccessful.jsp as your output view. Open struts-config.xml in the Source Editor, right-click anywhere, and choose Struts > Add Forward. Type success in Forward Name. Browse to loginSuccessful.jsp in Resource File. Click Action. Choose /login from the drop-down list. Click Add. Notice that struts-config.xml now shows the following (the new code is in bold):

    <action-mappings>
    <action input="/loginForm.jsp"
    name="NewStrutsActionForm"
    path="/login"
    scope="session"
    type="com.myapp.struts.NewStrutsAction"><forward name="success"
    path="/loginSuccessful.jsp"/>

    </action>
    <action path="/Welcome"
    forward="/welcomeStruts.jsp"/>
    </action-mappings>
  5. Run the Project. Right-click the MyVideoStoreApplication project node and choose Properties. In the Project Properties dialog box, click the Run node. In Relative URL, type login.do. Click OK.

    Now right-click the MyVideoStoreApplication project node and choose Run Project. The project deploys and displays the loginForm.jsp. You should see the following:

    When you enter a name (or anything else, so long as it's not nothing), loginSuccesful.jsp is displayed. One interesting thing to do at this point is to go back to the ApplicationResource.properties file. Change the first four keys to the following:

    errors.header=
    errors.prefix=<span style="color: red">
    errors.suffix=
    errors.footer=

    Now deploy the application again. If loginSuccesful.jsp opens, and you don't want it to, change the scope attribute of the /login path in struts-config.xml from session to request. Deploy again and notice that the error message has been reformatted:


In future blog entries (and probably in an official NetBeans tutorial), I'll expand on this scenario so that it provides more functionality and so that it becomes a more useful login form. (Update: Click here for Part 2!)

Join the discussion

Comments ( 25 )
  • anjan bacchu Friday, November 11, 2005
    hi there,


    when the entire world acknowledges that struts is stale and the way to move forward is shale/spring/webwork/tapestry, why is Netbeans encouraging people to go the struts way ?


    It would be nice for netbeans to acknowledge another apache project like Tapestry/Shale or similar licensed spring.

    my 2 cents.


    BR,

    ~A
  • Geertjan Friday, November 11, 2005
    NetBeans isn't 'encouraging' people to go 'the Struts way'. Why would it want to do that? JSF is the Sun technology, while Struts is not. However, there are many users out there who use Struts. For this reason, NetBeans provides support for this framework. Personally, I prefer Wicket (which is similar to Tapestry). I'm sure NetBeans will support other frameworks (especially Shale) in the future too. The more the merrier!
  • guest Wednesday, November 16, 2005
    test
  • guest Wednesday, November 16, 2005
    Shouldn't <code>errors.suffix</code> be set to </span>, like this:
    errors.header=
    errors.prefix=<span style="color: red">
    errors.suffix=</span>
    errors.footer=
  • Geertjan Thursday, November 17, 2005
    Thanks, 128.171.112.164. You're right.
  • Geertjan Thursday, November 17, 2005
    By the way, I asked the developer who implemented Struts support in NetBeans IDE 5.0 to review the steps above, because this is going to form the basis of an upcoming tutorial. Here are his comments:
    • Step 1—Why do you recommend to user to add Struts TLDs to the project? The trend is to keep them in the libraries and don't put them into the web application.
    • Step 2—You don't declare using tag libraries. At first you shuld write the declaration of tag libraries and then start to use the tags from libraries. The URI attribute in the taglib declaration is connected with what I wrote about step 1. The code completion offers all URI attributes of the tag libraries and, if you put the TLD in the WEB-INF, then the URIs of these URLS as well.
    • The ApplicationResource.properties file—user doesn't know where it is. So there should be written where the file is. Like, go to Source Packages and in the package <tt>com.myapp.struts</tt>, open the ApplicationResource.properties file.
    • Step 3—There is a sentence: "Now browse through the actionform bean in the Source Editor...". Maybe it should be changed to user knows that he can use hyperlink. So I suggest somethink like: "Hold key CTRL and place mouse pointer over the fully-qualified class name in type attribute. The value changes in the link which opens the action from bean java file."
    • Step 4—Maybe mention the hyperlink one more time. :) I suggest to change: "Browse through the action and look..." to: "Browse through (or use hyperlink) and look..."
    • It says—"Type success in Forward Name. Browse to loginSuccessful.jsp in Resource File. Click Action. Choose /login from the drop-down list. Click Add." BUT it is not clear which Action—there are two: one Forward To and one Location. So write here" "Change Location from Global to Action and choose /login." Or, the easier way how to do it is: Right-click inside the action definition with value of input attribute /loginForm.jsp and choose Struts > Add Forward , then the location will be set as Action and the action will be predefined.
  • guest Monday, April 24, 2006
    dfdfdf
  • guest Friday, September 15, 2006
    p[
  • Carl Smith Tuesday, September 26, 2006
    Hi GEERTJAN
    I am having a problem locating where to edit the JSP
    I am using NetBeans 5.0 with Tomcat. I found the JSP template in under web, but i dont know to attach it to my source. I have source & design code that i can view. Please help.
  • Geertjan Tuesday, September 26, 2006
    Sorry, don't understand your question. Write to me with the steps you've taken (geertjan.wielenga@sun.com) and I'll try to help.
  • Carl Smith Tuesday, September 26, 2006
    I have written source code with JFrame. I want to read from a database and display a list of clients
    into the JFrame list box.
    Hope i'm explaining myself better.
    Thanx Carl
  • Geertjan Tuesday, September 26, 2006
    JSP is in the web world. JFrame is not in the web world. It belongs to Swing. I don't understand what you're trying to do with a JSP and a JFrame in the same application.
  • Carl Smith Tuesday, September 26, 2006
    I read off a network server where the database resides. I extract a list of clients from the database & display then on a screen (JFrame).
    What is the best way for me to do this.
    Thanx for UR patience
    Carl
  • Geertjan Tuesday, September 26, 2006
    Join a general Java developer forum (http://forum.java.sun.com/index.jspa) and ask there. Your question doesn't apply to NetBeans specifically, but Java in general. Your initial question on JSP doesn't have anything to do with your JFrame question either.
  • Carl Smith Tuesday, September 26, 2006
    I am waiting for an email address. That is why I have not joined.
    But thanks anyway.
    Carl
  • sathish Friday, August 24, 2007

    sir,

    i want jsp coding for

    candidate login

    administrator login

    provider login

    these three in a single login page to access the different pages.


  • guest Thursday, May 8, 2008

    fgh


  • guest Thursday, January 15, 2009

    blah blah


  • Jay Parkaash Tuesday, February 10, 2009

    This is a stupid article.

    Ram Vijjay and Elephant headed God todl this to us a thousand years ago.

    You copied these contents from an Indian holy book. Thats how struts was conceived.

    Ram Verma and Ram Jaanti Nusht have clearly told that thats how Struts would look like


  • guest Friday, July 17, 2009

    e


  • guest Friday, July 17, 2009

    wwwwwwwwwwwwwwwwwwwwwwwww


  • Igor G Sunday, August 9, 2009

    For some reason when I run this app all I'm seeing is the "Login Form" header on my JSP page. The input box and submit button don't show up on the JSP page, even though I have coded them the same way as in this article.


  • Igor G Sunday, August 9, 2009

    Never mind. I forgot to copy the tags-bean and tags-html directives from welcomeStruts.jsp to loginForm.jsp. It all works now!


  • krishna Friday, December 11, 2009

    i am getting the login successful page. i get the following url:

    http://localhost:8084/MyVideoStoreApplication/login.do;jsessionid=ED735BFD19BC4A42C8EE9FF993164794

    which is blank. This is my struts-config.xml file

    <action-mappings>

    <action input="/loginForm.jsp"

    name="NewStrutsActionForm"

    path="/login"

    scope="session"

    type="com.myapp.struts.NewStrutsAction">

    <forward name="Success"

    path="/loginSuccessful.jsp"/>


  • guest Saturday, March 18, 2017

    get this error

    Missing message for key "login.name" in bundle "(default bundle)" for locale en_GB

    http error 500

    ... please help


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.