Struts Development Guide workflow

xiaoxiao2021-03-06  40

Struts Development Guide workflow

The picture below is the workflow of Struts. We mentioned before, all requests are submitted to the ActionServlet.

ActionServlet is a FrontController that is a standard servlet, which forwards request to RequestProcessor,

Actionmapping is a subclass of ActionConfig, essentially a map of Struts-Config.xml, which can get all configuration information from it.

RequestProcessor Gets the appropriate actionforn and action from ActionMapping based on the submitted URL, such as * .do. Then correspond to the parameters of the Request to ActionForm, perform Form verification. If verification is called an action () method () method to perform an action, eventually returns ActionFoward.

ActionFoward is a packaging of a foward in mapping, corresponding to a URL

Actionform uses viewhelper mode and is a package for Form in HTML. It includes a validate method for verifying the effectiveness of the FORM data. ActionForm is a class that meets the JavaBean specification, all attributes should meet GET and SET. For some complex systems, DynaActionForm can also be used to construct a dynamic Form, which is generated by a prefabricated parameter. This makes a more flexible extension program.

ActionerRORS is a packaging for error messages, once an exception occurs in execution or form.validate, you can generate an actionerror and eventually add to ActionerRors. During the Form verification, if there is ERROR, the page will be re-directed to the input page, and the error is prompted.

Action is the REQUSESTHANDLER for performing business logic. Each Action only establishes an instance. Action is not a thread safe, so you should not access specific resources in the action. In general, the business delegate mode should be changed to access the business tier to unprepose.

Struts provides a variety of Action for selection. Ordinary Action can only perform a task by calling Execute, and DispatchAction can be performed according to the configuration parameters, not only enter the execute () function, which can perform a variety of tasks. Such as INSERT, UPDATE, etc. The LookUpdispatchaction can perform a function according to the name of the submitting form button.

We can go back to the examples just now, understand the Struts process.

Let's take a look at the EXAMPLE example of Struts:

Note: Example 2 is the Example program comes with Struts, which implements login, registration, and modify the function.

Struts Taglib is applied in the code, and the Validator plugin is used to verify the FORM.

But the code sets a bad example, which is written in an action of a lot of business logic.

Part of the code is as follows:

Logon.jsp

