Struts learning summary (a simple struts instance)

xiaoxiao2021-03-06  37

Since some of the following configurations are used to use Struts's things (or ideas), there is a more detailed explanation of Struts.

I. According to the practice, the introduction of the package first:

It is the downloaded jakarta-struts inside the package under libs, which is the fundamental need to use the Struts framework, which is supported by the framework.

two. Then the configuration file (this is also the most important step, the relationship can not run).

Since the Web Project is made, the general web project configuration file web.xml is required, web-inf, web-inf / classes, web-inf / lib. It is also necessary. If you use the Struts tag (recommended on the Struts framework), there is a web-inf / tlds

Use the TLD file in Jakarta-Struts inside.

Then the Struts configuration file struts-config.xml. This is the foundation of this framework.

Examples of these two profiles are as follows:

Web.xml:

Action

Org.apache.struts.Action.ActionServlet

config

/web-inf/struts-config.xml

debug

3

detail

3

0

Action

*. do

http://jakarta.apache.org/struts/tags-bean

/web-inf/tlds/struts-bean.tld

http://jakarta.apache.org/struts/tags-html /web-inf/tlds/struts-html.tld

http://jakarta.apache.org/struts/tags-logic

/web-inf/tlds/struts-logic.tld

Struts-config.xml:

Attribute = "Helloform"

Name = "Helloform"

Path = "/ hello"

Scope = "request"

TYPE = "com.ln.struts.action.helloaction">

three. About the writing of the program: (take the most famous Hello World as an example)

Generally, there is a Form with an action, as well as a JSP page. FORM is used to interact with the page, and then obtain the page data via Form to make the corresponding processing, then the page transfer control is performed by calling Return FinForward.

Examples are as follows:

HelloAction.java:

// Created by myeclipse struts

// xsl source (default): platform: /plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.3/xslt/javaclass.xsl

Package com.ln.struts.Action;

Import javax.servlet.http.httpservletRequest; 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 com.ln.struts.form.helloform;

/ **

* MyECLIPSE STRUTS

* Creation Date: 01-14-2005

*

* Xdoclet definition:

* @struts: action path = "/ hello" name = "helloform" scope = "request" validate = "true"

* @struts: action-forward name = "Hello" path = "/ hello.jsp"

* /

Public class helloaction extends action {

/ / -------------------------------------------------------------------------------------------- --------- Instance Variables

/ / -------------------------------------------------------------------------------------------- --------- Methods

/ **

* Method Execute

* @Param mapping

* @Param Form

* @Param Request

* @Param Response

* @Return ActionForward

* /

Public ActionForward Execute

ActionMapping mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

HttpservletResponse response) {

Helloform Helloform = (Helloform) Form;

Helloform.SETMSG ("Hello World!");

Request.setttribute ("Helloform", Helloform);

Return mapping.findforward ("Hello");

}

}

Helloform.java:

// Created by myeclipse struts

// xsl source (default): platform: /plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.3/xslt/javaclass.xsl

Package com.ln.struts.form;

Import org.apache.struts.Action.actionform;

/ **

* MyECLIPSE STRUTS

* Creation Date: 01-14-2005

*

* Xdoclet definition:

* @struts: form name = "Helloform"

* /

Public class helloform extends actionform {

/ / -------------------------------------------------------------------------------------------- --------- Instance Variables / ** msg property * /

PRIVATE STRING MSG;

/ / -------------------------------------------------------------------------------------------- --------- Methods

/ **

* Returns the msg.

* @Return String

* /

Public String getmsg () {

Return MSG;

}

/ **

* Set the msg.

* @Param msg the msg to set

* /

Public void setmsg (String MSG) {

THIS.MSG = MSG;

}

}

Hello.jsp

<% @ page language = "java"%>

<% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "bean"%>

<% @ taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%>

<% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-logic" prefix = "logic"%>%>

hello.jsp </ title></p> <p></ hEAD></p> <p><body></p> <p><html: form action = "/ hello" method = "post"></p> <p><bean: write name = "Helloform" property = "msg" /></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>INDEX.JSP:</p> <p><% @ page language = "java"%></p> <p><% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "bean"%></p> <p><% @ taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%></p> <p><% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-logic" prefix = "logic"%>%></p> <p><! Doctype html public "- // w3c // DTD HTML 4.01 Transitional // En"></p> <p><html: html></p> <p><HEAD></p> <p><title> index.jsp </ title></p> <p></ hEAD></p> <p><body></p> <p>This a struts. <br></p> <p><logic: redirect page = "/ hello.do"> </ logic: redirect></p> <p></ body></p> <p></ html: html></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-61725.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="61725" 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.043</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 = 'crZeQDMGNPxccVZH35_2FdQ2lccNqX27xslWUH3PcGjyACUCkOFrk7TbdfcQom1MhfswYrk1ciZI51mq5iZnTrEA_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>