Struts2初级教程01 hello world

来源:互联网 发布:手机安装手电筒软件 编辑:程序博客网 时间:2024/05/17 09:23

一.新建一个web项目struts2的web应用所需要的最少类库

struts2-core-2.0.11.jarstruts2框架的核心类库xwork-2.0.4.jarXWork类库,struts2在基上构建ognl-2.6.11.jarstruts2框架使用的一种表达式语言freemarker-2.3.8.jarstruts2的UI标签的模板使用FreeMarker编写commons-logging-1.0.4.jar日志记录用

二.在web.xml文件中配置FilterDispatcher

struts2框架是基于MVC模式开发的,它提供了一个控制器.用于对所有请求进行统一处理.这个控制器是由一个名为FilterDispatcher的Servlet过滤器来充当的.
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>

三.编写Action类

一个action就是一段只有特定的URL被请求时才会执行的代码.action执行的结果,通常都对应着一个要呈现给用户的result,这个result可以是HTML,PDF,Excel.其配置在struts.xml中.
在Struts2中可以用一个普通的类作为Action类,只要这个类提供execute方法,如
public class TestAction {public String execute() throws Exception {System.out.println("你要执行的code");return "SUCCESS";}}

也可实现接口com.opensymphony.xwork2.Action,接口中定义了execute方法外,还有常常量.如:
package com.opensymphony.xwork2;public abstract interface Action{  public static final String SUCCESS = "success";  public static final String NONE = "none";  public static final String ERROR = "error";  public static final String INPUT = "input";  public static final String LOGIN = "login";  public abstract String execute()    throws Exception;}
在开发中常用继承类com.opensymphony.xwork2.ActionSupport,这个类也是实现了 com.opensymphony.xwork2.Action接口.
在所有action必须返回一个字条串类型的结果代码,以标识要呈现给用户的result.

涉及到的代码也相对简单,在这里就不贴出来了...

原创粉丝点击