1、页面获取action的返回值的方式
struts2.0的标签可以直接获取action中的属性,但是一个action中经常有很多操作函数,他们返回的属性可能不一样,导致属性冗余,而且也不是很直观的知道函数中返回那些对象,所以我一直沿用老的返回值方法,就是将返回对象放到request中,或者直接输出json字符串,页面上再使用EL来获取具体的值,可是struts的表情不支持el,所以现在只能使用#request.xxx方法获取返回值。
2、action获取页面传入的参数的方式
我设计的action一般不存在action的属性变量,因此无法直接从action属性中获取相应的值,我采用BeanUtils类来获取我要的属性。
3、页面校验设计
我定义了个通过的JS,他会根据各个HTML的控制中自定义的属性来校验数据,这些自定义属性包括:required(必须)、isInt(整数)、isNumeric、isEmpty、maxLen、minLen(最小长度)、equalsLen(长度)
BeanUtils代码:
public class BeanUtilsEx
{
private static Logger logger = Logger.getLogger(BeanUtilsEx.class);
private static BeanUtilsBean util = null;
static
{
DateConverter d = new DateConverter();
String[] datePattern = { "yyyy-mm-dd", "yyyy/mm/dd", "yyyy.mm.dd","yyyymmdd" };
d.setPatterns(datePattern);
ConvertUtils.register(d, java.util.Date.class);
// 以下几行解决当值为空时,用 null 设置对应字段的值, (但 String 类型仍然非null, 为空字符串)
ConvertUtils.register(new IntegerConverter(null), Integer.class);
ConvertUtils.register(new FloatConverter(null), Float.class);
ConvertUtils.register(new DoubleConverter(null), Double.class);
// ConvertUtils.register(new DateConverter("yyyy-MM-dd"), Date.class);
ConvertUtils.register(new DateConverter(null), Date.class);
ConvertUtils.register(new StringConverter(null), String.class);
util = new BeanUtilsBean(new ConvertUtilsBean(),new PropertyUtilsBeanEx() );
}
/**
* 从 request 传递的参数集合,组装到 JavaBean 中
*
* @param request
* @param obj
*/
@SuppressWarnings("unchecked")
public static void populate(HttpServletRequest request, Object obj) {
Map map = request.getParameterMap();
try
{
util.populate(obj, map);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
logger.error(e);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
logger.error(e);
}
}
}
用例
/**
* 从request生成指定的BEAN对象
* @param object bean对象
* @return
*/
public void request2Bean(Object object)
{
BeanUtilsEx.populate(this.getRequest(), object);
}
@Override
public String addUser() {
String ret=UserActionName.USERSEARCH;
User user = new User();
user.setEmployer(new Employer());
request2Bean(user);
User writer = this.getLoginUser();
userServ.addUser(user, writer);
return ret;
}
JS代码
var COMJS={
validate:function(form)
{
var required_objs = $(form).find("*[required='1']");
for(var i=0; i<required_objs.length; i++)
{
var obj = $(required_objs.get(i));
var v = $.trim(obj.val());
var msg = obj.attr("message");
if(v == '')
{
var str = "填写";
var tagName = obj.attr("tagName");
var tagType = obj.attr("type");
if(tagName == "SELECT" || tagType == "checkbox" || tagType=="radio")str="选择";
jAlert('info', "["+msg+"]必须"+str+"!", '警告');
return false;
}
obj.val(v);
}
var num_objs = $(form).find("*[isnumber='1']");
for(var i=0; i<num_objs.length; i++)
{
var obj = $(num_objs.get(i));
var v = obj.val();
var msg = obj.attr("message");
if(!/^[\-+]?[\d]*\.?[\d]+$/.test(v))
{
jAlert('info', "["+msg+"]只能是数值", '警告');
return false;
}
}
var int_objs = $(form).find("*[isint='1']");
for(var i=0; i<int_objs.length; i++)
{
var obj = $(int_objs.get(i));
var v = obj.val();
var msg = obj.attr("message");
if(!/^\d+$/.test(v))
{
jAlert('info', "["+msg+"]只能是数字组成", '警告');
return false;
}
}
var min_objs = $(form).find("*[minlength]");
for(var i=0; i<min_objs.length; i++)
{
var obj = $(min_objs.get(i));
var v = $.trim(obj.val());
var msg = obj.attr("message");
var minlength = obj.attr("minlength");
if(v == '' || v.length - minlength < 0)
{
jAlert('info', "["+msg+"]长度不能小于["+minlength+"]", '警告');
return false;
}
obj.val(v);
}
var equal_objs = $(form).find("*[equallength]");
for(var i=0; i<equal_objs.length; i++)
{
var obj = $(equal_objs.get(i));
var v = $.trim(obj.val());
var msg = obj.attr("message");
var equallength = obj.attr("equallength");
if(v == '' || v.length != equallength )
{
jAlert('info', "["+msg+"]长度必须是["+equallength+"]", '警告');
return false;
}
obj.val(v);
}
var max_objs = $(form).find("*[maxlength]");
for(var i=0; i<max_objs.length; i++)
{
var obj = $(max_objs.get(i));
var v = $.trim(obj.val());
var msg = obj.attr("message");
var maxlength = obj.attr("maxlength");
obj.val(v);
if(v.length - maxlength > 0)
{
jAlert('info', "["+msg+"]长度["+v.length+"]超出最大长度["+maxlength+"]", '警告');
return false;
}
}
var legal_objs = $(form).find("*[legalchar='1']");
for(var i=0; i<legal_objs.length; i++)
{
var obj = $(legal_objs.get(i));
var v = obj.val();
var msg = obj.attr("message");
if(!COMJS.isLegal(v))
{
jAlert('info', "["+msg+"]只能有数字、英文字母、_、-四种字符组成", '警告');
return false;
}
}
return true;
},
isLegal:function(str)
{
return /^[\d\w\-\_]+$/.test(str);
}
}
html示例:
<select message="订货单位" required="1" onchange="orderadd.changeClient(this,this.form)" name="client.id" id="client.id">
<script language="javascript">
COMJS.validate(frm);
</script>