Struts's first experience
Baitianhai baichenhong@hotmail.com
Development Environment WINDOW2000 JBuilder7 Tomcat4.1.12
First, download jakarta-struts-1.0.2.zip from http://jakarta.apache.org, there are two main directories lib and webapps after unwrapping, in the lib directory, use Struts to create applications, There are several suffixes under the webapps directory, familiar with Java Web development is more clear, put them under Tomcat / WebApps / Directory, and then restart Tomcat, Tomcat will deploy these web projects. Struts-Documentation.war is a document, struts-example.war is a good example, I mainly refer to this example. Then establish the struts directory in the JBuilder installation directory / lib / down, go in the things under the above lib directory, then start JBuilder7, run the menu Tools-> configure-> libraries-> new-> name Enter Struts, then select Just COPY The past struts.jar file, then OK.
Second, newly established a project, refer to struts, server selection Tomcat4, then build a jsp1.jsp file, then put all the .tld files under the lib / directory to your created project directory / default / Web-infault / Directory Below, in / default / web-INF / build action.xml, the content is as follows:
ActionClass = "eXample.logonaction" Formattribute = "logonform" Formclass = "eXample.logonform" InputForm = "/ logon.jsp"> action> action-mappings> The struts-config.xml file is as follows: Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.0 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> form-beans> global-forwards> TYPE = "eXample.logonaction" Name = "logonform" Scope = "request" INPUT = "/ logon.jsp"> action> Type = "org.apache.struts.Actions.addformbection" /> Type = "org.apache.struts.Actions.addforwardAction" /> Type = "org.apache.struts.Actions.addmapping" /> TYPE = "org.apache.struts.Actions.ReloadAction" /> Type = "org.apache.struts.Actions.removeformbection" /> TYPE = "org.apache.struts.Actions.removeforwardAction" /> TYPE = "org.apache.struts.Actions.removemapping" /> action-mappings> struts-config> then establishes Logon.jsp, the content is as follows: <% @ page language = "java"%> <% @ taglib URI = "/ web-inf / struts-bean.tld" prefix = "bean"% > <% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> Package example; Import org.apache.struts.Action.actionform; Import javax.servlet.http.httpservletRequest; import org.apache.struts.Action.Actionerror; Import org.apache.struts.Action.Actionerro; Import org.apache.struts.Action.actionform; Import org.apache.struts.action.actionmapping; Public class logonform extends actionform { Private string password = null; Private string username = null; Public string getpassword () { Return Password; } Public string getUsername () { Return UserName; } Public void setpassword (string password) { this.password = password; } Public void setusername (String username) { THIS.USERNAME = UserName; } Public void reset (actionMapping mapping, httpservletRequest request) { This.Password = NULL; THIS.USERNAME = NULL; } Public ActionerRors Validate (ActionMapping Mapping, HttpservletRequest request) { ActionerroS Errors = new actionerrors (); IF ((username == null) || (username.length () <1)) Errors.Add ("UserName", New Actionerror ("Error.userName.Required)); IF ((Password == null) || (Password.Length () <1)) Errors.Add ("Password", New ActionError ("Error.Password.Required)); Return Errors; } } Then build a class logonaction.java, inherit from Action, packaged in the example content as follows Package example; Import java.io.ioException; Import java.util.hashtable; Import java.util.locale; Import javax.servlet.RequestDispatcher; Import javax.servlet.servletException; 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; Imports.Apache.struts.action.actionform; import org.apache.struts.Action.actionforward; Import org.apache.struts.action.actionmapping; Import org.apache.struts.action.ActionServlet; Import org.apache.struts.util.MessageResources; Public class logonaction extends action { Public ActionForward Perform (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response THROWS IOException, servletexception { Locale locale = getLocale (request); // Messageresources Messages = getResources (); ActionerroS Errors = new actionerrors (); String username = ((logonform) form .getusername (); String password = ((logonform) form .Getpassword (); // Report Any Erroors We Have Discovered Back to the Original Form IF (! errors.empty ()) { SaveerRors (Request, Errors); Return (maping.getInput ()))); } // Save Our Logged-in User In the session HttpSession session = request.getations (); Session.SetaTRibute ("User", "OK"); IF (servlet.getdebug ()> = 1) servlet.log ("LogonAction: user:" "'logged on in session" session.getID ()); // 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")); } Then modify the /defaultroot/web-inf/web.xml file as XML Version = "1.0" Encoding = "UTF-8"?> URL-PATTERN> *. do url-pattern> servlet-maping> < HTML: Text Property = "UserName" size = "16" maxlength = "16" />