struts2框架转换器使用方法

最灵活最好用的转换器使用方法(有点夸张了(⊙o⊙)?):
jsp页面写法(随便写的示例):
<body>
<form action="convertion" method="post">
姓名:<input type="text" name="user.name"/><br>
生日:<input type="text" name="user.birthday"/><br>
年龄:<input type="text" name="user.age"/><br>
身高:<input type="text" name="user.height"/><br>
住址:<select name="user.address.province">
<option value="广东">广东</option>
<option value="江西">江西</option>
</select>省
<select name="user.address.city">
<option value=广州>广州</option>
<option value="南昌">南昌</option>
</select>市
<input type="text" name="user.address.town"/>区
<br>
<input type="submit" value="提交信息"/>
</form><br>

注意:省市区那里的user.不能省,省了就只能把值封装到address,而封装不到user里面去了


java代码:
package com.gzsxt.struts.po;

import java.util.Date;


public class User {
private String name;
private String password;
private String phone;
private String username;
private Date birthday;
private Integer age;
private double height;
private Address address;

public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User(String name, String password, String phone, String username, Date birthday, Integer age, double height,
Address address) {
super();
this.name = name;
this.password = password;
this.phone = phone;
this.username = username;
this.birthday = birthday;
this.age = age;
this.height = height;
this.address = address;
}
public User() {
}
public User(String name, String password, String phone, String username) {
super();
this.name = name;
this.password = password;
this.phone = phone;
this.username = username;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User [name=" + name + ", phone=" + phone + ", username=" + username
+ ", birthday=" + birthday + ", age=" + age + ", height=" + height + ", address=" + address + "]";
}


}

package com.gzsxt.struts.po;


public class Address {
private String province;
private String city;
private String town;

public Address() {

}


@Override
public String toString() {
return province + city +  town;
}


public Address(String province, String city, String town) {
super();
this.province = province;
this.city = city;
this.town = town;
}


public String getProvince() {
return province;
}


public void setProvince(String province) {
this.province = province;
}


public String getCity() {
return city;
}


public void setCity(String city) {
this.city = city;
}


public String getTown() {
return town;
}


public void setTown(String town) {
this.town = town;
}


}

package com.gzsxt.struts.action;

import java.util.Map;


import org.apache.struts2.util.StrutsTypeConverter;


import com.gzsxt.struts.po.Address;


public class AddressConversion extends StrutsTypeConverter{


@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
System.out.println("这里是AddressConversion");
Address address=new Address();
for (String string : arg1) {
System.out.println(string);
}
address.setProvince(arg1[0]);
address.setCity(arg1[1]);
address.setTown(arg1[2]);
return address;
}


@Override
public String convertToString(Map arg0, Object arg1) {
Address address=(Address)arg1 ;
return address.toString();
}


}

package com.gzsxt.struts.conversion;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;


public class DateConversion extends StrutsTypeConverter{


@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
System.out.println("dateConversion");
for (String string : arg1) {
System.out.println(string);
}
System.out.println(arg2.getName());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
try {
Date date=sdf.parse(arg1[0]);
System.out.println(date);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}


@Override
public String convertToString(Map arg0, Object arg1) {
System.out.println(arg1.toString());
Date date=(Date)arg1;
return date.toString();
}

}

package com.gzsxt.struts.action;

import com.gzsxt.struts.po.Address;
import com.gzsxt.struts.po.User;
import com.opensymphony.xwork2.ActionSupport;


public class ConvertionAction extends ActionSupport {
private User user;
@Override
public String execute(){
System.out.println(user);
return SUCCESS;
}

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

}

配置文件:xwork-conversion.properties(全局)
com.gzsxt.struts.po.User=com.gzsxt.struts.action.UserConversion
com.gzsxt.struts.po.Address=com.gzsxt.struts.action.AddressConversion
java.util.Date=com.gzsxt.struts.conversion.DateConversion

转换结果:

控制台:

dateConversion
1996/08/22
java.util.Date
Thu Aug 22 00:00:00 CST 1996
User [name=龚旭伦, phone=null, username=null, birthday=Thu Aug 22 00:00:00 CST 1996, age=21, height=165.0, address=广东广州白云]
null

总结:从上面结果中可以看出,服务器能正常拿到所有的参数值并自动封装进对象中。但是,参数的自动封装中struts框架没有调用自定义的Address转换器,只调用了Date转换器,那么user中的address值又是怎样来的呢?总结原因如下:

1.首先页面传递参数进来ConvertionAction,struts框架通过反射机制自动封装参数,所有的封装动态参数操作都是一个叫params的拦截器封装的,详细如下(属性驱动):
   * struts2框架利用反射来获取子类(ConvertionAction)中的属性列表和对应的实体对象 
     * 使用request.getParameterNames()方法来获取到表单中的name列表 
     * 当属性名称与表单name相同时就把表单中的内容通过反射设置到属性中去 
2.然后利用反射调用execute方法获取得到url地址,然后根据url地址来进行对应的请求转发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值