1.form表单提交按钮
<s:form action="register" namespace="/">
<s:textfield name="name" label="名称"/>
<s:textfield name="age" label="年龄"/>
<s:textfield name="birthday" label="生日"/>
<s:textfield name="point" label="坐标"/>
<s:submit value="提交" ></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
2.提交后被核心过滤器拦截,struts.xml中查找action--------------constant标签修改框架中的常量值,具体修改信息在message中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.jbit.action.RegisterAction">
<result>success.jsp</result>
<result name="input">register.jsp</result>
</action>
</package>
</struts>
3.message.properties存放提示的错误信息,具体效果见图片
xwork.default.invalid.fieldvalue=字段{0}输入无效
4.RegisterAction进行验证,失败返回input,成功返回success
import java.util.Date;
import com.jbit.entity.Point;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String name;
private int age;
private Date birthday;
private Point point;//getset方法,验证失败自动返回input
public String execute(){
return "success";
}
}
5.RegisterAction-conversion.properties 调用验证date的类 属性=验证类
birthday=com.yh.struts.action.DateConverter
6.DateConverter extends StrutsTypeConverter 验证date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.opensymphony.xwork2.conversion.TypeConversionException;
public class DateConverter extends StrutsTypeConverter {
SimpleDateFormat[] sdf = { new SimpleDateFormat("yyyy年MM月dd日"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyyMMdd") };
/**
* 进
*/
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
// TODO Auto-generated method stub输入流验证
String text = arg1[0];
Date date = null;
for (int i = 0; i < sdf.length; i++) {
try {
date = sdf[i].parse(text);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
continue;
}
}
throw new TypeConversionException("转换错误!");
}
/**
* 出
*/
@Override
public String convertToString(Map arg0, Object arg1) {
// TODO Auto-generated method stub输出验证
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
return sdf.format(arg1);
}
}
7.xwork-conversion.properties 全局验证point 类名=验证 放于src下
com.jbit.entity.Point=com.yh.struts.action.PointConverter
8. PointConverter extends StrutsTypeConverter
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.jbit.entity.Point;
import com.opensymphony.xwork2.conversion.TypeConversionException;
public class PointConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
// TODO Auto-generated method stub
String pointStr = arg1[0];// x,y
if (pointStr.indexOf(",") != -1) {
String[] xy = pointStr.split(",");
Point p = new Point();
p.setX(Integer.parseInt(xy[0]));
p.setY(Integer.parseInt(xy[1]));
return p;
}else{
throw new TypeConversionException("转换错误!");
}
}
@Override
public String convertToString(Map arg0, Object arg1) {
// TODO Auto-generated method stub
Point p = (Point) arg1;
return "(" + p.getX() + "," + p.getY() + ")";
}
}
9.执行action完成后返回struts.xml 成功则调制success.jsp
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.jbit.action.RegisterAction">
<result>success.jsp</result>
<result name="input">register.jsp</result>
</action>
</package>
10.success.jsp
<body>
name: <s:property value="name"/><br/>
birthday:<s:property value="birthday"/>
<br/>
Point:<s:property value="point"/>
</body>