Initial knowledge of architecture and pattern-struts framework

Source: Internet
Author: User
Tags object config final http request string version tld tomcat
Architecture

1. How to install Struts:

First to http://jakarta.apache.org/Struts download struts, the recommended release version, now the highest version of 1.2.6, there are a variety of OS versions (Windows,linus ...), download and extract, You can see this directory: There are some war files under Lib and Webapps,webapps. If you have Tomcat installed under C:tomcat, copy those war files to C:tomcatwebapps and restart Tomcat. Open the browser, enter in the Address bar: http://localhost:8080/Struts-example/index.jsp, if you can see the "Powered by Struts" dark blue icon, that means successful. This is an example of struts, with a detailed documentation that can be a beginner's introductory tutorial. In addition, struts provides a system utility object: XML processing, automatic processing of JavaBeans attributes via Java Reflection APIs, internationalization hints and messages, and so on

2. Practice doing an example:

A user registration system, the user input related information through the Web: Registration ID number, password, EMAIL, if the registration is successful, then return the success of the message, in turn, the registration failure prompted information.

The following are some of the core code for the related file.

Project Establishment:

This project needs to be established in Tocmat (my tomcat is installed in C:omcat) before it is formally developed. A quicker way to build a new directory under C:omcatwebapps is to test the next c:omcatwebappsstruts-example

The Web-inf directory is copied to the test directory, then the SRC and classes directories under Testweb-inf are emptied, and the contents of struts-config.xml files are emptied. In this way, we need the struts class package and the related configuration files are all together.

Development, the JSP file in the test directory, Java original file placed under the TESTWEB-INFSRC, the compiled class files under Testweb-infclasses.

Registration page: reguser.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"% >

< html:html locale= "true" >

< head >

< title >RegUser</title >

< html:base/>

< body bgcolor= ' white ' >

< html:errors/>

< Html:form action= "/reguseraction" focus= "logname" >

< table border= "0" width= "100%" >

< tr >

< th align= "right" >

Logname:

</th >

< TD align= "left" >

< Html:text property= "logname" size= "maxlength="/>

</td >

</tr >

< tr >

< th align= "right" >

Password:

</th >

< TD align= "left" >

< Html:password property= "password" size= "maxlength="/>

</td >

</tr >

< tr >

< th align= "right" >

E-Mail:

</th >

< TD align= "left" >

< Html:password property= "email" size= "maxlength="/>

</td >

</tr >

< tr >

< TD align= "right" >

< Html:submit property= "submit" value= "submit"/>

</td >

< TD align= "left" >

< html:reset/>

</td >

</tr >

</table >

</body >

This JSP page is different from the normal JSP page, because it uses a lot of taglib, these taglib for beginners, may be difficult to grasp, but this is one of the essence of struts. Flexible application, will greatly improve the development efficiency.

Struts-config.xml:

< Struts-config >

< Form-beans >

< Form-bean name= "Reguserform"

Type= "Org.cjea.Struts.example. Reguserform "/>

</form-beans >

< action-mappings >

< action path= "/reguseraction"

Type= "Org.cjea.Struts.example.RegUserAction"

Attribute= "Reguserform"

Scope= "Request"

Validate= "false" >

< forward name= "failure" path= "/messagefailure.jsp"/>

< forward Name= "Success" path= "/messagesuccess.jsp"/>

</action >

</action-mappings >

</struts-config >

The core of Struts is controller, the Actionservlet, and the core of Actionservlet is the struts-config.xml,struts-config.xml to centralize the navigation definitions of all the pages. For large Web projects, you can quickly grasp the context of this configuration file, whether for early development, or later maintenance or upgrades are helpful. Mastering Struts-config.xml is the key to mastering struts.

Formbean:reguserform

Package org.cjea.Struts.example;

Import javax. Servlet.http.HttpServletRequest;

Import Org.apache.Struts.action.ActionForm;

Import org.apache.Struts.action.ActionMapping;

