2 bbzhshw bbzhshw 于 2014.04.09 22:22 提问

从页面表单提交数据时,为啥没有经过类型转换类?求解

在进行类型转换测试时发现,在从页面提交参数到后台时,并没有进过我配置的类型转换器,但在将后台类中的属性输出到页面上时,经过了类型转换器。
有没有哪位骚年能指出错在哪里?

struts.xml的配置内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">


   <action name="LoginAction" class="com.st.action.LoginAction">
       <result name="success">/result.jsp</result>
   </action>


接受页面传过来的参数的是Customer类
public class Customer
{
private String username;
private String password;
private int age;
public String getUsername() {
System.out.println("从customer中取值username");
return username;
}
public void setUsername(String username) {
System.out.println("往customer中赋值username");
this.username = username;
}
public String getPassword() {
System.out.println("从customer中取值password");
return password;
}
public void setPassword(String password) {
System.out.println("往customer中赋值password");
this.password = password;
}
public int getAge() {
System.out.println("从customer中取值age");
return age;
}
public void setAge(int age) {
System.out.println("往customer中赋值age");
this.age = age;
}

}

Action类中存放的是Customer类型的属性
public class LoginAction extends ActionSupport
{
private Customer ct;

public Customer getCt()
{
System.out.println("从Action中取值");
return ct;
}

public void setCt(Customer ct)
{
System.out.println("给Action中的属性赋值");
this.ct = ct;
}

@Override
public String execute()
{
System.out.println("execute is invoked");
return SUCCESS;
}

@Override
public void validate() {
System.out.println("validate is invoked");
}

}

下方是我的类型转换器的代码

public class CustomerConverter extends DefaultTypeConverter
{
@Override
public Object convertValue(Map context, Object value, Class toType)
{
System.out.println(toType.getName());
if(Customer.class==toType)
{
System.out.println("外转内");
String[] str = (String[])value;
String username = str[0];
String password = str[1];
int age = Integer.parseInt(str[2]);
Customer ct = new Customer();
ct.setUsername(username);
ct.setPassword(password);
ct.setAge(age);
return ct;
}
else if(String.class==toType)
{

System.out.println(toType.getName());
System.out.println("内转外");
Customer ct1 = (Customer)value;
String username = ct1.getUsername();
String password = ct1.getPassword();
String age = String.valueOf(ct1.getAge());
String info = "username is "+username+" password is "+password+" age is "+age;
return info;

}
return null;
}
}

properties文件也有配置就放在action的包中

ct=com.st.converter.CustomerConverter

提交表单后,页面输出ognl.NoConversionPossible

1个回答

bbzhshw
bbzhshw   2014.04.13 14:28

此问题已解决,是由于jsp页面中input标签里的name属性值写错造成的。name的属性值要和action中的属性变量名要一样。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!