[JAVA] Roughly Struts2

Let's take a quick look at the Struts2 interceptor

It's been about a year and a half since I entered the programming industry. Of course, there are many things I don't know yet. Most things start with "What's that ..." and "What's this ...". It's good to have a lot to learn, but since there is almost no output other than at work, I will write an article after practicing.

This time, I will summarize Struts2, which is a framework used in the field, although it is also a knowledge organization. Qiita already has a lot of good articles by other authors, so I will post them with citations.

agenda

・ What is struts2? ・ MVC model in struts2 ・ Interceptor

What is Struts2

One of the web application frameworks. Generally, Struts means "Ver1.x series", and Struts2 means "Ver2.x series". Struts2 was originally the result of the name change of the framework "WebWork2", and it is different from Struts1 from the design background (I did not know).

MVC model in struts2

What is an MVC model in the first place?

Abbreviation for M (Model) V (view) C (Controller), which is a coding model that is implemented separately by function. Roughly.

In Struts2

struts_2_architecture.gif (Fig. 1)

Model: Represents the Action class and holds and operates input data. In addition to the Action class, the Business class, Model, and Entity class are also included in this. View: JSP. Struts has its own tag library that starts with "S: ~". (Example: form tag with s: form) Contoroller: A typical one is the Interceptor class. Pass the input data from the client (user) to Model.

Interceptor

What is Interceptor?

□ English meaning Interceptor: stealers, blockers, interceptors

Processing can be inserted before and after processing by the main Action class of Model based on the interceptor settings. struts_2_interceptor.gif (Fig. 2)

In other words, common processing such as validation and token check can be performed before receiving data from the client and passing it to the Action class.

The interceptor is defined in struts.xml.

<struts>
    <package name="hoge-package" extends="json-default" abstract="true">
<!--Interceptor definition-->
        <interceptors>
<!--Interceptor class definition-->
            <interceptor name="sample" class="sample.sample"> 
            <interceptor name="hoge" class="hogehoge.sample"> 
        </interceptor>
<!--Interceptor stack definition-->
            <interceptor-stack name="samplestack1">
                <interceptor-ref name="xxx" />
                <interceptor-ref name="yyy" />
            <!--As mentioned above, multiple interceptors can be built together-->
            </interceptor-stack>
        </interceptors>
    </package>
</struts>

It is also possible to separate the xml file for each function, in which case you can include other files in struts.xml.

<!--Include-->
<include file="sampleA.xml">
<include file="sampleB.xml">

In addition, this interceptor can be implemented not only in class units but also in method units. ・ AbstractInterceptor (class unit) -MethodFilterInterceptor (method unit)

You can specify the method name in the \ * part.

<interceptor-ref name="samplestack1" />
<!--Interceptor name if you want to enable the interceptor.includeMetohds -->
    <param name="xxx.includeMethods">*</param>
<!--Interceptor name to disable the interceptor.excludeMetohds -->
    <param name="yyy.excludeMethods">*</param>
</interceptor-ref>
References

[About Struts version] http://write-remember.com/archives/4983/ [[For super beginners] Struts2 Super Primer-2018 Edition] https://qiita.com/tarosa0001/items/889faa2ab5853005f26b → Starting with Struts, the benefits of introduction and the procedure for building an environment are summarized in an easy-to-understand manner. [Struts 2-Architecture] * Citation of Fig. 1 https://www.tutorialspoint.com/struts_2/struts_architecture.htm → Explanation of MVC model in Struts [About MVC model] https://qiita.com/s_emoto/items/975cc38a3e0de462966a → It is an MVC explanation article by another author [Interceptor] https://qiita.com/alpha_pz/items/f155b1ba1b5de4bd0a26 → This article focuses on the interceptor of Struts2. [Introduction to Struts 2 (4) -Mechanism called interceptor-] * Citation of Fig. 2 https://codezine.jp/article/detail/3264

Recommended Posts

Roughly Struts2
Drafting Struts2
Roughly try Java 9
Learn Docker roughly