实例代码:test-conversion.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test-conversion.jsp' starting page</title>
</head>
<body>
<s:form action="Test_Conversion">
<s:textfield name="product" label="Product"></s:textfield>
<s:textfield name="customer" label="Customer"></s:textfield>
<s:textfield name="quantity" label="Quantity"></s:textfield>
<s:textfield name="price" label="Price"></s:textfield>
<s:textfield name="orderDate" label="OrderDate"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
相对应的javabean类Order.java:
package com.llp182.test;
import java.sql.Date;
public class Order {
private String product;
private Customer customer;
private int quantity;
private float price;
private Date orderDate;
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
@Override
public String toString() {
return "Order [customer=" + customer + ", orderDate=" + orderDate
+ ", price=" + price + ", product=" + product + ", quantity="
+ quantity + "]";
}
}
相对应Action类,TestConversionAction.java:
package com.llp182.test;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import java.util.*;
public class TestConversionAction extends ActionSupport
implements ModelDriven<Order>,Preparable{
private Collection<Customer> custs;
public Collection<Customer> getCusts() {
return custs;
}
public void setCusts(Collection<Customer> custs) {
this.custs = custs;
}
public String addCusts(){
System.out.println("custs:"+custs);
return SUCCESS;
}
public String add(){
System.out.println("add: "+order);
return SUCCESS;
}
private Order order=null;
public void prepare() throws Exception {
order=new Order();
}
public Order getModel() {
return order;
}
}
类型转换器:
CustomerConverter.java:
package com.llp182.test;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class CustomerConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
if(arg2.getName().equals(Customer.class.getName())){
if(arg1!=null && arg1.length>0){
String value = arg1[0];
String[] strs=value.split("-");
if(strs != null && strs.length > 1){
String customerName = strs[0];
String phoneNumber = strs[1];
Customer customer = new Customer();
customer.setCustomerName(customerName);
customer.setPhoneNmber(phoneNumber);
return customer;
}
}
}
return null;
}
@Override
public String convertToString(Map arg0, Object arg1) {
if(arg1!=null && arg1 instanceof Customer){
Customer cust=(Customer)arg1;
return cust.getCustomerName()+"-"+cust.getPhoneNmber();
}
return null;
}
}
在相对应Action类包中com.llp182.test新建一个TestConversionAction.properties文件
#Invalid field value for field "orderDate"
#invalid.fieldvalue.fieldName=custMsg
invalid.fieldvalue.orderDate=\u65E5\u671F\u683C\u5F0F\u4E0D\u5408\u6CD5
接下来,使类型转化器和action中的对应Customer属性关联起来新建一个properties文件
这里有2种方法
1)第一种是在com.llp182.test包中新建一个Order-conversion.properties文件
customer=com.llp182.test.CustomerConverter
2)是在src目录下直接新建一个文件 xwork-conversion.properties
com.llp182.test.Customer=com.llp182.test.CustomerConverter