Basic flow of Struts

2019-03-08 04:34发布

问题:

Well I want to study Struts so I am going to begin with Struts 1, I would like to know the general flow. What files are required?

Whats the function of struts-config.xml? validation.xml? validation-rules.xml When you visit your JSP page, and an action gets fired, what happens? What does the Action and Form class do? Which class is called first when an action gets fired.

I just downloaded a sample form, and all these files are confusing at first. I would like to know whats going on to get a better idea of Struts.

回答1:

You should start with a tutorial on Struts, that will make it easy to understand :D. You can find plenty on the web, especially for Struts 1. Here is a starting point for example.

But just for the sake of it, here is a high view presentation.

First you add the Struts ActionServlet into your web.xml file and you configure it to accept requests that match a certain mapping. For Struts this is *.do (you can have whatever you want for this, *.do is just a convention in the Struts community).

Now, whatever arrives on the server with such a *.do URL pattern is sent to the ActionServlet.

Now, the content of struts-config.xml file comes into play. The ActionServlet is a front controller which just dispatches to other more appropriate resources for specific processing. Those specific resources are the Action classes.

You can have for example a LoginAction that must process requests that arrive on the login.do path. In the struts-config.xml you specify this: everything that comes on the login path must be sent to LoginAction class.

And you can have as many declarations as you want, for path x call XAction, for y call YAction etc etc.

Normally your client submits data to the server, data that he inputs into a HTML form. This data you need to process in your Action class. Now enter ActionForm.

The ActionForm is a bean that the Struts framework fills with the data from the HTML form. Instead of doing request.getParameter("foo") you can work with objects like formBean.getFoo() for example.

Once you do your processing in the Action class using the ActionForm, you then must present the results in some view (by the way, Struts is a MVC framework so you must know a stuff or two about this also).

The views are normally JSP files. When you return from your Action, you specify a "forward" location by name (i.e. to what view to go). Now again the information is in the struts-config.xml file where the name of the view is mapped to a JSP location.

The framework will then forward control to that JSP, the JSP presents the data which is then sent to the client as HTML (the client will no longer need to access JSPs directly - they must be hidden by the Struts framework).

In the new page the client again performs some requests and the process repeats.

Well, that's about as high presentation as it can get. Off course there is more than this, but you will discover them while learning about Struts.

As for the validator-rules.xml and validation.xml, those are used by the Validator framework which you can integrate with Struts by the use of a plugin (Struts has plugins you can use to add new stuff to it) to also add validation to the user's input.

Well, that is about it. You can find plenty of tutorials on the web offering more details but hope helps you get a better start.

Good luck!



回答2:

  1. When a user submitted a jsp page. that page having (attribute of )action="login.do". the container will call to web.xml. in that web.xml there are two sections servlet And servlet mapping
  2. In servlet mapping it find *.do in the url-pattern. if it found to take the name of servlet. and check the corresponding class. in the servlet section. that class is ActionServlet.
  3. ActionServlet is the controller of Struts module architecture. in Action servlet having the service method. in that method we create RequestPrecessor class instance
  4. Service(req,res) RequestPrecessor rp = new RequestPrecessor();
  5. We call a process method of RequestProcessor class through the instance rp.process(req,res)
  6. In the request processor class have the process method with the parameter of req,res. then it has 1 if condition in this class. that condition return always true. because that is dummy method.

Inside that condition there are 6 steps are processing

  1. Create a action mapping instance in the Struts- Config.xml. it will keep all details of the action mapping path, value, type forward, validation=true/false, input ="*.jsp" etc these r created instance
  2. Then it will create Form class instance before it check the name of action mapping and form name are coincidence or not if it same it will create form instance
  3. Then it will go to ActionMapping instance the ris mention or not the validate =true/false if false it will not execute the this step else it will execute this step.
  4. Then it will create action instance
  5. Next it will take four parameters of execute Method it will return ActionErrors instance. if it is not empty. it will go to error page other wise it will got to corresponding page. else if it is empty if will go further and display corresponding value of page in jsp view.This is struts flow.