<% @ Page ContentType = "Text / HTML; Charset = UTF-8" Language = "Java"%> // Declaration Taglib <% @ Taglib URI = "/ Web-inf / struts-bean.tld" prefix = "bean "%> <% @ Taglib URI =" / Web-inf / struts-html.tld "prefix =" html "%> // bean is used from ApplicationResource Read i18n information <bean: message key = "logon.title" /> </ title> <html: base /> </ head> <body bgcolor = "white"> // error message section <HTML: Errors /> // Login Form, Action is logion.do <html: form action = "/ logon" FOCUS = "UserName" οnsubmit = "Return Validatelogonform (this);"> <table border = "0" width = "100 %> <Tr> <TH Align = "Right"> <bean: Message Key = "Prompt.user" />: </ th> <TD align = "left"> <html: text property = "username" Size = "16" MaxLength = "18" /> <tr> <trh align = "right"> <bean: message key = "prompt.password" bundle = "alternate" />: < / TH> <TD align = "left"> <html: password property = "password" size = "16" maxlength = "18" redisplay = "false" /> </ td> </ tr> <tr> <TD Align = "Right"> <</p> <p>HTML: Submit value = "submit" /> </ td> <TD align = "left"> <html: reset /> </ td> </ tr> </ table> </ html: form> // validator plugin for verification form <html: javascript formName = "logonForm" dynamicJavascript = "true" staticJavascript = "false" /> <script language = "Javascript1.1" src = "staticJavascript.jsp"> </ script> </ body > </ Html: html> struts-config.xml configuration</p> <p><Form-beans> <!-Bean name = "logonform" type = "org.apache.struts.validator.dynavalidatorform"> <form-protety name = "username" type = " Java.lang.String "/> <form-property name =" password "type =" java.lang.string "/> <form-bean> <!" form-bean-> <form-bean name = "subscriptionform" type = "org.apache.struts.Webapp.example.subscriptionform" /> </ form-beans> <action-mapings> <! - edit mail subscription -> <action path = "/ editsubscription" TYPE = "org.apache.struts.webapp.example.EditSubscriptionAction" attribute = "subscriptionForm" scope = "request" validate = "false"> <forward name = "failure" path = "/ mainMenu.jsp" /> <forward name = "Success" Path = "/ subscription.jsp" /> </ action> ...</p> <p>subscriptionForm is a standard ActionForm, wherein the method is used to clear reset the value form, validate a method for verifying public final class SubscriptionForm extends ActionForm {// The maintenance action we are performing (Create or Edit) .private String action = "Create" ;? // Should we auto-connect at startup time private boolean autoConnect = false; // The host name.private String host = null; private String password = null; private String type = null; private String username = null; public String getAction () {return (this.action);} public void setAction (String action) {this.action = action;} public boolean getAutoConnect () {return (this.autoConnect);} public void setAutoConnect (boolean autoConnect) {this .autoConnect = autoConnect;} public String getHost () {return (this.host);} public void setHost (String host) {this.host = host;} public String getPassword () {return (this.password);} public Void setPassword (String password) {this.password = password;} public string gettype () {return (this.type);} public void settype (string type) {this. type = type;} public String getUsername () {return (this.username);} public void setUsername (String username) {this.username = username;.} / *** Reset all properties to their default values ​​** @param mapping The mapping used to select this instance * @param request The servlet request we are processing * / public void reset (ActionMapping mapping, HttpServletRequest request) {this.action = "Create"; this.autoConnect = false; this.host = null This.Password = null; this.Type = null; this.username = null;} / *** Validate The Properties That Have Been set from this http request, * and return AN <code> ActionerRors </ code></p> <p>object that encapsulates any * validation errors that have been found. If no errors are found, return * <code> null </ code> or an <code> ActionErrors </ code> object with no * recorded error messages. ** @param mapping The mapping used to select this instance * @param request The servlet request we are processing * / public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = new ActionErrors (); if ((host == null) || ( Host.Length () <1)) Errors.Add ("Host", New Actionerror ("Error.host.Required"); 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)); if ((Type == Null) || (Type.Length () <1)) Errors.Add (" Type ", New ActionError (" Error.Type.Required " )); ELSE IF (! "" "&&!" POP3 ".Equals (type)) errors.add (" Type ", New Actionerror (" Error.Type.invalid ", TYPE); Return (Errors);}} logonaction</p> <p>public final class LogonAction extends Action {/ *** Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code> ActionForward </ code> instance describing where and how * control should be forwarded, or <code> null </ code> if the response has * already been completed. ** @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating ** @exception Exception if business logic throws an exception * / public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {// Extract attributes we will need Locale locale = getLocale (request); MessageResources messages = getResources (request); User user = null; // Validate the request parameters specified by the user Action Errors errors = new ActionErrors (); String username = (String) PropertyUtils.getSimpleProperty (form, "username"); String password = (String) PropertyUtils.getSimpleProperty (form, "password"); UserDatabase database = (UserDatabase) servlet. . getServletContext () getAttribute (Constants.DATABASE_KEY); if (database == null) errors.add (ActionErrors.GLOBAL_ERROR, new ActionError ( "error.database.missing")); else {user = getUser (database, username); IF ((user! = null) &&! user.getpassword (). Equals (password)) user = null; if (user ==</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-68961.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="68961" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'idl5lNaTTUYh_2BId3zSPCnyyzkYDlcKTL2YVOR2022wOqkm93BufzMUnVa950nJ9lvCMzYff5bhgCylRLI16tqQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>