论坛首页 Java企业应用论坛

struts2转化器—数组

浏览 2110 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-29  
转化java文件,如输入 23,34  显示<23,34> 一下代码符合数组跟list

 public Object convertFromString(Map context, String[] values, Class toClass)
		{
			if (values.length > 1)
			{
				User[] result = new User[values.length];
				for (int i = 0; i < values.length ; i++ )
				{
					User user = new User();
					String[] userValues = values[i].split(",");
					user.setName(userValues[0]);
					user.setPassword(userValues[1]);
					result[i] = user;
				}
				return result;	
			}
			else
			{
				User user = new User();
				String[] userValues = values[0].split(",");
				user.setName(userValues[0]);
				user.setPassword(userValues[1]);
				return user;
			}

	    }

	    @Override
	    public String convertToString(Map context, Object o)
		{
			if (o instanceof User)
			{
				User user = (User)o;
				return "<" + user.getName() + "," + user.getPassword() + ">";
			}
			else if (o instanceof User[])
			{
				User[] users = (User[])o;
				String result = "[";
				for (User user : users )
				{
					result += "<" + user.getName() + "," + user.getPassword() + ">";
				}
				return result + "]";
			}
			else
			{
				return "";
			}

	    }


jsp显示
   提交页面
      <s:form action="converterAction.action" method="post">
       <s:textfield label="user" name="user" ></s:textfield>
       <s:textfield label="user" name="user" ></s:textfield>
       <s:textfield label="user" name="user" ></s:textfield>
       <s:submit></s:submit>
       </s:form>
   显示页面

       user:<s:property value="user[0]"/><br>
      user:<s:property value="user[1]"/><br>
      user:<s:property value="user[2]"/><br>
   配置文件
xwork-conversion.properties      org.apache.struts.model.User=org.apache.struts.converter.UserConverter
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics