struts的类型转换(一)内置的类型转换器

Web应用程序的交互都是建立在http之上的,互相传递的都是字符串,也就是说后台程序从客户端接受的都是字符或字符串,而在一个web应用中往往使用到包括int,string,date以及用户自己定义的类的各种各样类型的对象,因此再服务器端必须将用户传过来的字符串转化成对应的各种类型。

Struts2框架给我们提供了类型转换的能,对于原生数据类型以及String,Data等常见类型,struts2使用内置的类型转换器自动进行转换。

Struts2自动转换的类型:

1基本数据类型及其对应的包装类:char/Character,int/Integer,float/Float.long/long,double/Double,boolean/Boolean

2 日期类型,字符串类型:Date类型,String类型

3 数组类型:struts2会将request.getParameterValues(Stirng str)中返回的字符串数据中的每个字符串组成数组,其中的元素数组类型

4 集合类型:Collectionl类型和Set类型,Collection类型将request.getParameterValues(Stirng str)返回的字符串数据按Collectionl类型进行转换。Set与上述形似按Set(无序不允许重复)类型进行转换,只不过会去掉其中重复相等的数据。

5  Map类型:

XWork will automatically handle the most common type conversion for you. This includes support for converting to and from Strings for each of the following:

  • String
  • boolean / Boolean
  • char / Character
  • int / Integer, float / Float, long / Long, double / Double
  • dates - uses the SHORT format for the Locale associated with the current request
  • arrays - assuming the individual strings can be coverted to the individual items
  • collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created

struts2使用内置的类型转换器自动进行转换例子程序:

1 新建web项目struts2

2 导入包

3 配置文件web.xml






    

       struts2

        
             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        

    

    

       struts2

       /*

    


  

    index.jsp

  



4 编写测试文件,类RegisterAction.java

package com.Mycom.action;

import java.util.Date;

import com.sun.net.httpserver.Authenticator.Success;

public class RegisterAction {
	private String username;
	private String password;
	private int age;
	private String sex;
	private double height;
	private Date birthday;
	private boolean above18;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public boolean isAbove18() {
		return above18;
	}
	public void setAbove18(boolean above18) {
		this.above18 = above18;
	}
	
	public String execute(){
		if(username.equals("hello")&&password.equals("world"))
			return "success";
		return "input";
	}

5 编写测试jsp文件typeConversion.jsp,registerResult.jsp

  

    
username:
password:
age:
sex:male female
height:
birthday:
isAbove18:YES NO
submit:

registerResult.jsp

  

    username:${requestScope.username }
password:${requestScope.password }
age:${requestScope.age }
sex:${requestScope.sex }
height:${requestScope.height }
birthday:${requestScope.birthday }
isAbove18:${requestScope.above18 }

6 配置struts.xml


	
		
			registerResult.jsp
			TypeConversion.jsp
		

	




以上例子程序中可以看出struts2框架对这些原生数据String,Date类类型通过内建的类型转换器进行来了自动转换。对于用户传过来的其他的类型的数据该怎么处理了,有以下两种方法

你可能感兴趣的:(struts2)