10 struts2 java study notes

2010-10-28  来源:本站原创  分类:Java  人气:150 

Recently I have spare time Java Virtual Machine Research, the intern assigned to the project team inside, do not want it used to be free, and managed to find time to continue the topic post. I intend to end some of J2ee and then talk about the JVM and JavaScript, as long as I have summed up the latest study notes, will be used in a timely manner and to share. Java sincerely hope that the relationship with the love of my colleagues in the common progress of a large ... ...

The first, prepared to continue to talk about the topic last Struts-2, a brief review of a history of men: over time, Web application framework of constantly changing demand, resulting in a number of the next generation of Struts solution. Struts Ti which continue to adhere to MVC model based on the improvement, continue the successful experience of Struts. WebWork project was released in March 2002, and it has revolutionized the Struts framework to improve style, the introduction of many new ideas, concepts and features, but the Struts and the original code is not compatible. WebWork is a mature framework, after several major improvements and release. In December 2005, WebWork and Struts Ti decision merged, then the same time, Struts Ti renamed to Struts Action Framework 2.0, as the true next generation Struts.

See Struts-2 of the process flow:

1) Browser generates a request and submit the framework to deal with: According to decide which interceptor configuration, action classes and results.

2) request through a series of interceptors: According to the request interceptor to do the different levels of different treatments. This Struts-1 in the RequestProcessor class is very similar.

3) Call Action: action to generate a new instance, call the business logic methods.

4) call to produce results: match result class and call the generated instance.

5) Request to return again after a series of interceptors: the process can also be configured to reduce the number of interceptors

6) The request returns the user: to return from the control servlet, build Html.

Here it is clear that the scope does not exist FormBean package, directly obtained from the Action data. There is a Strut-2 configuration web.xml file:


<filter-name> controller </ filter-name>

<filter-class> org.apache.struts.action2.dispatcher.FilterDispatcher </ filter-class>

</ Filter>


<filter-name> cotroller </ filter-name>

<url-pattern> / * </ url-pattern>

</ Filter-mapping>

Noting the previous servlet into a filter, ActionServlet into FilterDispatcher, *. do to become a / *. filter configuration defines the name (for association) and the filter class. filter mapping to match the success of the request URI to call the filter. By default, the extension ". Action". This file is in default.properties the "struts.action.extension" attribute defined.

default.properties attribute definition file, the path through the classpath in the project contains a file named "struts.properties" file to set different property values. The Struts-2 in the default configuration file name struts.xml. 1 and 2 of the action because the extension was. Do and. Action, so it can co-exist side. We look at the action of a Struts-2 code:

