The relationship between the Struts program
The relationship between the "How to achieve the simplest Struts program" example in each file and the source code analysis are described.
[Related Links]
"JavaWebstudio Series Development Tools Visaul Struts Version" http://dev.9cbs.net/develop/Article/28/28871.shtm
"How to achieve the simplest Struts program" http://dev.9cbs.net/develop/Article/28/28946.shtm
"The implementation of the Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.shtm
"The implementation of the Sruts radio box" http://dev.9cbs.net/develop/Article/29/29042.shtm
"Struts check box" http://dev.9cbs.net/develop/Article/29/29043.shtm
Development Environment: Java Web Studio Series Development Environment Visual Strutst Version.
The latest JavaWebStudio download address: ftp: //210.36.64.79/kui
[background knowledge]
Struts uses the MVC structure; the logic diagram shown in Figure 2.1 describes the high-level structure of the frame.
We now discuss each element in the figure.
A representation of a Struts-based application (VIEW) is built using the Struts Tag Library (Taglibs). Request from the customer is passed to the servlet called ActionServlet, which uses the Struts application, all requests that need to pass through the ActionServlet. This ActionServlet passes data from request to Actionform JavaBean.
ActionForm is a JavaBean that represents data from a form view component. These forms are generated by JSP with the Struts HTML tag library. This BEAM is generated by ActionServlet, with a request parameter, which also requests ActionForm to verify the legality of the user's submit data.
ActionServlet is configured by defining a set of actionMApping. An ActionMApping is an object that maps the URL in the request to the components of the process requested by the application developer. The configuration of ActionServlet and ActionMapping is done in the XML configuration file.
A dedicated application is used to handle the components of the request called an Action class. In the MVC structure, they represent models. They may be used to verify the legality of the user input. If an application error occurs during the information processing, the Action class can create an instance of an Error object, and then save the HTTP Request object. If the logic in the Action class is successfully passed to the Controller to deliver an ActionForward object, represent the JSP of the desired depiction response. There are two types of ActionForward: dedicated to a specific Action class or global forwarding (any Action class can transfer these ActionForwards to Controller).
The relationship between this main file:
The beginners often don't know how helloword.jsp, hellowordform.java, hellowordaction.java, how to associate together, in fact they are implemented by struts-config.xml configuration. These configurations are a difficult point in the Struts program design. Fortunately, in the Visual Struts of Java Web Studio, all of these configurations are automatically completed in the Struts file wizard.
[Key Source Code Analysis]
1 Enter the JSP page file
Entering the JSP page file name is Hello.jsp, which is mainly implemented with a form with a text box, and the file code is as follows:
<% @ Page ContentType = "Text / HTML; Charset = GB2312" Language = "Java"%> <% @ Taglib URI = "/ Web-INF / STRUTS-Bean.TLD" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>
hEAD>
html: form>
body>
html: html>
The first is to define this page as support Chinese display, Chinese encoding uses GB2312 format, program language to Java:
<% @ page contenttype = "text / html; charset = GB2312" Language = "java"%>
Then define four Struts tags beans, html, logic, and template, the labels that are not used in this file can remove the corresponding definition, if you need to use a custom label in the file, you also need to add the corresponding definition here. :
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>
html: form>
2 Output JSP page file
Output JSP page file name is HelloWordout.jsp, which is mainly implemented for the value of the text box submitted by the file, the file code is as follows:
<% @ page contenttype = "text / html; charset = GB2312" Language = "java"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
hEAD>
hEAD>
body>
html: html>
body>
html: html>
The output file is unique to the previous input file is to turn the
3 actionform bean file
The Hello program's ActionForm bean file implementation contains the bean that contains a variable. The file name is HelloWordform.java, the file code is as follows:
Package EmptyPRJ;
Import javax.servlet.http.httpservletRequest;
Import org.apache.struts.Action.Actionerror;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Public Final Class HelloWordForm Extends Actionform
{
Private string mybeanvariable1;
Public string getMyBeanvariable11 ()
{
Return (this.mybeanvariable1);
}
Public void setMyBeanvariable1 (String MyBeanvariable1)
{
THIS.MYBEANVARIABLE1 = MyBeanVariable1;
}
}
The first is to define the Package name:
Package EmptyPRJ;
This is the default package name EMPTYPRJ for JSPSTUDIO, and it is necessary to note that the package name corresponds to the directory name. Then introduced five types of HTTPSERVLETREQUEST, ActionError, ActionerRors, ActionForm, ActionMApping, which are commonly used in Actionform Beans, is automatically joined by jspstudio. In fact, the HelloWordForm class is only used for Actionform classes, and other parts can be deleted, but here keep it to facilitate Use it after the extension program.
Import javax.servlet.http.httpservletRequest; import org.apache.struts.Action.Actionerror;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Next, hellowordform, a parameter MyBeanVariable1 and its corresponding GET (), set () function, which is exactly the same as the normal bean defined.