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

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_9" version="2.4"

    xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <filter>

       <filter-name>struts2</filter-name>

        <filter-class>
             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>

    </filter>

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>


  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

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

  <body>

    <form action="typeConvertor.action">

    username:<input type="text" name="username"/><br/>

    password:<input type="password" name="password"/><br/>

    age:<input type="text" name="age" /><br/>

    sex:male<input type="radio" name="sex" value="male"/>

        female<input type="radio" name="set" vlaue="female" /><br/>

    height:<input type="text" name="height" ><br/>

    birthday:<input type="text" name="birthday"/><br/>

    isAbove18:YES<input type="radio" name="above18" value="true"/>

              NO<input type="radio" name="above18" vlaue="false"/><br/>

    submit:<input type="submit" value="提交"><br/>

    </form>

  </body>

registerResult.jsp

  <body>

    username:${requestScope.username }<br/>

    password:${requestScope.password }<br/>

    age:${requestScope.age }<br/>

    sex:${requestScope.sex }<br/>

    height:${requestScope.height }<br/>

    birthday:${requestScope.birthday }<br/>

    isAbove18:${requestScope.above18 }<br/>

  </body>

6 配置struts.xml

<struts>
	<package name="dufault" extends="struts-default">
		<action name="typeConvertor" class="com.Mycom.action.RegisterAction">
			<result name="success">registerResult.jsp</result>
			<result name="input">TypeConversion.jsp</result>
		</action>

	</package>

</struts>


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值