public class MyAction {

public String execute () throws Exception {

/ / Do the work

return "success";



Obvious difference is no longer inherit any classes and interfaces, the return of just a String, without parameters. In fact, any Struts-2 free parameters to return String methods can be configured to call the action. Get all the parameters come from it? The answer is Inversion of Control Technology (Inversion of Control). The author as the most popular way to explain, we first tried to get the Action request object, which can extract any parameters submitted the page. Then we set the request of a member variable, then it needs a set method. As most of the action needs to do this, we set this method as interfaces.

public interface ServletRequestAware {

public void setServletRequest (HttpServletRequest request);


public class MyAction implements ServletRequestAware {

private HttpServletRequest request;

public void setServletRequest (HttpServletRequest request) {

this.request = request;


public String execute () throws Exception {

/ / Do the work directly using the request

return Action.SUCCESS;



Who will call the set method? Who will control the action that the behavior of the past, we are in the appropriate place to write their own on a action.setServletRequest (...), that is, control of the programmer here. However, the idea of Inversion of Control where calls to the container to determine the running, as long as the use of Java reflection to get Method object and call its invoke method of passing arguments can do so from the programmer control here transferred to the container side. Many programmers can reduce the tedious work of more focus on business logic. Request can be injected into action, the other can also be any object. Action of the member variables in order to ensure thread safety, Struts-2's action is not a single case, each new request will have a new action instance.

So one may ask, in the end who will do the object into the work? The answer is interceptors. Interceptor is what? I again try to explain the popular concept of interceptors. We have to understand the words of the interceptor, first we must understand the kinds of design patterns in GOF23 Proxy mode.

A object to calling f (), it is hoped that agent to do B, then B must be A reference to an object, then B's f () is called by A object reference object A f () method, and ultimately achieve A of f () is called purpose. Will anyone think this is very troublesome, why obviously as long as Af () can be completed must be packaged to B, f () method go? What are the benefits?

1) Here we have only one A, when we have a number of A, when B only need to monitor an object f () method can control all from the overall situation is called f () method.

2) Also, since an agent B to get A reference to an object, then B can determine the object in the real tune A f () method which can be done before the pre-work and get it done back to the previous post which may have to do the work.

This point, we see that the concept of interceptors that yet? It intercepts the next tone f () method of the request, and then make a unified treatment (can be different for each approach, analytical object A can be identified), processed and then released. This look like a cross-section of the river flows a knife, all the water molecules want to be searched and then released? This is the AOP (Aspect of Programming Aspect Oriented Programming) ideas.

Anyway, Struts-2 is the use of the AOP and IoC technology to reduce the coupling between action and framework, trying to maximize the re-action purposes. In such a technology motivated by, Struts-2's action by the framework into a simple to use POJO (Plain Old Java Object) nothing more. Practical thoughts on AOP and IoC has been out all over each new frame, they are not how new technologies are also using the JDK already can do the most to them is more representative of programming to an interface to improve reuse, increase the extension of an idea. Struts-2 is only part of the design using both thought to be completed, and the other is the fire in the framework of the recent Spring, to a greater extent represent the two design ideas, I will be the next one to further explore the structure of Spring.

PS: on the Struts-2 did not really how I used, here are some of the older generation saw the post after the online write your own learning experience, inadequate please forgive me!

  • 10 struts2 java study notes 2010-10-28

    Recently I have spare time Java Virtual Machine Research, the intern assigned to the project team inside, do not want it used to be free, and managed to find time to continue the topic post. I intend to end some of J2ee and then talk about the JVM an

  • Struts2.1 Study Notes 2010-09-02

    Struts2.1 study notes. . . Integrated network of other people's notes

  • Java Learning small mind (10) sees the study notes (2) 2010-08-09

    Notes I wrote Battle City version is mainly lug important to write about today is a very important to write the version is 0.7 and 0.8 versions, this version is mainly to achieve an object-oriented way of thinking, Mashi Bing teacher an example is im

  • Effective Java study notes carefully Article 10 rewrite clone --- 2011-04-21

    To be an object Clone, then to achieve the Clone () interface, this interface does not define any way, but if you do not implement this interface, then call the clone method, when there will be CloneNotSupportedException, this is the type of interfac

  • Struts2.0 Study Notes (1) name space and. Xml file configuration 2010-02-07

    First of all to say sorry, thank the concerns that I blog friends. Near future because of the change do the work, because continuous learning, continuous progress in order to make their own to find a better job. Therefore, during this period did not

  • design patterns java study notes 2010-05-10

    Today take some time to learn the system under design mode, from the Internet to find a point of information and the learning experience of predecessors, 1: Learning goals and methods of design patterns (master's recommendation summary references) De

  • java study notes 7 interfaces & components. container 2010-10-28

    Finally they calm down and continue to write the sequel to the topic, the first six principal told some J2se experiences and feelings, now J2ee Java application has been occupied a considerable area, and some even claim that Java has been replaced by

  • java study notes 8 Servlet / Jsp 2010-10-28

    J2ee finally officially entered the details of the, of course, bear the brunt of the Servlet and Jsp, J2ee on the articles have mentioned only a specification and guidelines, define a set of interfaces must be followed, the core concept of components

  • java study notes 12 ORM 2010-10-28

    Java Zatan this is the last one, and to talk about ending ORM framework, but also be considered to be the last one to cover J2ee to, summed up the reason why so late ORM this regard, the first author is a busy two weeks, and the other On the one hand

  • Struts2 integration study notes Groovy - Groovy class as Action 2011-01-04

    Note that: As a recent study of things related to Groovy, Groovy and Struts2 wanted to have an integrated solution, a search online, and sure enough there is a plug-in, the class can write directly as Groovy Struts in Action. However, the plug-in dep

  • thinking in java study notes (b) 2011-08-20

    Chapter <br /> Shenma main types, but also difficult listening, I go to Le, called basic types do not you? Where the existence of this object, or the jvm which is more realistic, here about the stack, non-constant static ram even register that the c

  • apache commons-lang-2.3 StringUtils.java Study Notes (1) 2010-03-29

    StringUtils in the method of operation of the object is an object of type java.lang.String, is provided by the String type of JDK operation are complementary, and is null safe (that is, if an input parameter String is null does not throw NullPointerE

  • Java Study Notes (1) - Object-Oriented 2009-01-03

    Next from the Internet, a Java video courseware, the following content is based on what they hear and do the course notes. 1, object-oriented - is a kind of thinking, a problem-solving model. In other words, whether we encounter any problems, all nee

  • Java study notes - at the top of the words 2010-05-04

    Note: Please indicate the source is reproduced. Select a language is to choose a culture to accept a philosophy. I personally do not think so soon before the start learning java, a lot of things by chance, in fact, inevitable. Learning java for two r

  • java study notes oracle interview 2010-07-16

    Current user who created the table, view, and so on. This table is called the user dictionary table select table_name from user_tables; select view_name from user_views; select constraint_name, table_name from user_constraint; declare variable block

  • java study notes an interview subject 2010-07-17

    JAVA side 1 What are the characteristics of object-oriented aspects of 2 String is the most basic data type? 3 int and Integer What is the difference 4 String and StringBuffer difference 5 run-time exception and the similarities and differences betwe

  • Effective java study notes 2010-08-01

    1. Static factory method instead of constructor Such as: public static Boolean valueOf (boolean) ( return (b? Boolean.TRUE: Boolean.FALSE); ) A class provides a static factory method is good a. easy to read, the return type explicitly b. call does no

  • Java study notes --- 16. 11-Java object-oriented programming Polymorphism, instanceof, and Object Class 2010-08-20

    Polymorphism is a confused concept until, by the notes on the concept and application of polymorphism have a more comprehensive understanding of the instanceof keyword, and also learn the characteristics of Object class. 1. Java polymorphism in class

  • java study notes 9 Struts 2010-10-28

    J2ee many open source framework, I can only introduce a few familiar, the other in the Chinese IT industry, the current application was not a lot. Hope that the new framework should not blind a respected, first of all must be familiar with it than th

  • Struts2.0 Study Notes (1) 2010-12-02

    One. Struts2 project developed the basic steps 1. Struts into the core support package 2. FilterDispatcher configured in the web.xml 3. Development Dao 4. Development of action 5. Prepare front display page 6. Struts.xml profile created II. Struts2 p