Struts2.1的HelloWorld

 

Struts2.1的HelloWorld

一、Struts2?

       一个web层框架,在 struts 和WebWork的技术基础上进行了合并,全新的Struts 2框架。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与Servlet API完全脱离开,所以Struts 2可以理解为WebWork的更新产品。

二、编写HelloWorld

1.搭建环境



web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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></web-app>
   

2.界面jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h2><s:property value="message" /></h2>
    </body>
</html>
 

 

3.action类

 

package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String MESSAGE = "Struts is up and running ...";

    public String execute() throws Exception {
        setMessage(MESSAGE);
        return SUCCESS;
    }

    private String message;

    public void setMessage(String message){
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
 

 

 

 

4.配置文件struts.xml

 

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="tutorial" extends="struts-default">
        <action name="HelloWorld" class="tutorial.HelloWorld">
            <result>/HelloWorld.jsp</result>
        </action>
        <!-- Add your actions here -->
    </package>
</struts>

 

三 、调用

部署工程,输入地址http://localhost:8080/Struts2_01_HelloWorld/HelloWorld.action

结果:



 四、流程解释

 

1.The container receives from the web server a request for the resource HelloWorld.action. According to the settings loaded from the web.xml, the container finds that all requests are being routed to org.apache.struts2.dispatcher.FilterDispatcher, including the /* requests. The FilterDispatcher is the entry point into the framework.

   Struts容器接受一个请求:HelloWorld.action,根据web.xml中的配置/*,所有的请求requests将途经FilterDispatcher,FilterDispatcher是框架的入口

2.The framework looks for an action mapping named "HelloWorld", and it finds that this mapping corresponds to the class "HelloWorld". The framework instantiates the Action and calls the Action's execute method.

   框架寻找一个HelloWorld的action,并找到它对应的java类"HelloWorld"。 框架实例HelloWorld这个action病调用execute方法

3.The execute method sets the message and returns SUCCESS. The framework checks the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render as the response to the request, the resource HelloWorld.jsp.

执行execute方法給message赋值并返回SUCCESS,框架检查如果结果返回是SUCCESS时,要加载的界面HelloWorld.jsp

4.As the page HelloWorld.jsp is being processed, the <s:property value="message" /> tag calls the getter getMessage of the HelloWorld Action, and the tag merges into the response the value of the message.


当处理HelloWorld.jsp时,<s:property value="message" />意味要调用HelloWorldAction中的getMessager方法,方法的返回值将写到页面上

 

5.A pure HMTL response is sent back to the browser.

一个单纯的HTML界面被发到浏览器上

 

五、Struts2体系

 

 


 

 

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.

 

一个请求经过一个过滤器链filter chain进入servlet容器。这个filter chain 包括(可选)了一个叫ActionContectCleanUp 的过滤器。 接着FilterDispatcher被调用,FilterDispatcher反过来询问ActionMapping去决定是否请求中需要调用一个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.

 

如果需要调用一个action,FilterDispatcher将控制委托给ActionProxy。ActionProxy询问框架的onfiguration Files manager (配置文件管理器,从struts.xml文件中初始化的)。接着actionProxy创建一个ActionInvocation,在调用action前调用一些列的拦截器Interceptors

 

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.

 

一旦action返回,ActionInvocation 有责任区查看在struts.xml中配置的result。这个result经常调用一个用jsp或FreeMaker写模板进行渲染. 当渲染时可以使用框架提供的struts标签。这些控件将和ActionMapper一起去渲染合适的URL

 

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.

在结构中的所有对象,是有一个对象工厂(ObjectFactory)创建的。这个ObjectFactory是可插入的。我们可以提供自己的ObjectFactory,一个比较流行的ObjectFactoy是由Spring通过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. 

Interceptors 将被反向调用依次。最后,response通过在web.xml中注册的filters返回.

 

六、小结

The framework uses Actions to process HTML forms and other requests. The Action class returns a result-name such as SUCCESSERROR, or INPUT. Based on the mappings loaded from the struts.xml, a given result-name may select a page (as in this example), another action, or some other web resource (image, PDF).

框架使用Action处理表单或其他请求.Action类返回一个result-name如SUCCESS等。基于在struts.xml中的配置对应的result将选择一个界面page,其他action 或一些其他资源(image,pdf)

 

When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.

当一个服务界面被渲染,经常会包含一些有action提供的动态数据。为了更简单的展现动态数据,框架提供一系列的标签去和html标记生成一个服务界面

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值