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.
Here's what you should see, with the most important file (the struts-config.xml file) selected (click to enlarge):
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" />
login.name=Name
The body of loginForm.jsp is now as follows:
<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 />
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>
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!)
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
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.
into the JFrame list box.
Hope i'm explaining myself better.
Thanx Carl
What is the best way for me to do this.
Thanx for UR patience
Carl
But thanks anyway.
Carl
sir,
i want jsp coding for
candidate login
administrator login
provider login
these three in a single login page to access the different pages.
fgh
blah blah
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
e
wwwwwwwwwwwwwwwwwwwwwwwww
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.
Never mind. I forgot to copy the tags-bean and tags-html directives from welcomeStruts.jsp to loginForm.jsp. It all works now!
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"/>
get this error
Missing message for key "login.name" in bundle "(default bundle)" for locale en_GB
http error 500
... please help