Struts development

zhaozj2021-02-16  55

Our first step began to configure Tomcat connection pool

First we put the mysql's JDBC driver to $ {tomcat_home} / circat_home} / common / lib, change the MyWeb settings as follows

Workdir = "E: / dev / myweb / work / org / apache / jsp">

Set the JNDi name of the connection pool

->

TYPE = "javax.sql.datasource" />

Factory class

->

factory

org.apache.commons.dbcp.basicDataSourceFactory

Connection pool basic settings

->

maxactive

100

maxidle

30

maxwait

10000

We write resource files

Password = Password format error

UserName = account format error

HBM.Session.Error = Establish an Hibernate Session error.

No.user = This is not found to have users.

HBM.Session.Query = Hibernate HQL query error.

Errors.Header =

    Errors.footer =

    Logon = landing

    Logon.username = account number

    Logon.password = password

    Save to C: /1.txt

    Then run

    Native2ascii -Encoding GBK C: /1.TXT C: /ApplicationResources_zh_cn.properties

    Will be generated C: /ApplicationResources_zh_cn.properties to copy to the src directory, we re-run this login program.

    There is garbled, we forgot to set the JSP page encoding into UTF-8, add

    ContentType = "text / html; charSet = UTF-8", refresh again

    OK success

    When you enter Chinese in the account box, it will appear in the picture.

    There is a garbled, such a case solution has 3: 1. Set Response to coding in each action, this method is more cumbersome, and each Action must be set once.

    2. Use the filter in servlet2.3, unified settings

    3. Overload the Process function in the ActionServlet.

    The first method is difficult to obtain an encoding method through an XML configuration.

    2, 3 can be solved by configuring parameters in Web.xml

    This article describes how to use it, filter Filter

    There is a ready-made coded filter under the Filter folder inside the Samples in Tomcat.

    Copying, in Eclipse uses reconstruction, solve the Package mismatch problem.

    The following is a web.xml for this filter.

    Filter

    ->

    Set Character Encoding

    com.yjsoft.filter.setcharacterencodingfilter

    Encoding

    UTF-8

    Set Character Encoding

    / *

    end

    ->

    Start Tomcat test again, OK. Below start processing JSP page error

    Add an error page, the content is as follows:

    <% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

    <% @ page isrrorpage = "true"%>

    ... other omitted, please see the source file

    When you want to handle Exception on other pages, just add it on the head

    <% @ Page ErrorPage = "Error.jsp"%>

    The test is as follows:

    <% @ page language = "java" contenttype = "text / html; charSet = UTF-8"%>

    <% @ Taglib Uri = "/ Tags / Struts-Bean" prefix = "bean"%>

    <% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%>

    <% @ Page ErrorPage = "Error.jsp"%>

    <%

    INT n = 0;

    INT N1 = 10 / N;

    %>

    Turn the error page when requesting:

    Test the Exception in the action below.

    Now let's start processing for an exception definition in the action.

    Configure global Exception first

    Modify our logonAction so that it throws an exception. Such a global configuration file can be processed.

    The above verification is through the Server terminal, we inherit ActionForm, in fact, we have a simpler way, that is, using the Validator component.

    Using the Validator component, you can support both the client's verification, while the Server end is also verified. At the same time, the verification method can be reused.

    Let's meet the Validator of Struts1.1.

    1. Validators A Validator is a Java class that calls the Validator framework when performing a verification rule. The framework calls this Validaotor class based on the method defined in the configuration file. In general, each Validator class provides a separate verification rule, and then these rules can be combined into a more complex rule set. The Validator framework provides most of the validation rules that the web application needs. You can use these existing verification rules to create your own verification profile. We can also add more Validator as needed.

    2. Profile

    Struts1.1 Validator Framework, uses the two profiles, one called Validator-Rules.xml, another called Validation.xml; actually you can also name them at will, and you can even merge them into an XML file. However, you are still best to separate them because they have their own uses.

    Validator-rules.xml file validator-rules.xml file Define the validator that the application can use. Validator-rules.xml acts as a template's role, define the validator that all applications may be used.

    Below we redesigned our logonform

    Change to

    Public Class logonform extends ValidatorForm / * Actionform * /

    We commented out an ActionForm's validate () function.

    We configure the struts-config.xml file, and the file is finally increased.

    Let's modify the validation.xml file

    increase

    Property = "Username"

    Depends = "Required">

    Property = "Password"

    Depends = "Required">

    Increase in resource files

    Errors.Required = {0} is required.

    Restart the program, then the login program server is authenticated.

    Let's add the Client's JavaScript authentication.

    Modify logon.jsp

    <% @ page language = "java" contenttype = "text / html; charSet = UTF-8"%>

    <% @ Taglib Uri = "/ Tags / Struts-Bean" prefix = "bean"%>

    <% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%>

    <% @ Page ErrorPage = "Error.jsp"%>

    <bean: message key = "logon" /> </ title></p> <p></ hEAD></p> <p><body></p> <p><html: Errors Property = "HBm.Session.Error" /></p> <p><html: errors proty = "no.user" /></p> <p><html: errors proty = "hbm.session.query" /></p> <p><html: form action = "/ jsp / logon.do" FOCUS = "username"</p> <p>οnsubmit = "Return ValidateLogonform (this)"></p> <p><bean: message key = "logon.username" />: <html: text protety = "username" /> <html: errors proty = "username" /> </br></p> <p><bean: message key = "logon.password" />: <html: text proteth = "password" /> <html: errors proty = "password" /> </br></p> <p><HTML: Submit></p> <p><bean: message key = "logon.submit" /></p> <p></ html: Submit></p> <p></ html: form></p> <p><html: javascript Dynamicjavascript = "true" staticjavascript = "true" formname = "logonform" /></p> <p><body></p> <p></ html></p> <p>OK</p> <p>Next, press the Excel program to start implementing the previous thoughts.</p> <p>Download Excel Plan, this period source file</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-19845.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="19845" 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.038</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 = 'wQmGAbT_2F88zEGi6DwXSgYA78vvBo45keGy9Nb3YWqYLNiTmQlRCG8yN5ADnduBuWTTiL_2BM4jSCBokfvS'; 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>