A user registration system, the user enters information via the web page: Register the ID number, password, email, if the registration is successful, return successful prompt information, and the registration failed prompt information.
The following is some of the core code of the related files.
Project is established:
Before formal development, you need to establish this item in TOCMAT (my Tomcat installed in C: / Tomcat). A faster setup method is: New Directory TEST under C: / Tomcat / WebApps, then under C: / Tomcat / WebApps / Struts-EXAMPLE
Web-INF directory copy to the TEST directory, then empty the SRC and CLASSES directory under Test / Web-INF, and clear the content in the struts-config.xml file. This way, the Struts package and related configuration files we need are all.
When developing, put the JSP file in the Test directory, the Java original file is placed under Test / Web-INF / SRC, and the compiled class file is placed under Test / Web-INF / CLASSES.
This JSP page is different from a normal JSP page, because it uses Taglib, which may be difficult to master for beginners, but this is one of the essence of Struts. Flexible use, will greatly improve the development efficiency.
Struts-config.xml:
TYPE = "Org.cjea.struts.example. reguserform" />
form-beans>
TYPE = "org.cjea.struts.example.Reguserac"
Attribute = "reguserform"
Scope = "request"
Validate = "false">
action>
action-mappings>
Struts-Config>
The core of Struts is Controller, namely ActionServlet, and the core of ActionServlet is struts-config.xml, and struts-config.xml sets all pages navigation definitions. For large Web projects, you can quickly grasp the context through this profile, whether it is for the early development, or later maintenance or upgrade is very beneficial. Master Struts-Config.xml is the key to master 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;
}
}
Every FormBean must inherit the Actionform class, and the FormBean is a package requested to page. That is, the HTTP Request package is in an object, and the point to explain is that multiple HTTP Request can share a FORMBEAN to facilitate maintenance and reuse.
ActionBean: ReguSerction
Package org.cjea.struts.example;
Import javax.servlet.http. *;
Import org.apache.struts.action. *;
Public Final Class ReguSerction 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");
/ *
Take the user request, do the corresponding database operation, slightly
* /
}
}
The generation of FORMBEAN is to provide data to ActionBean, and data in FormBean can be obtained in ActionBean. After the corresponding logic is processed, the business method completes the corresponding business requirements.
Evolution of servlet: In the routine JSP, Servlet, JavaBean three-layer structure, JSP implements the functionality of the View, the servlet implementation of the Controller function, JavaBean implements Model implementation.
In Struts, the servlet split in conventional cases is part of the ActionServlet, FORMBEAN, ActionBean. ActionServlet works with Struts-Config.xml, full-time completion page navigation, and no longer responsible for the specific data acquisition and corresponding logic, which is completed by FormBean and ActionBean. Struts advantages and disadvantages
advantage:
Struts is the same as Tomcat, Turbine, etc., is open source software, which is a great advantage. Make developers more in-depth understanding of their internal implementation mechanisms.
In addition, Struts's advantages are mainly concentrated in two aspects: taglib and page navigation. Taglib is the Struts tag library, flexible, which can greatly improve development efficiency. In addition, in addition to the current Domestic JSP developers, in addition to the common tags that come with JSP, we have rarely develop their own tags, maybe Struts is a good starting point.
About page navigation, I think that will be a development direction in the future, in fact, doing so, make the system's context more clear. With a configuration file, you can grasp the contact between the entire system, which has a great advantage for later maintenance. Especially when another group of developers take over the project, this advantage is more apparent.
Disadvantages:
Taglib is a great advantage of Struts, but for beginners, it is necessary to continue to learn, and even chaos your habits written, but when you are used to it, you will feel that it is really Baton.
Struts divided the MVC's Controller one by three, and also increased the complexity of the system while gaining the structure clearer.
Struts has been generated less than half a year from now, but it has gradually been used for commercial software. Although it still has a lot of shortcomings, it is a very good J2EE MVC implementation. If your system is ready to adopt J2EE MVC architecture, then you may wish to consider Struts.
Struts implementation experience:
1. Project development based on the Struts architecture, first of all, there is a good overall plan, including which modules included in the entire system, each module needs how much formbean and ActionBean, etc., and it is best to be Struts-config.xml Management. The difficulty of developing Struts-based projects is that configuration management, especially for Struts-Config.xml
2, if your project is very tight, and there is no experience in the project team, it is recommended not to take Struts. Struts needs a process that requires a skilled JSP programmer, you need to take about half a month. If you combine titls, you need a longer time.
3, if you use Taglib in a large number in the web, your art will make some sacrifice. This sacrific is particularly obvious when you combine Tiles. Of course, you will decide yourself by your own.
4, taglib is a good thing, but flexible uses it requires a process, if you don't want to spend too much time on taglib, then just understand several tags related to Form, other tags are put, Look again, first study ActionServlet and Struts-Config.xml, you will feel very accomplishment
5, is Struts only suitable for large projects? NO! Struts is suitable for projects of various sizes. Of course, for large projects, it is more obvious.