Struts of application framework based on MVC pattern

Source: Internet
Author: User

Struts is the framework based on the MVC pattern!

(Struts is also a servlet package to improve development efficiency!) )

Struts Development steps:

1. Web project, introducing Struts-jar Package

2. The core functionality of struts is introduced in Web. xml

Configure filters

3. Develop action

4. Configure the action

Src/struts.xml

1. Introduction of 8 jar files

2. Web. xml

<!-- introduction struts core filter -->

<FILTER>

<FILTER-NAME>STRUTS2</FILTER-NAME>

<filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</ Filter-class>

</FILTER>

<FILTER-MAPPING>

<FILTER-NAME>STRUTS2</FILTER-NAME>

<URL-PATTERN>/*</URL-PATTERN>

</FILTER-MAPPING>

3. Develop Action

Develop action: Process requests

Public class helloaction extends Actionsupport {

Processing Requests

Public String Execute () throws Exception {

System. out. println (" access to action, processing request ");

System. out. println (" Call Service");

return "Success";

}

}

4. Configure action:src/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<! DOCTYPE Struts Public

"-//apache software foundation//dtd Struts Configuration 2.0//en"

"Http://struts.apache.org/dtds/struts-2.0.dtd" >

<struts>

<package name= "xxxx" extends="Struts-default">

<action name="Hello" class="cn.itcast.action.HelloAction" method="Execute" >

<result name="Success">/success.jsp</result>

</action>

</package>

</struts>

Struts2 Development process:

Version: 2.3

    • Introducing jar files

Commons-fileupload-1.2.2.jar "File Upload related package"

Commons-io-2.0.1.jar

Struts2-core-2.3.4.1.jar "struts2 Core Feature Pack"

Xwork-core-2.3.4.1.jar "xwork Core Pack"

Ognl-3.0.5.jar "ognl expression feature support table"

Commons-lang3-3.1.jar "struts extension to java.lang Package"

Freemarker-2.3.19.jar "struts tag Template Library jar file"

Javassist-3.11.0.ga.jar "struts -to-bytecode processing related jar"

    • Configure Web. xml

Tomcat boot -à load itself Web. Xml---à load all Project Web. XML

By Introducing Filters in the project's web. XML,

initialization of the core function of the-àstruts, done through the filter

Àfilter "

init/ Start Execution

dofilter/ Access Execution

Destroy

<!-- Introducing struts core Filters --

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

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

</filter-mapping>

Struts2-core-2.3.4.1.jar

Strutsprepareandexecutefilter is the core filter

Attention:

The core filter class is different from the version of struts that you use!

    • Development Action

Attention:

1. Action class, also called Action class ; General Inheritance Actionsupport class

the class that processes the request (The action class in struts supersedes the previous servlet)

2. business methods in action, handling specific requests

-à must return a String

À method cannot have parameters

Public class helloaction extends Actionsupport {

Processing Requests

Public String Execute () throws Exception {}

}

  • Configure Struts.xml
  • Struts2 Execution Process

    Server Startup:

    1. Load Project Web. XML

    2. Create a Struts Core Filter object, execute filter àinit ()

    Struts-default.xml, initialization of core functions

    Struts-plugin.xml, struts related plugins

    Struts.xml User-written configuration files

    Access:

    3. user access Action, the server based on the access path name, find the corresponding aciton configuration , Create action Object

    4. Execute the four interceptors defined in the default interceptor stack

    5. Business Processing Method for executing action

    Struts-default.xml, detailed

    Catalog:struts2-core-2.3.4.1.jar/struts-default.xml

    content:

    1. Bean node Specifies the type of object that struts creates at run time

    2. Specify struts-default Package "user-written packages (struts.xml) to inherit this package"

    The package Struts-default defines:

    A. the result type of the jump

    Dispatcher forwarding, no default forwarding is specified

    REDIRECT redirection

    Redirectaction redirect to action resource

    Stream ( used when the file is downloaded )

    B. all interceptors are defined

    defines a total of three interceptors!

    for interceptor Reference convenience, you can refer to the interceptor by defining the stack,

    if the stack is referenced at this point, the interceptor in the stack will be referenced !

    Defaultstack

    The default stack, which defines the four interceptors to be executed by default !

    C. the default execution of the interceptor stack, the default action

    <default-interceptor-ref name= "Defaultstack"/>

    <default-class-ref class= "Com.opensymphony.xwork2.ActionSupport"/>

    <interceptor

    Name= "Prepare" class= "Com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

    <interceptor

    Name= "params" class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

Interceptor (Sneak Peek):

The Interceptor function is similar to the filter function.

Difference:

Common denominator: Intercept Resources!

Difference:

filters, interceptors all resources are available; (/INDEX.JSP/SERVLET/IMG/CSS/JS)

Interceptor to intercept only the action request.

interceptors are the concept of struts and can only be used in struts .

filters are The concept of servlets that can be used in struts projects andservlet projects.

interview Question: When does the Interceptor execute? ( access / start ) executes the action class creation first, Execute the Interceptor first?

---" 1. execute sequentially when user accesses - an interceptor;

---" 2. Execute First Action class is created, the interceptor is executed, the last interceptor executes, and the business method is executed.

Struts of application framework based on MVC pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.