struts2中的几个基础类

1、Action类定义了几个常量
public interface Action{
public static final String SUCESS = "success";
public static final String ERROR = "error";
public static final String NONE = "none";
public static final String INPUT = "input";
public static final String LOGIN = "login";

public String execute() throws Exception;
}
2、ActionSupport类,实现了Action类,同时实现了Validateable接口,提供了数据校验功能
其中的方法validate()
public void validate(){
if(getUserName() == null || getUserName().trim().equals("")){
addFieldError("userName", "user.required");
}
if(getPassword() == null || getPassword().trim().equals("")){
addFieldError("password", "password.required");
}
}
该方法在执行系统的execute方法之前执行,如果执行该方法后,fieldErrors中已经有值,请求将被转发到input逻辑视图处

3、ActionContext类
package com.opensymphony.xwork2.ActionContext
访问servlet API
request/session/application相当于
HttpServletRequest/HttpSession/ServletContext

ActionContext atx = ActionContext.getContext();
Integer count = (Integer)atx.get("count"); String count = (String)atx.get("count");
若为Integer类型,在jsp页面中需要注意类型的转化,不然会报错:
严重: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: java.lang.Integer

atx.getApplication().put("count", count); application范围
atx.getSession().put("count", count); session范围
atx.put("tip", count); 这样是放在request范围内的,可以用s:property访问到
取值要通过<s:property value="" />或在任意的<s:/>标签内使用%{}
当Action的valueStack中有该属性的值时,只需直接使用该属性的名字即可;
当Action的valueStack中没有该属性的值时,比如在session,application范围中的属性值时,需要加#或者#attr.

假设某Action中有person成员变量,在application中存在company属性
那么我们可以通过以下方法取值:
<s:property value= "person.name" />
<s:property value= "#person.name" />
<s:property value= "company.name" /> //无法取到,因为company不在action的valueStack中
<s:property value= "#company.name" />
<s:textfield name= "person.name" value= "person.name" /> //错误,value会直接显示person.name字样
<s:textfield name= "person.name" value= "%{person.name}" />
<s:textfield name= "person.company.name" value= "%{#company.name}" />
<s:textfield name= "person.company.name" value= "%{#attr.company.name}" /> 在application范围需要加#attr.

原文地址:https://www.cnblogs.com/ikuman/p/2241103.html