Struts2初级教程01 hello world

一.新建一个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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值