所有的页面与控制器传递的数据都是String类型,在对其进行处理时可能会用到各种的数据类型,程序无法自动完成数据类型的转换,这就需要我们在代码中进行手手动操作,这个过程就称为类型转换。
package com.mxl.entity; public class Product { private String name;//商品名称 private double price;//商品价格 private int num;//入库数量 private String content;//商品描述 public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }Action:
package com.mxl.actions; import com.mxl.entity.Product; import com.opensymphony.xwork2.ActionSupport; public class ProductAction extends ActionSupport{ private Product product; public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } @Override public String execute() throws Exception { return SUCCESS; } }
</action> <action name="pro" class="com.mxl.actions.ProductAction"> <result>/pro_success.jsp</result> </action>
商品名称:<s:property value="product.name"/><br/><br/> 商品价格:<s:property value="product.price"/><br/><br/> 入库数量:<s:property value="product.num"/><br/><br/> 商品描述:<s:property value="product.content"/>
package com.mxl.converter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.mxl.entity.Product; public class ProductConverter extends StrutsTypeConverter{ @Override public Object convertFromString(Map context, String[] values, Class toClass) { Product pro = new Product();//实例化该类 String[] proValues = values[0].split("/");//将传递过来的数组中的第一个元素以“/”分隔并组成新的数组 pro.setName(proValues[0]);//将新数组中的第一个元素赋值给product类中name属性 pro.setPrice(doubleValue(proValues[1]));//将新数组中的第二个元素赋值给product类中price属性 pro.setNum(Integer.parseInt(proValues[2]));//将新数组中的第三个元素赋值给product类中num属性 pro.setContent(proValues[3]);//将新数组中的第4个元素赋值给product类中content属性 return pro; } @Override public String convertToString(Map context, Object obj) { Product pro = (Product)obj; return ""; } }
package com.mxl.actions; import com.mxl.entity.Product; import com.opensymphony.xwork2.ActionSupport; public class ProConverterAction extends ActionSupport{ private Product product1; private Product product2; public Product getProduct1() { return product1; } public void setProduct1(Product product1) { this.product1 = product1; } public Product getProduct2() { return product2; } public void setProduct2(Product product2) { this.product2 = product2; } @Override public String execute() throws Exception { return SUCCESS; } }
<action name="proConverter" class="com.mxl.actions.ProConverterAction"> <result>/pro_list.jsp</result> </action>
<font style="font-size:12px; color:red">在文本框中依次输入商品的名称、价格入库数量和描述之间使用“/”分隔</font> <s:form action="proConverter.action" method="post" cssStyle="margin-top:0px;"> <s:textfield name="product1" label="商品1" size="50"/> <s:textfield name="product2" label="商品2" size="50"/> <s:submit value="确认入库" align="left"/> </s:form>
<ul id="heng" class="addPro"> <li style="font-weight:bold;">商品名称</li> <li style="font-weight:bold;">商品价格</li> <li style="font-weight:bold;">商品数量</li> <li style="font-weight:bold;">商品描述</li> </ul> <ul id="heng" class="addPro"> <li><s:property value="product1.name"/></li> <li><s:property value="product1.price"/></li> <li><s:property value="product1.num"/></li> <li><s:property value="product1.content"/></li> </ul> <ul id="heng" class="addPro"> <li><s:property value="product2.name"/></li> <li><s:property value="product2.price"/></li> <li><s:property value="product2.num"/></li> <li><s:property value="product2.content"/></li> </ul>
package com.mxl.entity; import java.util.Date; public class User { private String username;//用户名 private String password;//密码 private String realname;//真实姓名 private int age;//年龄 private Date birthday;//生日 private String address;//家庭住址 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
<action name="userException" class="com.mxl.actions.UserExceptionAction"> <result>/user_success.jsp</result> <result name="input">/user_regist.jsp</result> </action>添加局部资源文件:
invalid.fieldvalue.user.age=会员年龄必须为整数 invalid.fieldvalue.user.birthday=会员出生日期必须为日期格式注册页面Z:
<s:form action="userException.action" method="post"> <s:textfield name="user.username" label="用户名" size="15"/> <s:password name="user.password" label="密码" size="15"/> <s:textfield name="user.realname" label="姓名" size="15"/> <s:textfield name="user.age" label="年龄" size="15"/> <s:textfield name="user.birthday" label="出生日期" size="15"/> <s:textfield name="user.address" label="家庭住址" size="15"/> <s:submit type="button" value="提交"/> </s:form>跳转界面:
用户名:<s:property value="user.username"/><br/><br/> 密码:<s:property value="user.password"/><br/><br/> 真实姓名:<s:property value="user.realname"/><br/><br/> 年龄:<s:property value="user.age"/><br/><br/> 出生日期:<s:property value="user.birthday"/><br/><br/> 家庭住址:<s:property value="user.address"/><br/><br/>