Start by building a Struts2 development environment

  • 2020-04-01 01:40:01
  • OfStack

Struts 2

Struts2 is based on WebWork2. Like struts1,   Struts2 also belongs to the MVC framework. One thing to note, though, is that although Struts2 and struts1 are not very different in name, they are almost different in coding style. So if we have struts1, why do we have struts2? Mainly because struts2 has the following advantages:

1   >   Struts2 does not follow Servlet  as struts1 does in software design; API and struts  The API is tightly coupled, and Struts2 applications can be servlet&server independent; API and struts  API.   This design of Struts2 is non-invasive, while Struts1 is invasive.

Public  Class  OrderListAction  Extends  Action  {

Public  ActionForward  Execute (ActionMapping  The mapping,   ActionForm  The form,

HttpServletRequest  Request,   HttpServletResponse  The response)

Throws  Exception  {

}

}

2 >   Struts2 provides interceptors, which can be used for AOP programming, such as authorization interception and other functions.

3 >   Struts 2 provides a type converter that converts special request parameters to the desired type. In Struts1, if we want to achieve the same functionality, we must register the type converter with the underlying implementation of Struts1, BeanUtil.

4 >   Struts2 provides support for a variety of presentation layer technologies, such as JSP, freeMarker, Velocity, and more

5 >   The input validation of Struts2 can verify the specified method, which can solve the pain of Struts1 for a long time.

6 >   An internationalized resource file management implementation for global scope, package scope, and Action scope is provided

Set up Struts2 development environment

1. Find the jar files needed to develop Struts2 applications

Develop the least required jar files for Struts2

Struts-core-2. X.X.J: Struts2 framework core class library

Xwork-2. X.x.jar: xwork class library on which Struts2 is built

Ognl-2.6.jar: image navigation language (object & cake; Graph  Navigation  The Struts2 framework reads and writes properties of objects through it

Freemarker-2.3.x.js: UI tag template for Struts2 is written using freemarker

Commons-logging-1.1.x.ar: logging package produced by ASF, which is used by Struts2 framework to support logging of Log4J and JDK1.4 +

Commons -filteupload-1.2.1.jar file upload component, after version 2.1.6 must be added to the file

2. Write the Struts2 configuration file

Struts2's default configuration file is struts.xml, which needs to be stored under web-inf /classes. The configuration template of this file is as follows:

< ? Xml  Version = "1.0"   Encoding = "utf-8"? >

< ! DOCTYPE  Struts  PUBLIC " . / / Apatche  Software  Foundation / / DTD  Struts  Configuration  2.0 / / EN " " http://struts.apatche.org/dtds/struts-2.0.dtd ">

< Struts>

< / struts>

3. Add Struts2  to web. XML; The MVC framework starts the configuration

In struts s1.x, the struts framework is started through a Servlet. In struts2, the struts framework is started through a Filter. His configuration in web.xml is as follows:

< Filter>

< The filter - name> Struts 2 < / filter - name>

< The filter - class> Org. Apatche. Struts 2. Dispatcher. Ng. Filter. StrutsPrepareAndExecuteFilter< / filter - class>

< ! -   Since Struts  After 2.1.3, the FilterDispatcher below has been flagged as out of date

        < The filter - class> Org, apache struts 2. Dispatcher. FilterDispatcher< / filter - class>   - >  

< / filter>

< The filter = mapping>

< The filter - name> Struts 2 < / filter - name>

< Url - pattern> / * < Url - pattern>

< / filter - mapping>

In StrutsPerpareAndExecuteFilter init () method will read the classpath to the default configuration file struts. The XML to complete initialization.

Package introduction in struts.xml configuration

< Package  Name = "itcast"   The namespace = "/ test"   Extends = "struts - the defult" >

< Action  Name = "helloworld"   Class = "cn. Itcast. Action. HelloWorldAction"   Method = "execute" >

< Result  Name = "success" > / WEB - INF/page/hello.html jsp< / result>

< / action>

< / package>

Find the jar file it needs to develop     To be extracted :struts-2.3.8-all.gz

Or struts-2.3.8-apps.gz find struts-2.3.8-all\struts-2.3.8\ apps&sp; The following documents are as follows:

  < img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/2013041117194734.png ">
Provides a blank item struts2-blank& blank; We can then develop struts2 on this project.

If additional jar files are needed during development   How to do?

    Need to \ struts - 2.3.8 -all \ struts - 2.3.8 \ lib  Find the appropriate jar file in.

Struts 2 - blank. War project   How to uncompress     -     Copy to webapps in tomcat   Just start tomcat

The apache tomcat \ bin  Find     Startup.bat click start

Observation:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/2013041117194735.png ">

You can open the browser IE

      Input     http://localhost:8080/struts2-blank

Find the unzipped jar file   : apache tomcat \ webapps \ struts 2 - blank \ WEB - INF \ lib

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/2013041117194736.png ">

Bug:

1, java.net.BindException:  Address  Already  In  Use:   JVM_Bind

          Analysis:     The tomcat you use is picked up a second time   Or n times   Start         Never closed before   Cause a bug

Solutions:

      Close   Tomcat can be started.

HTTP  Status  404   -   / demo/servlet/demo. Do

            Analysis: couldn't find what you requested   The url is: servlet/demo) do

              Solutions:

                          Does your url exist?     If it doesn't exist   Add by business.              

3,

There  Is  No  Action  Mappend  For  Action  Name  The demo.

  Analysis: the corresponding demo action cannot be found on the server side

The Demo. The action

  CSDN/demo. The action

Only prompt   : not found in the demo

/ CSDN not found below   Demo  Action 

Bug 

No  Result  Defined  For  Action  Com. Opensymphony. Xwork2. ActionSupport  And  Result  success


Related articles: