[Struts] instance learning struts fast Dad's bear @ 2004-07-31 14:27 Transfer from http://vip.6to23.com/hanson/htdocs/struts.htm Select pure JSP or pure servlet design site has it Limitations, 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 can be different, see their documents: 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 requires 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 projects In this routine we have to develop a simple web application, allowing 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"?>
XML Version = "1.0" Encoding = "ISO-8859-1">
Message-resources parameter = "org.monotonous.struts.ApplicationResources" /> struts-config> Creating View Now returns to Eclipse, 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 "%>
<% @ 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"%>
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.ActionErrors; 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 ActionErrors errors = new ActionErrors ( ); String username = (String) PropertyUtils.getSimpleProperty (form, "username"); String password = (String) PropertyUtils.getSimpleProperty (form, "password");! If ((username = "foo") || (password ! = "BAR")) Errors.add (actionerrors.global_error, new actionerror ("error.password.mismch")); // report any errors we have attovered back to the Original Form If (! errors.Isempty ()) {SaveError (Request, Errors); Return (mapping.getinputforward ());} // save our logged -in user in the session httpsession session = request.getations ();
// DO Something with sessions ... // Remove the obsole form bean if (mapping.getattribute ()! = Null) {if ("request" .Equals ()) Request.removeAttribute (mapping.getattribute ()); else session.removeAttribute (mapping.getAttribute ());} // Forward control to the specified success URI return (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.getSession (); session.removeAttribute ( "userattrib"); session.invalidate (); / / Forward Control to the specified success uri return ("success"));}} Appreciate it in the browser, but when you can't open champagne, maybe you should consider some security measures for this application. Let me talk about it next time.