Software By JeffMain Page | About | Help | FAQ | Special pages | Log in
The Free Encyclopedia
Printable version | Disclaimers

Our Struts Web Pages

From Software By Jeff

Starting with Struts - Setting Up Struts - Struts Webserver Configuration - Our Struts Classes - Our Struts Web Pages - Struts Server Deployment - Making Struts Projects in IDEs


Even before we completed the Struts classes, we'd know we'd need these JSPs. Whether we develop these before, during, or after we develop the classes depends on our style, the design of the site, and potentially the complexity of the bean. Since our bean is so simple, we've done that first.

Table of contents

default.jsp

Our default.jsp will give us an example of the page instantiating the bean for us. In our example so far we've got a two-page site, this page will tell us to check a box and hit a button. Little more is necessary to exercise our Action and ActionForm.

Additionally, we'll do this with Struts tags, to show the link between the bean and the JSP form. An alternative to using Struts tags is to write all kinds of JavaScript, but if we were going to do that, we wouldn't be using Struts anyway.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head><title>Struts Sample Default</title></head>
<body>
<html:form action="/example.do">
<html:checkbox property="checked" />Check this box
<br><html:submit> Click this button
</html:form>
</body>
</html>

That should do it. What does it do?

The first line defines a tag library (http://struts.apache.org/userGuide/struts-html.html) to use in the preparation of the JSP. The Struts tag libraries give us the html library that we're using here. The bulk of the page is straight HTML. We'll focus only on the html tags.

html:form

The html:form tag defines the bounds of an HTML form. The action attribute must correlate to an action defined in our struts-config.xml file. Although we don't need the bean to render the page, the bean used will be the one associated with our action element in the struts-config.xml file, so we don't really need to do anything else to define the bean we'll be using. Had we associated with an action that had no bean defined, we would have to use one of the other bean methods provided by Struts; since our action defines the ExampleForm bean, that is the one that will be used. In the page, if we needed to reference the bean directly, we'd use the name attribute from the struts-config.xml for the action (in this case exampleForm).

html:checkbox

The html:checkbox provides the HTML form checkbox that we'll use to alter our bean's property. The property attribute is the name of the property in the bean with which this control is associated. Usually the properties are String objects, or in some cases arrays of String objects. Collections can be used, but they would typically contain String objects. String objects are just easy to move back and forth. Other types can be used, but be prepared for a little more work in that case.

html:submit

The html:submit provides the HTML form submit button that will engage the Struts action. It sends the form POST request (the default, which will be used since we didn't state otherwise), with the information contained in the form. In this case, our intialized bean will be sent with the single value of checkbox set according to the state of the form's checkbox.

Rendering

When the page is initially rendered, the value of the named property, in our case checked will be used to determine if the box is on. Since we didn't set a value to be used, the default of "on" will be used. If our bean's property contains exactly "on" then the checkbox would be rendered initially checked.

example.jsp

The second JSP page in our example is simply named example.jsp. We'll make it very simple, like the default.jsp, showing only the one check box.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head><title>Struts Sample Example</title></head>
<body>
<html:form action="/example.do">
<html:checkbox property="checked" />This better be checked on entry
<br><html:submit> Click this button
</html:form>
</body>
</html>

Very similar; really just a little static text has changed.

You'll note the action of the html:form is the same as the other. For this example it is because this is the only page in our application that has a bean and action associated with it. What this allows is for the user to leave the box checked or remove the check, and upon hitting submit they'll return to either this page or default.jsp.

Since the action defined by /example specifies an ActionForm, the page's Action is entered with the bean as populated by the calling page. The bean is possibly affected by the Action, although not in our case, and the result that ends up here is used to generate the page. Since our Action sends us to this page only if the bean value is set to "on," the property checked will be set to "on," and the checkbox should be initially rendered as checked; of course you can un-check the box as you use the page.

Retrieved from "http://swbyjeff.com/index.php/Our_Struts_Web_Pages"

This page has been accessed 22733 times. This page was last modified 19:53, 13 Jan 2006.


Find
Browse
Main Page
Community portal
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Create an account or log in
Special pages
New pages
Image list
Statistics
Bug reports
More...