Public final class Reguserform extends actionform{

Private String logname;

private String password;

Private String Email;

Public Reguserform () {

LogName = null;

Password = null;

email = null;

}

Public String Getlogname () {

return this.logname;

}

public void Setlogname (String logname) {

This.logname = logname;

}

public void SetPassword (String password) {

This.password = password;

}

Public String GetPassword () {

return This.password;

}

public void Setemail (String email) {

This.email = email;

}

Public String Getemail () {

return this.email;

}

public void Reset (actionmapping mapping, httpservletrequest request)

{

LogName = null;

Password = null;

email = null;

}

}

Each formbean must inherit the Actionform class, Formbean is the encapsulation of the page request. To encapsulate HTTP request in an object, it is important to note that multiple HTTP request can share a formbean for easy maintenance and reuse.

Actionbean:reguseraction

Package org.cjea.Struts.example;

Import javax. servlet.http.*;

Import org.apache.struts.action.*;

Public final class Reguseraction extends Action

{

Public Actionforward Perform (actionmapping mapping,

Actionform form, HttpServletRequest req,

HttpServletResponse Res)

{

String title = Req.getparameter ("title");

String Password = req.getparameter ("password");

String email = req.getparameter ("email");

/*

Get the user request, do the corresponding database operation, slightly

*/

}

}

Formbean is produced to provide data to Actionbean, in the Actionbean can obtain the data encapsulated in Formbean, after the corresponding logic processing, call the business method to complete the corresponding business requirements.

The evolution of the Servlet: in the conventional Jsp,servlet,javabean three-tier structure, JSP implementation view function, Servlet implementation controller function, JavaBean implement model implementation.

In struts, the general situation of the servlet is split with the Actionservlet, Formbean, Actionbean three parts. Actionservlet with Struts-config.xml, full-time completion of the page navigation, and no longer responsible for specific data acquisition and the corresponding logic, these two parts of the function by Formbean and Actionbean to complete.

3.Struts Advantages and Disadvantages

Advantages:

Struts, like Tomcat, turbine and many other Apache projects, is open source software, which is one of its great advantages. To give developers a deeper understanding of their internal implementation mechanisms.

In addition, the advantages of struts are mainly embodied in two aspects: Taglib and page navigation. Taglib is a marker library of struts, which can greatly improve the development efficiency by using flexibly. In addition, the current domestic JSP developers, in addition to the use of JSP with the common tag, rarely develop their own tags, perhaps struts is a good starting point.

About page navigation, I think that will be the future direction of development, in fact, this makes the system's context clearer. Through a configuration file, you can grasp the whole system of the relationship between the parts, which for the maintenance of the latter has great benefits. This is especially true when another group of developers takes over the project.

Disadvantages:

Taglib is one of the great strengths of struts, but for starters, it takes a continuous learning process and even disrupts your web authoring habits, but when you get used to it, you think it's really great.

Struts divides the controller of MVC into three, increasing the complexity of the system while obtaining a clearer structure.

Struts from the generation to now less than six months, but has gradually more and more for commercial software. Although it still has a lot of shortcomings, but it is a very good Java EE MVC implementation, if your system is ready to use the Java-EE MVC architecture, then may wish to consider struts.

4.Struts Implementation Experience:

1, based on the framework of struts project development, the first need to have a good overall planning, the entire system includes which modules, each module needs how many Formbean and Actionbean, and preferably have a person responsible for struts-config.xml management. The difficulty in developing a project based on struts lies in configuration management, especially for Struts-config.xml management

2, if your project is very tight, and the project team does not have experienced struts developers, it is recommended not to take struts. The mastery of struts requires a process, and for a skilled JSP programmer, self-study may take about half a month. If combined with TITLS, it will take a longer time

3, if you use a lot of taglib in the Web page, your art will make some sacrifices. This sacrifice is especially noticeable when you combine tiles and enhance the function. Of course, your choice of function and beauty is up to you.

4, Taglib is a good thing, but the flexibility to use it requires a process, if you do not want to spend too much time on the taglib, then just understand the form related to a few tags, the other tags on it, and then see, You'll feel a sense of accomplishment to study Actionservlet and Struts-config.xml first.

5. Is struts only suitable for large projects? No! Struts is suitable for projects of all sizes, and of course, for large projects, the advantages it embodies are more pronounced.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.