先有一个类user,其中有一个name字段,一个computer对象,computer对象中有cpu和ram两个字段,如下:
package
ch2.example1;
public class User ... {
private String name;
private Computer pc;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public Computer getPc() ...{
return pc;
}
public void setPc(Computer pc) ...{
this.pc = pc;
}
}
package ch2.example1;
public class Computer ... {
private String cpu;
private String ram;
public String getCpu() ...{
return cpu;
}
public void setCpu(String cpu) ...{
this.cpu = cpu;
}
public String getRam() ...{
return ram;
}
public void setRam(String ram) ...{
this.ram = ram;
}
}
public class User ... {
private String name;
private Computer pc;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public Computer getPc() ...{
return pc;
}
public void setPc(Computer pc) ...{
this.pc = pc;
}
}
package ch2.example1;
public class Computer ... {
private String cpu;
private String ram;
public String getCpu() ...{
return cpu;
}
public void setCpu(String cpu) ...{
this.cpu = cpu;
}
public String getRam() ...{
return ram;
}
public void setRam(String ram) ...{
this.ram = ram;
}
}
jsp表单页面:
<
ww:form
action
="showuser.dhtml"
>
< ww:textfield label ="请输入名称:" name ="user.name" />< br >
< ww:textfield label ="请输入pcu:" name ="user.pc.cpu" />< br >
< ww:textfield label ="请输如ram:" name ="user.pc.ram" />< br >
< input name ="submit" type ="submit" value ="submit" />
</ ww:form >
< ww:textfield label ="请输入名称:" name ="user.name" />< br >
< ww:textfield label ="请输入pcu:" name ="user.pc.cpu" />< br >
< ww:textfield label ="请输如ram:" name ="user.pc.ram" />< br >
< input name ="submit" type ="submit" value ="submit" />
</ ww:form >
action:
package
ch2.example1;
import java.util.Map;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
public class ShowUser extends ActionSupport implements Action ... {
private User user;
public User getUser() ...{
return user;
}
public void setUser(User user) ...{
this.user = user;
}
public String execute() throws Exception ...{
System.out.println(user.getName());
System.out.println(user.getPc().getCpu());
System.out.println(user.getPc().getRam());
return NONE;
}
}
import java.util.Map;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
public class ShowUser extends ActionSupport implements Action ... {
private User user;
public User getUser() ...{
return user;
}
public void setUser(User user) ...{
this.user = user;
}
public String execute() throws Exception ...{
System.out.println(user.getName());
System.out.println(user.getPc().getCpu());
System.out.println(user.getPc().getRam());
return NONE;
}
}
运行结果:
1
2
3
可以看到,使用webwork可以很容易在表单中处理类的继承关系,这是非常方便的一格特性