A practical JBUILDER8 STRUTS, a simple example
Liu Xiaowei: Liuxiaowei2000@sina.com
I. Introduction
This article describes a simple Struts application step using JBuilder 8, mainly based on step, theoretical instructions. I hope to help everyone, I hope everyone can criticize!
Operating environment:
Windows 2000 Server (SP3)
J2SDK1.4.1_01
JAKARTA-STRUTS-1.0.2 (JBuilder8 comes with)
Second. Actual combat
1 Create a Web Application
1.1 Select File- "New Project to establish a new project called StrutsDemo, as shown below:
figure 1
1.2 Click Next, remove the items in Required Libraries, others constant, click Finish.
figure 2
1.3 Select Project- "Project Properties, set the Server page according to the image below, click OK:
image 3
1.4 Select File- "New-" Web page - "Web Application, click OK.
Figure 4
1.5 After filling out the picture below, click OK.
Figure 5
2 establish an actionform
2.1 Select File- "New-" Web Page - "ActionForm, click OK.
Image 6
2.2 After filling in the picture below, click OK.
Figure 7
2.3 Click the Add button, add the definition of the Name and Password field, click Next.
Figure 8
2.4 After filling out the picture below, click Finish.
Figure 9
2.5 Write an ActionForm code.
Simple validity checking in ActionForm, fill in the loginActionform's validate () function:
Public ActionerRors Validate (ActionMapping ActionMapping, HttpservletRequest HttpservletRequest) {
ActionerroS Errors = new actionerrors ();
IF ((Name == null) || (Name.length () <1))
Errors.Add ("Name", New ActionError ("Error.userName.Required)));
IF ((Password == null) || (Password.Length () <1))
Errors.Add ("Password", New ActionError ("Error.Password.Required));
Return Errors;
}
3 establish an action
3.1 Select File- "New-" Web page - "Action, click OK.
Figure 10
3.2 After filling out the picture below, click Next.
Figure 11
3.3 After filling out the picture below, click Finish. Figure 12
3.4 Write an action code.
In general, write logic in Action, access the database, etc. Fill in the Loginaction's Perform () function as follows:
Public ActionForward Perform (ActionMapping ActionMApping, ActionMApping ActionMApping, Actionform ActionForm, HttpServletRequest HttpservletRequest, httpservletResponse httpservletResponse) {
LoginActionform form = (loginactionform) Actionform;
IF (Form.GetName (). Equals ("Liuxiaowei" && (Form.GetPassword (). Equals ("Liuxiaowei"))))))))
{
// Save Our Logged-in User In the session
HttpSession session = httpservletRequest.getations ();
Session.setttribute ("Username", Form.GetName ());
Return (ActionMapping.FindForward ("Success");
}
ActionerroS Errors = new actionerrors ();
IF (! form.getname (). Equals ("liuxiaowei")))
Errors.Add (ActionerRors.global_ERROR, New Actionerror ("Error.userName.wrong"));
IF (! form.getpassword (). Equals ("liuxiaowei"))
Errors.Add (ActionerRors.global_ERROR, New Actionerror ("Error.Password.wrong");
SaveerRors (httpservletRequest, Errors);
Return (ActionMapping.FindForward ("login"));
}
4 establish JSP
4.1 Select File- "New-" Web Page - "JSP from ActionForm, click OK.
Figure 13
4.2 After filling in the picture below, click Next.
Figure 14
4.3 After filling out the picture below, click Next.
Figure 15
4.4 After filling out the picture below, click Finish.
Figure 16
4.5 Modify the Login.jsp file.
After the modification is complete, you are as follows (red is a modification section):
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> <% @ Page ContentType = "text / html; charset = GBK"%>
title>
hEAD>
html: form>
body>
html: html>
4.6 Select File- "New-" Web Page - "JavaServer Page, click OK.
Figure 17
4.7 After filling out the picture below, click Next.
Figure 18
4.8 After filling out the picture below, click Finish.
Figure 19
4.9 Modify the Loginok.jsp file.
After the modification is complete, you are as follows (red is a modification section):
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>
title>
head>
<% = Request.getSession (). GetaTtribute ("UserName")%>
h1>
body>
html>
4.10 Modify Struts-Config.xml files
Double-click the struts-config.xml file on the left side of the file tree, select the Global Forwards item below, click the Add button on the right, add two items, as shown below:
Figure 20
4.11 Modify Web.xml file
Double-click the web.xml file on the file tree on the left side, select the servlets-> Action item below, click the Add button on the right, add an application, as shown below:
Figure 21
Figure 22
4.12 Write a resource file
Select File- "Save all to save all files.
Select Project- "Make Project" strutsdemo.jpx "compile project.
Write a text file called ApplicationResources.properties, save the SRC / STRUTSDEMO in the project storage directory and add it to the project, right-click this file, select Properties, and press the figure.
Figure 23
The document content is as follows:
Login.title = Struts demo
Login.Heading =
LGOIN.MESSAGE = Welcome to the strutsdemo!
Login.inputPassword = please input password:
Login.inputName = please input username:
Loginok.title = User login successfull
Loginok.Message =
Error.Username.Required =
Error.Password.required =
Error.username.wrong =
Error.Password.wrong =
Errors.footer = ul>
Errors.Header =
1. Right-click on the login.jsp in the left file tree, select Webrun Useing Defaults.
2. If you do not enter Name and Password, click Submit, you will see an error message, which is generated by ActionForm.
3. If you enter the wrong Name and Password, click Submit, you will also see the error prompt, which is generated by action, correct Name and Password in this example are "liuxiaowei", the correct words will look To the Login Successful page, Loginok.jsp, the username is passed through Session.