Title: Struts Practice
[comment]
Author:
Liu Guodong
(dev2dev id: yahoo163)
introduction
I recently used Struts, and now I will write down my personal feelings, I hope to help you. It is used to translation in the writing format of foreigners in the translation document. This is also available for use, which looks like it is clearly J. I am not intended to introduce the advantages of Struts and what is MVC mode. There will be many articles on the Internet. I only specify an example. Combine this example to put Struts's primary stuff some stuff (Advanced, I didn't understand it, I didn't look at it. Ok, nonsense doesn't talk about it. Otherwise, someone wants to throw bricks.
Operating environment
I can't help but say the steps to say two environmental buildings. It is really what I think this is very important. You have only tolerate the first.
1. Required software
In order to run this example, you need the following software:
WebLogic8.1sp2 or any other web server, I use WebLogic8.1sp2. You can download from the BEA's website: http://commerce.be.com/index.jsp struts 1.0, you can be fooled from www.apache.org. A easy-to-use Java IDE, I use JBuilder, of course, this is not necessary. Another is a tool to write JSP, and the general web editing tool can be.
2. Environmental construction
Install struts
Several deployment description files and tag library description files Copy to your defaultApp / web-inferid directory from the Struts compression package.
These files include:
Struts-config.xml: The file used to configure your serveet and mapping Java Bean.
Struts-html.tld: This is a label library description file that he implements several labels related to the request response in HTML.
Struts-logic.tld: This is also a label library description file, which implements the functionality of logical operations in Java, such as loop, judgment, and more.
Struts-template.tld: Template tag library description file
Struts-bean.tld: Some tag libraries required for Java bean mapping.
In the DEFAULTAPP / WEB-INF / LIB directory copy of Struts.jar. It's okay, and then configure it. Web.xml is OK.
Configure web.xml files
You can directly put this file copy to the defaultApp / web-inflicment from the Struts compression package, if you want to figure out what they mean, it is not difficult, you will know if you have a little bit a little bit of JSP tag library. He is just to load the label library you want to use into your web app. In order to save space, I will no longer post him.
Start
This example we have to say is a simple login verification page, which is to enter the username and password. Click "Submit" to print out the username and password at the console. Since Struts is MVC structure, we may wish to start our example in the order of this structure.
Struts Model
The so-called model I understand that a container for storage information may be a bit shallow, but beginners may wish to understand this, so it is easy. To put it bluntly, the information submitted by the user from the JSP page is saved to a Java Bean. Of course, this Java Bean is not a normal Java Bean because it inherits from Struts Actionform, so that the Control class can recognize it and control it. From the literal meaning of the Actionform, we are not difficult to see, it is like the Form tag in our HTML, an actionform can represent the content in a form, such as this class Loginform in our example:
Package mytest.struct; // Import must be packaged
Import org.apache.struts.action. *;
Import javax.servlet.http. *;
// Inherited ActionForm
Public class loginform extends an actionform {
Private string name; // represents Name in the JSP page, note that the Name and the NAME in the page must be the same.
PRIVATE STRING Password;
/ / Set NAME value
Public string getname () {
Return Name;
}
Public void setname (String name) {
THIS.NAME = Name;
}
Public string getpassword () {
Return Password;
}
Public void setpassword (string password) {
this.password = password;
}
// Data check, you can do it yourself.
Public ActionerRors Validate (ActionMapping ActionMapping, HttpservletRequest HttpservletRequest) {
ActionerroS Errors = new actionerrors ();
IF (name == null) {
Errors.Add ("Name", New Actionerror ("Name Can Not Be Null!");
}
IF (Password.length () <3) {
Errors.Add ("Password", New ActionError ("Password Lengh At Least 3 Chars");
}
Return Errors;
}
// Data reset processing
Public Void Reset (ActionMapping ActionMapping, httpservletRequest httpservletRequest) {
Name = NULL;
Password = NULL;
}
}
Ok, this is one of our Models, only two fields of Name and Password, which inherits the ActionForm and has the Java Bean standard Getter and Setter methods. That's so much, let's take a look at what is in the view.
Struts VIEW
VIEW is our JSP page, but if you use struts, JSP is more different. Let us take a look at the code:
Login.jsp
<% @ 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"%>
login
title>
hEAD>
Username:
PASSWORD: