Instance Struts
Choosing pure JSP or a pure servlet design site has its limitations, and Struts is a powerful tool that links them together. The Struts can develop MVC mode-based applications, and the concept of MVC can see GOF's "design mode - the basis for object-oriented software".
What you have to do now is, download, install, configure the following tools, version different words may be some differences, specific to their documentation:
Tomcat 4.1.24 Apache 2.0.43, w / mod_jk2 2.0.43 Java 2 SDK Standard Edition 1.4.0 Struts 1.1 Eclipse 2.1.0
Struts is written with Java, which should be a JDK 1.2 or higher. If you use JDK 1.4, just like me, XML Parser and JDBC 2.0 Optional package binary have been included in the default.
new project
In this routine, we have to develop a simple web application that allows users to log in and log out. Simply put, the data is set to constants, not in the database, after all, Struts is Struts, not Java.
First create a directory in your Tomcat Configuration, for example, logoApp. Create a directory SRC and Web-INF in LogonApp, create directory classs and libs in Web-INF, copy struts.jar to lib directory from Struts distribution, and copy $ catalina_home / common / lib / servlets.jar to LiB directory. Copy all struts * .tld to the web-inflicity from the Struts distribution.
Now open Eclipse, you will see four views. Now we have a new project, click File -> New Project, open a window, select Java in the first pane, select Java Project in the second pane, click Next. Enter the project name (for a good memory, you are also called LogonApp), remove the check box of the USE DEFAULT check box, browse to the logoApp directory, click Next. A new window appears, click Add Folder on the Source Tab, add $ app_base / src, fill in $ app_base / web-inf / classes in Default Output Folder, click Finish. Click on Window -> Open Perspective -> Resource to see if the .project file has automatically contain all JAR files in the lib directory.
Your logoApp / web-inf / web.xml should look like this:
XML Version = "1.0"?>
Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
init-param>
servlet>
servlet-maping>
welcome-file-list>
taglib>
taglib>
taglib>
web-app>
Struts profile logoApp / web-inf / struts-config.xml is as follows:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
Type = "org.apache.struts.validator.dynavalidatorform"> form-bean> form-beans> global-forwards> TYPE = "org.monotonous.struts.logonaction" Name = "logonform" SCOPE = "session" INPUT = "Logon"> action> Type = "org.monotonous.struts.logoffaction"> action> action-mappings>
Local or Global "Forward" Rather Than Module-Relative Path -> controller> Struts-Config> Create View Now return to Eclipse to create a new page index.jsp: <% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%> <% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> hEAD>
html: Submit>
html: form>
body>
html: html>
Successfully logged in with page main.jsp:
<% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
hEAD>
html: link>
body>
html: html>
You may notice that these two pages use the convenient and international characteristics, this requires at least a default properties file ApplicationResources.properties:
Index.title = Struts HomePage
Prompt.username = username
Prompt.password = password
Index.logon = LOG ON
Main.title = Struts main page
Main.logoff = log off
Error.Password.mismatch = invalid username and / or password.
Create Controller
LogonAction.java:
Package org.monotonous.struts;
Import java.util.locale;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpsession;
Import javax.servlet.http.httpservletResponse; import org.apache.struts.Action.action;
Import org.apache.struts.Action.Actionerror;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.util.MessageResources;
Import org.apache.commons.beanutils.propertyUtils;
Public Final Class LogonAction Extends Action {
Public ActionForward Execute
ActionMapping mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws exception {
Locale locale = getLocale (request);
MessageResources Messages = GetResources (Request);
// Validate the Request Parameters Specified by The User
ActionerroS Errors = new actionerrors ();
String username =
(String) PropertyUtils.getsimpleProperty (Form, "UserName");
String password =
(String) PropertyUtils.getsimpleProperty (Form, "Password");
IF ((UserName! = "foo") || (Password! = "bar")))
Errors.Add (actionerroS.Global_ERROR,
New ActionError ("Error.Password.mismatch))));
// Report Any Erroors We Have Discovered Back to the Original Form
IF (! errors.isempty ()) {
SaveerRors (Request, Errors);
Mapping.getInputForward ());
}
// Save Our Logged-in User In the session
HttpSession session = request.getations ();
// Do Something with sessions ...
// Remove the Obsolete form bean
IF (mapping.getattribute ()! = null) {
IF ("REQUEST" .Equals (mapping.getscope ()))))
Request.Removettribute (mapping.getattribute ());
Else
Session.Removettribute (mapping.getattribute ());
}
// forward control to the specified surcess URI
Mapping.Findforward ("Success"));
}
}
Logoffaction.java:
Package org.monotonous.struts;
Import java.util.locale;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpsession;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.action;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.util.MessageResources;
Public final class logoffaction extends action {
Public ActionForward Execute
ActionMapping mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws exception {
Locale locale = getLocale (request);
MessageResources Messages = GetResources (Request);
HttpSession session = request.getations ();
Session.RemoveAttribute ("Userattrib");
session.INVALIDATE ();
// forward control to the specified surcess URI
Mapping.Findforward ("Success"));
}
}
Let's appreciate it in your browser, but when you can't open champagne, maybe you should consider some security measures for this app, next time I will talk.