카테고리 보관물: Apache Project/Struts 2.x

Struts 1.0 Vs Struts 2.0

아래 내용은 http://www.javabeat.net/tips/139-struts-10-vs-struts-20.html의 내용입니다.

Difference between Struts 1.0 and Struts 2.0

In the following section, we are going to compare the various features between the two frameworks. Struts 2.0 is very simple as compared to struts 1.0,1.1, few of its excelent features are:

1.Servlet Dependency

Actions in Struts1 have dependencies on the servlet API since the
HttpServletRequest and HttpServletResponse objects are passed to the
execute method when an Action is invoked but in case of Struts 2.0, Actions are not container dependent because they are made simple POJOs. In Struts 2.0, the servlet contexts are represented as simple Maps which allows actions to be tested in isolation. Struts 2.0
Actions can access the original request and response, if required.
However, other architectural elements reduce or eliminate the need to
access the HttpServetRequest or HttpServletResponse directly.

2.Action classes

Programming the abstract classes instead of interfaces is one of
design issues of struts 1.0 framework that has been resolved in the Struts 2.0
framework. Struts 1.0 Action classes needs to extend framework
dependent abstract base class. But in case of Struts 2.0 Action class
may or may not implement interfaces to enable optional and custom
services. In case of Struts 2.0 , Actions are not container dependent because they are made simple POJOs. Struts 2.0
provides a base ActionSupport class to implement commonly used
interfaces. Albeit, the Action interface is not required. Any POJO
object with an execute signature can be used as an Struts 2.0 Action object.

3.Validation

Struts 1.0 and Struts 2.0 both supports the manual
validation via a validate method.
Struts 1.0 uses validate method on the ActionForm, or validates through
an extension to the Commons Validator. However, Struts 2.0 supports
manual validation via the validate method and the XWork Validation
framework. The Xwork Validation Framework supports chaining validation
into sub-properties using the validations defined for the properties
class type and the validation context.

4.Threading Model

In Struts1, Action resources must be thread-safe or synchronized. So
Actions are singletons and thread-safe, there should only be one
instance of a class to handle all requests for that Action. The
singleton strategy places restrictions on what can be done with Struts
1.0 Actions and requires extra care to develop. However in case of Struts 2.0,
Action objects are instantiated for each request, so there are no
thread-safety issues. (In practice, servlet containers generate many
throw-away objects per request, and one more object does not impose a
performance penalty or impact garbage collection.)

5.Testability

Testing Struts 1.0 applications are a bit complex. A major hurdle to
test Struts 1.0 Actions is that the execute method because it exposes
the Servlet API. A third-party extension, Struts TestCase, offers a set
of mock object for Struts1. But the Struts 2.0
Actions can be tested by instantiating the Action, setting properties
and invoking methods. Dependency Injection support also makes testing
simpler. Actions in struts2 are simple POJOs and are framework
independent, hence testability is quite easy in struts2.

6.Harvesting Input

Struts 1.0 uses an ActionForm object to capture input. And all
ActionForms needs to extend a framework dependent base class. JavaBeans
cannot be used as ActionForms, so the developers have to create
redundant classes to capture input. However Struts 2.0 uses Action properties (as input
properties independent of underlying framework) that eliminates the
need for a second input object, hence reduces redundancy. Additionally
in Struts 2.0, Action properties can be accessed from the web page via the taglibs. Struts 2.0
also supports the ActionForm pattern, as well as POJO form objects and
POJO Actions. Even rich object types, including business or domain
objects, can be used as input/output objects.

7.Expression Language

Struts 1.0 integrates with JSTL, so it uses the JSTL-EL. The struts1 EL has basic object graph traversal, but relatively weak collection and indexed property support. Struts 2.0
can also use JSTL, however it supports a more powerful and flexible
expression language called “Object Graph Notation Language” (OGNL).

8.Binding values into views

In the view section, Struts1 uses the standard JSP mechanism to bind
objects (processed from the model section) into the page context to
access. However Struts 2.0
uses a “ValueStack” technology so that the taglibs can access values
without coupling your view to the object type it is rendering. The
ValueStack strategy allows the reuse of views across a range of types
which may have the same property name but different property types.

9.Type Conversion

Usually, Struts 1.0 ActionForm properties are all Strings. Struts
1.0 uses Commons-Beanutils for type conversion. These type converters
are per-class and not configurable per instance. However Struts 2.0 uses OGNL for type conversion. The framework includes converters for basic and common object types and primitives.

10.Control Of Action Execution

Struts 1.0 supports separate Request Processor (lifecycles) for each
module, but all the Actions in a module must share the same lifecycle.
However Struts 2.0
supports creating different lifecycles on a per Action basis via
Interceptor Stacks. Custom stacks can be created and used with
different Actions as needed.

 

struts2 dtd 파일입니다.

struts2 dtd file

