Struts2同名对象转换--只成功了局部转换器

虽然据说有全局和局部转换器,但是我只调试成功了后者。

局部:需要在Converter中把传入的参数看作一包对象(所有同名都传进来了),逐个对这个对象数组进行操作。

转换器:

package Converter;

import actions.Tel;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;

public class TypeConverter extends StrutsTypeConverter
{

@Override
public Object convertFromString(Map arg0, String[] values, Class arg2)
{
Tel[] tel = new Tel[values.length];
for (int i = 0; i < values.length; i++) {
Tel t = new Tel();
String[] userValues = values[i].split("-");
t.setFirst(userValues[0]);
t.setSecond(userValues[1]);
tel[i] = t;
}
return tel;
}

@Override
public String convertToString(Map arg0, Object o)
{
Tel[] tel = (Tel[]) o;
String result = "[";
for (Tel t : tel) {
result += "<" + t.getFirst() + "," + t.getSecond() + ">";
}
return result + "]";
}
}

index.jsp

<s:form action="tel">
<s:textfield label="电话1" name="tel"/>
<s:textfield label="电话2" name="tel"/>
<s:submit value="提交" />
</s:form>

result.jsp

<s:iterator value="tel">
区号:<s:property value="first" />
电话:<s:property value="second" />
<br>
</s:iterator>

ConverterAction1

package actions;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;

public class ConverterAction1 extends ActionSupport
{
private Tel[] tel;

public void setTel(Tel[] t)
{
tel = t;
}

public Tel[] getTel()
{
return tel;
}

public String execute()
{
return SUCCESS;
}
}

ConverterAction1同目录下放置ConverterAction1-conversion.properties

tel=Converter.TypeConverter
 

Leave a Reply

Your email address will not be published. Required fields are marked *