Struts Best Practice

xiaoxiao2021-03-06  38

background:

From the present issue, everyone has seen the entire history of Web development.

Struts experienced a long spring and autumn since June 1.0 Release, with a huge user group, and IBM, Tomcat Web console has Struts framework ..

There are many options for web frameworks, do not have to use Struts, but if you are just starting to use, I hope some of the practices below can bring you help, just this, so, please forgive, this article does not have any instance code, Because my friend Leemassn told me this morning, this thing has been rotten: (, so I believe that you know more than me, I don't involve some advanced usage, I will all said below. Belongs to the paper.

1. Correctly oriented, is the real purpose of Web Framework.

From time to time because Struts ear has more, we must use struts, and from time to time, MVC must use Struts, when you can flexibly 游 刃 between StrutsActionform, Action, Lib, " It is the truthfulness of our use of the framework when you can expand your web framework.

Let me say some topics that have nothing to do with Struts, because Struts can't replace all your application framework, regardless of the big or small, complex or simple

some advices:

1. Use support for a wide range of JDO or Hibernate, or iBates to do your data persistence.

2. Please join the Struts-EL tab,

3. Please use the application server's JNDI connection database

4. Please use StrutstestCase to do unit testing

5. Read the Sun's naming specification.

1. Struts object use

The Struts version has been changed much, if you just use the most basic form, Action believes that 0.5 is enough, but everything is 1.1.

Every write application uses inherited baseAction, and baseform, of course, BaseAction is best to inherit LookupDispatchaction, Baseform is best to inherit the ValidatorActionform (these two are starting from 1.1)

ValidatorActionform also inherited Actionform, because 1.1 has Plugin, when you use ValidatorPlugin to make your Formbean have verification capabilities.

(1.1 also DynaActionform, I believe everyone knows this bean, but it seems to be in addition to letting you write some ActionForm, unless you want to completely not give ActionForm, you can use the ACTIONFORM. DynaActionform, it seems that Struts seems to have no more information to your Actionform, so I want to stay, we all know that Actionform can't fully act as Model (Model should be your lasting layer), then we can at least use it to pass the value. Convenient spending is not required to get GET, then set it. So, we need to use ActionForm, and we also need to check for user-end for each property, so we choose ValidatorActionform.)

LOOKUPDISPATCHACTION inherits Dispatchaction, plus multiple methods, get productMethod (), localmap, which can help you find your Add method from your local Submit resource file, such as Submite's name. You can find your Add method.

2. Struts label library.

I believe that struts is very particular

The label library is mainly used to do View, so when designing Actionform, it is very boldly designed, don't worry that they can't display on the JSP page, it is the Data type, or use List, Map, such conformity Type, or reference object type, because they can make it easy to display on the page If Test1form contains a variety of model objects,

TEST1FORM {

Student Student // has ID, Name and other properties

TEACH TEACH / / Id, ClassID and other attributes

List class // A set of classes objects Classes also contains ClassID and other attributes

Test2form test2form --- // has ATTR2, etc. Property

....

}

Then

Seeing, they can get anything, play your imagination label library to show your object attribute values.

Remember to initialize it in the Actionform RESET and get an object in turn. This way you can also submit all of these contents to the ActionServlet. If you haven't used it, try it.

Try to use the EL tag: Mainly used for complex logic judgment,

For example Determines if A is existing, but you want to know if A is equal to 1, how to take it, change to EL, it is ....

Add some: For the front desk, the Struts label has to be said that it seems to have become a chicken rib, beautiful, easy to operate the interface, often in other technologies, the biggest purpose of Struts label is also convenient for convenience Data shows that beautiful complex operations still depends on your HTML and JavaScript.

3. Abnormal processing of Struts.

Basically, Struts provides a relatively complete framework for exception handling.

By reasoning, when a system is officially delivered, we also let users see that a large English letter is an extremely rude behavior, so we have set up from both.

1. Use the validationform to verify confidity, of course, you can also use you to accumulate more javascript, no matter that way, you will benefit you, just learn to use regular expression. 2.Action processing, in Try Catch uses SaveMessage to use SaveErrorS () or SaveMessaes, because (Struts1.2 starts abandoning ActionerRors, all is ActionMessages)

Here, I have to say something on some architecture. The exception handled in Struts Action is not your original anomaly. To throw DataException in your DAO layer, and in your business layer, you need to capture To, as your custom exception, you have been friendly in the struts, and it makes sense.

We all know that DAO's layer is a unified anomalies such as DataExpcetion.

Then go to your business to capture the service, you can capture the DataExpcetion because the servce layer, you are always what you want to do, so it is recommended to capture the DetaException after the service layer, then throw business logic abnormal

For example, in business processing, add a student process, if

Public void doaddstudent (student student) throws dupkeyexption, ser {

Try {

IF (student.getid ()). GetId () == student.getId ()) {

Throw new dupkeyexcption ("change students already exist");

}

StudentDao.Add ();

} catch (dataException e) {

Logger.debug ("Error Message:", E);

Throws New StudEntexception ("Add Fail");

}

}

Try {

Service.doaddstudent ();

} catch (dupikeException E1) {

// Add it repeatedly

Errors.Add ("Message", New Actionerror ("Student.Error.dupKey));

} catch (exception e) {

//add failed

Errors.Add ("Message", New Actionerror ("student.error.Add")));

Logger.error (E.GetMessage ());

E.PrintStackTrace ();

}

SaveerRors (Request, Errors);

As for the display, use directly. But you have to feel that there is no blank place, or you can use it.