想把string转int,又不想用强制类型转换的时候

比如单选钮的value必须跟的是String,而后台提交又想提交int,而value写的时候是英文字母,不是数字的String,又不想改,这时强制类型转换就不好用了,所以,就有了下面的代码

package struts.form;

import java.util.* ;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class RationDrawInvoiceForm extends ActionForm {

 private String rationDrawInvoiceMoney;

 int rationDrawInvoiceMoneyInt ;

 private List errors ;

 public void setErrors(List errors)
 {
  this.errors = errors ;
 }
 public List getErrors()
 {
  return this.errors ;
 }

 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
    return  null;
 }

 public void reset(ActionMapping mapping, HttpServletRequest request) {
  rationDrawInvoiceMoney = "fifty" ;
 }

  public String getRationDrawInvoiceMoney() {
  if(rationDrawInvoiceMoney.equals("fifty"))
  {
   this.rationDrawInvoiceMoneyInt = 50 ;
  }
  if(rationDrawInvoiceMoney.equals("oneHundred"))
  {
   this.rationDrawInvoiceMoneyInt = 100 ;
  }
  if(rationDrawInvoiceMoney.equals("twoHundred"))
  {
   this.rationDrawInvoiceMoneyInt = 200 ;
  }
  return rationDrawInvoiceMoney;
 }
 public void setRationDrawInvoiceMoney(String rationDrawInvoiceMoney) {
  this.rationDrawInvoiceMoney = rationDrawInvoiceMoney;
 }
 public int getRationDrawInvoiceMoneyInt() {
  return rationDrawInvoiceMoneyInt;
 }
 public void setRationDrawInvoiceMoneyInt(int rationDrawInvoiceMoneyInt) {
  this.rationDrawInvoiceMoneyInt = rationDrawInvoiceMoneyInt;
 }
}



红色部分是后加上去的,而不是jsp提交来的值,这样写了之后,可以在action里直接像调用其他属性一样来调用转换成int的变量,调用过去后就已经转换成int了



黑色头发 http://heisetoufa.iteye.com