Action类似于servlet,在用户对浏览器输入url访问的时候充当控制器的角色。它在访问时产生,执行execute()之后就销毁了。

写Action是代理事务,它实现的三种方式是: (1)POJO类

                     (2)继承ActionSupport类

                     (3)实现Action接口

POJO类

public class ActionPrint {

    public String execute(){
System.out.println("ActionPrint execute!");
return "success";
}
}

继承ActionSupport类

import com.opensymphony.xwork2.ActionSupport;

public class ActionInputCheck extends ActionSupport{
private int a;
private double b;
private char c;
private String d;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public char getC() {
return c;
}
public void setC(char c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String execute(){ return SUCCESS;
}
public void validate(){
if(a<0)
this.addActionError("d不能小于0"); } }

实现Action接口

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven; import Beans.User; public class MDriven implements Action,ModelDriven<User>{ private User user=new User();
public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public User getModel() {
// TODO Auto-generated method stub return user;
} @Override
public String execute() throws Exception {
ActionContext context=ActionContext.getContext();
context.getSession().put("username", user.getUsername());
return SUCCESS;
} }

ModelDriven接口是让javabean的实现和Action分离,Action只用扮演好控制器的角色就好,javabean是在Model层的东东,这样比较符合MVC的设计模式。

写了Action之后相应加上struts.xml的配置

           </action>
<action name="InputCheck" class="StrutsFund.ActionInputCheck">
<result name="success">/output.jsp</result>
</action>
<action name="MDriven" class="StrutsFund.MDriven">
<result name="success">/login_success.jsp</result>
</action>
<action name="print" class="StrutsFund.ActionPrint">
<result name="success">/index.jsp</result>
</action>

访问的时候就在网址栏中输入:http://localhost:8080/项目名/ActionName

struts.properties配置文件

该文件定义了struts2框架的大量属性。只要将该文件放在web应用的classes下,struts2框架就会自动加载。

最新文章

  1. Redis 哨兵模式实现主从故障互切换
  2. IBM Domino 9 出现 Server Controller 未在主机上运行或未在端口2050监听 解决方案
  3. Hadoop总结篇之五---模块间是怎么驱动执行的
  4. Permission is only granted to system apps
  5. js checkbox 选中判断
  6. Shell 编程基础之变量和环境变量
  7. 从客户端中检测到有潜在危险的 Request.Form 值。
  8. breakpoints
  9. linux 屏幕亮度调节
  10. PHP之cookie相关实例教程与经典代码
  11. bzoj1093
  12. CodeForces 396C 树状数组 + DFS
  13. matlab 中max函数用法
  14. 教你用python写:HDU刷题神器
  15. js图片懒加载(滚动加载)是否生效
  16. bzoj1997 Planar
  17. 解析:为什么设计师选择mac电脑居多?
  18. Python3基础-分数运算
  19. xadmin后台分段导出避免timeout
  20. Linux下找不到so文件的解决办法

热门文章

  1. Ubuntu修改文件关联
  2. 迷你DVD管理器
  3. 用centos光盘安装RPM包的方法
  4. 屠龙之路_战胜View&amp;对DataBase猛烈进攻_ThirdDay
  5. Android&#160;Material&#160;Design&#160;控件常用的属性
  6. hdu3410 单调队列
  7. 如何知道某个网站的IP地址
  8. Redis有序集合Zset(sorted set)
  9. BZOJ 4384: [POI2015]Trzy wieże
  10. Python基础5:列表 元祖 字典 集合