<?xml version=”1.0″ encoding=”UTF-8″?>
<!–
/*
 *
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * “License”); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
–>

<!– START SNIPPET: strutsDtd –>

<!–
   Struts configuration DTD.
   Use the following DOCTYPE
   
   <!DOCTYPE struts PUBLIC
    “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
    “http://struts.apache.org/dtds/struts-2.0.dtd”>
–>

<!ELEMENT struts (package|include|bean|constant)*>

<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
<!ATTLIST package
    name CDATA #REQUIRED
    extends CDATA #IMPLIED
    namespace CDATA #IMPLIED
    abstract CDATA #IMPLIED
    externalReferenceResolver NMTOKEN #IMPLIED
>

<!ELEMENT result-types (result-type+)>

<!ELEMENT result-type (param*)>
<!ATTLIST result-type
    name CDATA #REQUIRED
    class CDATA #REQUIRED
    default (true|false) “false”
>

<!ELEMENT interceptors (interceptor|interceptor-stack)+>

<!ELEMENT interceptor (param*)>
<!ATTLIST interceptor
    name CDATA #REQUIRED
    class CDATA #REQUIRED
>

<!ELEMENT interceptor-stack (interceptor-ref*)>
<!ATTLIST interceptor-stack
    name CDATA #REQUIRED
>

<!ELEMENT interceptor-ref (param*)>
<!ATTLIST interceptor-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-interceptor-ref (#PCDATA)>
<!ATTLIST default-interceptor-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-action-ref (#PCDATA)>
<!ATTLIST default-action-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-class-ref (#PCDATA)>
<!ATTLIST default-class-ref
    class CDATA #REQUIRED
>

<!ELEMENT global-results (result+)>

<!ELEMENT global-exception-mappings (exception-mapping+)>

<!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
<!ATTLIST action
    name CDATA #REQUIRED
    class CDATA #IMPLIED
    method CDATA #IMPLIED
    converter CDATA #IMPLIED
>

<!ELEMENT param (#PCDATA)>
<!ATTLIST param
    name CDATA #REQUIRED
>

<!ELEMENT result (#PCDATA|param)*>
<!ATTLIST result
    name CDATA #IMPLIED
    type CDATA #IMPLIED
>

<!ELEMENT exception-mapping (#PCDATA|param)*>
<!ATTLIST exception-mapping
    name CDATA #IMPLIED
    exception CDATA #REQUIRED
    result CDATA #REQUIRED
>

<!ELEMENT include (#PCDATA)>
<!ATTLIST include
    file CDATA #REQUIRED
>

<!ELEMENT bean (#PCDATA)>
<!ATTLIST bean
    type CDATA #IMPLIED
    name CDATA #IMPLIED
    class CDATA #REQUIRED
    scope CDATA #IMPLIED
    static CDATA #IMPLIED
    optional CDATA #IMPLIED
>

<!ELEMENT constant (#PCDATA)>
<!ATTLIST constant
    name CDATA #REQUIRED
    value CDATA #REQUIRED    
>

<!– END SNIPPET: strutsDtd –>

Struts2 아키텍처..

아래의 이미지는 Strut2의 아키텍처이고, 이 구조에서 사용자가 요청을 하면,

1. Request를 처리할 액션(Action)을 ActionMaper에서 찾아서 실행한다
2. Request가 처리되면서 Action의 Interceptor 전,후 처리를 한다.
3. Response를 한다. 

사용자 삽입 이미지

In the diagram, an initial request goes to the Servlet container (such as Jetty or Resin) which is passed through a standard filter chain. The chain includes the (optional) ActionContextCleanUp filter, which is useful when integrating technologies such as SiteMesh Plugin. Next, the required FilterDispatcher is called, which in turn consults the ActionMapper to determine if the request should invoke an action.

If the ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to the ActionProxy. The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xml file). Next, the ActionProxy creates an ActionInvocation, which is responsible for the command pattern implementation. This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml. The result is then executed, which often (but not always, as is the case for Action Chaining) involves a template written in JSP or FreeMarker to be rendered. While rendering, the templates can use the Struts Tags provided by the framework. Some of those components will work with the ActionMapper to render proper URLs for additional requests.

<!–[if !vml]–><!–[endif]–>

All
objects in this architecture (Actions, Results, Interceptors,
and so forth) are created by an ObjectFactory.
This ObjectFactory is pluggable. We can provide our own ObjectFactory for any
reason that requires knowing when objects in the framework are created. A
popular ObjectFactory implementation uses Spring as provided by the Spring
Plugin
.

Interceptors are executed again (in reverse order, calling the after clause). Finally, the response returns through the filters configured in the web.xml. If the ActionContextCleanUp filter is present, the FilterDispatcher will not clean up the ThreadLocal ActionContext. If the ActionContextCleanUp filter is
not present, the FilterDispatcher will cleanup all ThreadLocals.

스트러츠 2의 .action 확장자 변경하기

스트러츠(struts) 2는 기본으로 아래와 같은 환경파일을 사용하고 있다. 이 파일들 중에 struts.properties 파일이 스트러츠 2의 기본 설정을 가지고 있다.

File
Optional
Location Purpose
web.xml no /WEB-INF/ Web deployment descriptor to include all
necessary framework components
struts.xml yes /WEB-INF/classes/ Main configuration, contains result/view
types, action mappings, interceptors, and so forth
struts.properties yes /WEB-INF/classes/ Framework properties
struts-default.xml yes /WEB-INF/lib/struts2-core.jar Default configuration provided by Struts
struts-default.vm yes /WEB-INF/classes/ Default macros referenced by
velocity.properties
struts-plugin.xml yes At the root of a plugin JAR Optional configuration files for Plugins struts.xml. in
the same format as
velocity.properties yes /WEB-INF/classes/ Override the default Velocity configuration


위 설정 파일들 중에서 struts.properties 에서
### Used by the DefaultActionMapper

### You may provide a comma separated list, e.g.
struts.action.extension=action,jnlp,do struts.action.extension=action
위의 빨간색 설정 부분을
xxx,do,action 등과 같이 우선순위가 높은 순서대로 설정 하면 된다.