struts 2 nutshell

Apache Struts 2 Architecture in a Nutshell

 

  1. The web browser requests a resource (/mypage.action, /reports/myreport.pdf, et cetera)
  2. The Filter Dispatcher looks at the request and determines the appropriate Action
  3. The Interceptors automatically apply common functionality to the request, like workflow, validation, and file upload handling
  4. The Action method executes, usually storing and/or retrieving information from a database
  5. The Result renders the output to the browser, be it HTML, images, PDF, or something else

Struts Tags in a nutshell

     The Struts Tags help you create rich web applications with a minimum of coding.

 

    <s:actionerror/>
<s:form action="Profile_update" validate="true">
    <s:textfield label="Username" name="username"/>
    <s:password label="Password" name="password"/>
    <s:password label="(Repeat) Password" name="password2"/>
    <s:textfield label="Full Name" name="fullName"/>
    <s:textfield label="From Address" name="fromAddress"/>
    <s:textfield label="Reply To Address" name="replyToAddress"/>
    <s:submit value="Save" name="Save"/>
    <s:submit action="Register_cancel" value="Cancel" name="Cancel"  οnclick="form.οnsubmit=null"/>
</s:form>

 

The Struts Tags also support validation and localization as first-class features. So not only is there less code, but there is more utility. In about the same amount of code as two conventional controls, the Struts Tags can create an entire data-input form with eight controls. Not only is there less code, but the code is easier to read and maintain.

 

 

Struts Configuration in a Nutshell

 

Here's a typical configuration (struts.xml) for a login workflow:

 

<struts>
    <package name="default" extends="struts-default">
        <action name="Logon" class="mailreader2.Logon">
            <result name="input">/pages/Logon.jsp</result>
            <result name="cancel" type="redirectAction">Welcome</result>
            <result type="redirectAction">MainMenu</result>
            <result name="expired" type="chain">ChangePassword</result>
        </action>

        <action name="Logoff" class="mailreader2.Logoff">
            <result type="redirectAction">Welcome</result>
        </action>
    </package>
</struts>

Struts MVC in a Nutshell

 

Struts MVC in a Nutshell

Struts is a Model View Controller framework. Struts provides Controller and View components, and integrates with other technologies to provide the Model.

The framework's Controller acts as a bridge between the application's Model and the web View.

To make it easier to present dynamic data, the framework includes a library of markup tags. The tags interact with the framework's validation and internationalization features,

to ensure that input is correct and output is localized. The tag library can be used with JSP, FreeMarker, or Velocity. Of course, other tag libraries, JSTL, and AJAX can also be used,

with or without the Struts tags. JavaServer Faces components are also supported.

When a request is received, the Controller invokes an Action class. The Action class examines or updates the application's state by consulting the Model (or, preferably, an interface

representing the Model). To transfer data between the Model and the View, properties can be placed on the Action class, or on a plain old JavaBean.

Most often, the Model is represented as a graph of JavaBean objects. The Model should do the "heavy lifting", and the Action will act as a "traffic cop" or adapter. The framework provides

sophisticated, automatic type conversion to simplify transfering data between rich domain objects and text-only HTTP requests.

Struts is extensible. Very extensible. Every class deployed by the framework is based on an interface. We provide all the base classes an application may ever need, but if we missed something,

it's easy to add your own. We provide the general-purpose framework, but you can still write your application your way.

 

AJAX

 

Both Struts 1 and Struts 2 can return any type of response. We are not limited to forwarding to a server page. In Struts 1, you can just do something like:

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World!  This is an AJAX response from a Struts Action.");
out.flush();
return null;

In Struts 2, we can do the same thing with a Stream result.

 

    package actions;

   import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;

public class TextResult extends ActionSupport  {
    private InputStream inputStream;
    public InputStream getInputStream() {
        return inputStream;
    }

    public String execute() throws Exception {
        inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
        return SUCCESS;
    }
}

 

<action name="text-result" class="actions.TextResult"> <result type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> </action>

 

Struts 2 can also return a JSON (JavaScript Object Notation) response, using a plugin.

On the client side, there are two basic strategies, which can be mixed and matched.

First, you can use some type of JSP tag. Here, you don't have to know very much at all about Ajax or JavaScript. The taglib does all the work, and you just have to figure out how to use the taglib. The standard Struts 2 taglib includes several Ajax JSP tags, and many third-party libraries are available, including:

Alternatively, you can use a plain-old Ajax widget on a plain-old HTML page, using libraries like Dojo, JQuery, or YUI, and the StreamResult or the JSON Plugin. Here, the sky's the limit, but you actually have to learn something about JavaScript as a language.

 

Dependency Injection

 

 

Internally, the framework uses its own dependency injection container that is very similar to Google Guice (both were originally developed by Bob Lee). Plugins are available to integrate applications with other IoC containers, including the Spring Plugin and the Plexus Plugin. An application can even use a local copy of Google Guice for dependency injection needs.

 

 

Profiling

 

Profiling software looks for bottlenecks in program execution. In addition to the profiling services provided by IDEs and standalone profilers, the framework provides its own internal support for profiling.

Profiling aspects

Struts2 profiling aspects involves the following :-

  • ActionContextCleanUp
  • FreemarkerPageFilter
  • DispatcherFilter
    • Dispatcher
      • creation of DefaultActionProxy
        • creation of DefaultActionInvocation
          • creation of Action
      • execution of DefaultActionProxy
        • invocation of DefaultActionInvocation
          • invocation of Interceptors
          • invocation of Action
          • invocation of PreResultListener
          • invocation of Result

 

Using

To enable profiling, first make sure that the profiling interceptor is applied to your action, like:

<action ... >   
   ... 
   <interceptor-ref name="profiling"> 
       <param name="profilingKey">profiling</param> 
   </interceptor-ref> 
   ... 
</action> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值