我有一个演示来展示我们如何在 struts2 中定义客户类型转换器。演示很简单,输入一个字符串“Tom,12”,代表用户的姓名和年龄。该值将被放入用户对象中。控制台将输出这些值。下面是代码:JSP:
<form action=testconvert method="post">
Please enter your name<br/>
<input type="text" name="user"/>
<input type="submit" value="test"/>
</form>
UserConverter:这是一个用户定义的转换器。
public class UserConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object value, Class toType) {
System.out.println("convertValue");
if(User.class == toType)
{
String[] str = (String[])value;
String firstValue = str[0];
System.out.println(firstValue);
StringTokenizer st = new StringTokenizer(firstValue, ",");
String username = st.nextToken();
int age =Integer.parseInt( st.nextToken());
User user = new User();
user.setUsername(username);
user.setAge(age);
return user;
}
else if(String.class == toType)
{
User user = (User)value;
String username = user.getUsername();
int age = user.getAge();
String userInfo = "username: " + username + ", age: " + age;
return userInfo;
}
return null;
}
}
我有名为“testconvert-conversion.properties”的属性文件:
user=com.tutorialspoint.sconvert.UserConverter
动作文件:
public class testconvert extends ActionSupport
{
private User user;
public User getUser()
{
System.out.println("getUser");
return user;
}
public void setUser(User user)
{
System.out.println("setUser");
this.user = user;
}
@Override
public String execute() throws Exception
{
System.out.println("username: " + user.getUsername());
System.out.println("age: " + user.getAge());
System.out.println("execute");
return SUCCESS;
}
}
当我提交值时: "Tom,20" ,控制台输出以下内容:
WARNING: Parameter [user] is not on the excludeParams list of patterns and will be appended to action!
convertValue
Tom,20
setUser
username: Tom
age: 20
execute
它有一个警告,但它可以输出我需要的东西。然后我更改 JSP 文件。我将代码更改如下:
<input type="text" name="user222"/>
我认为输入标签的名称并不重要。但我错了。我没有正确获得输出。
所以我的问题是:
1,警告是什么意思?
2、如果我想要<input>
名字是user222怎么办?