Struts 2.0的Action讲解

Struts 2.0Action讲解

Struts 1.x经验的朋友都知道ActionStruts的核心内容,当然Struts 2.0也不例外。不过,Struts 1.xStruts 2.0Action模型很大的区别。

 

Struts 1.x

Stuts 2.0

接口

必须继承org.apache.struts.action.Action或者其子类

无须继承任何类型或实现任何接口

表单数据

表单数据封装在FormBean

表单数据包含在Action中,通过GetterSetter获取

虽然,理论上Struts 2.0Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法。具体的实现,如例1所示:

<% @ page contentType = " text/html; charset=UTF-8 " %>
<%
@ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > Hello World! </ title >
</ head >
< body >
   
< h2 >< s:property value ="message" /></ h2 >
</ body >
</ html >

1 HelloWorld.jsp

package tutorial;

import java.text.DateFormat;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
   
private String message;
   
   
public String getMessage() {
       
return message;
   }

   
   @Override 
   
public String execute() {
       message =
" Hello World, Now is " + DateFormat.getInstance().format( new Date());
       
return SUCCESS;
   }

}

1 classes/tutorial/HelloWorld.java

< package name ="ActionDemo" extends ="struts-default" >
   
< action name ="HelloWorld" class ="tutorial.HelloWorld" >
       
< result > /HelloWorld.jsp </ result >
   
</ action >
</ package >

1 classes/struts.xmlHelloWorld Action的配置

在浏览器地址栏中键入http://localhost:8080/Struts2_Action/HelloWorld.action


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值