类型转换 | 学习笔记

简介: 快速学习类型转换,介绍了类型转化系统机制, 以及在实际应用过程中如何使用。

开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:类型转换】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/537/detail/7312


类型转换


简介:

在 servlet 中如果表单提交非字符串数据的时候,需要进行类型转换
在 struts2中,常见数据类型 struts2已经自动的进行了类型转换无需程序猿进行手动转换在某些情况下,有自定义的类型时struts2 不能完成类型转换,那么需要手动转换,如果该自定义类型使用的频率较高时,手动转换重复代码将会增多---使用struts2提供的类型转换器来进行类型转换

内容介绍:

一.UserServlet 代码

二.坐标点 (不进行类型转换方式)

三.使用类型转换

四.坐标点(使用类型转换方式)


一.UserServlet 代码

案例:提交 age)

package cn.sxt.servlet;import java.io.IOException;public class UserServlet extends HttpServlet@Overrideprotected void service (HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException String strAge = req.getParameter("age");int age=0;if(strAge!=null)age= Integer.parseInt(strAge);


二、坐标点(不进行类型转换的方式)

(1)struts 代码

<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC//Apache Software Foundation//DTDStrutsConfiguration2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name= "default" namespace="/"extends="struts-default">

<action name= "point" class= "cn.sxt.action.PointAction"><result>/ index.jspk/result></action></package></struts>

2point 代码package cn.sxt.entity;public class Pointprivate int x;

Private  int y;public int getX() return x;public void setX(int x)

this.x =x;public int getY() return;public void setY(int y)

this.y =y;
(3)pointaction
import com.opensymphony.xwork2.Action;import cn.sxt.entity.Point;public class PointAction fprivate Point point;public String execute()System.out.println(point.getX()+"-----"+point.getY());

return Action.SUCCESS;public Point getPoint()return point;public void setPoint/Point point <package cn.sxt.actlon;public classPointAction

(4)pointer(页面)

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %>@<%Stringpath = request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+reque%><!DOCTYPE HTML PUBLIC"-//W3C//DTDHTML4.01Transitional//EN">.<html><head><base href="<%=basePath%>"><title>My JSP'index.jsp'starting page</title><meta http-equiv="pragma "content= "no- cache"><meta htto-eauiv="cache-controL"content="no-cache"><meta http-equiv="cache-control"content="no-cache"><meta http-equiv="expires"content="G"><meta http-equiv="keywords"content="keyword1,keyword2,keyword3"><meta http-equiv="description"content="This is my page"><link rel="stylesheet"type="text/css"href="styles.css"></head><body>

<form action="point.action" method="vost">点:x:<s:property value="point.x"/>br>

y:<s:property value="point.y"/>br>

<input type= "submit" value="提交"/></body></html>

5index<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %>@<%Stringpath = request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+reque%>.">My JSP'index.jsp'</span></code><code><span class="lake-fontsize-1515">starting pagecontent= "no- cache">

点:

x:br>

y:br>


运行结果

image.png

注:这种方式不符合数学逻辑


三、使用类型转换

(1)步骤

编写类型转换器.继承 StnutsTypeConverter 类

编写 xwork-conversion.properties 的配置文件,放于 src 下;内容为要转换的类型=类型转换器
2案例(pointconverter 代码)
import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class PointConverter extends Struts TypeConverter*context ActionContext
*values 要进行类型转换的字符串数组
*toClass 被转换的类型
@Overridepublic object convertFromString(context,String[] values,ClasstoClass

String value=values[0];System.out.println(value);

Point point = new Point();String x = value.substring(1,value.indexOf(","));String y=value.substring(value.indexOf(",")+1,value.length()-1);System.out.println("x="+x);System.out.println("y="+y);point.setX(Integer.parseInt(x));point.setY(Integer.parseInt(y));return point;@Overridereturn null;@Overridepublic String convertToString(Map context,Object o) return null;

3)StrutsTypeConverter.
public abstract class StrutsTypeConverter extends DefaultTypeConverter fpublic object convertValue(Map context,object o,Class toClass) if (toClass equals(String.class)) return convertToString( context,else if (o instanceof String[]) return convertF romString(context,(String[])o,toClass);else if (o instanceof String) return convertF romString(context, new String[lf(String)o],toClasreturn performFalbackConversion(context,o,toClass);Converts one or more String values to the specified class.@param context the action context@param valuestheString values to be converted,such as those submitt

@param toClass theclass toconvert to@return the converted objectpublic abstract Object convertF romString(Map context, String[]values, Class/***Converts the specified object to a String.*@param context the action context*@paramthe object to be converted@return the converted Stringpublic abstract String convertToString(Map context,object o);

4xwork- conversion properties 配置文件
cn.sxt.entity.Pointacn.sxt.converter.PointConverter

(5)运行结果

4,3

x:4

y:3

注:类型转换器起作用


四、坐标点(使用类型转换方式)

完整步骤

(1)类型转化器代码
public class PointConverter extendsStruts TypeConverter{/***将表单提交的字符串数据转换为指定自定义类型

*context是ActionContext*values要进行类型转换的字符串数组*toClass被转换的类型**/@Overridepublic Object convertFromString(Map context,String[] values,ClasstoClass) string value=values[e];Point point = new Point();String x=value.substring(1,value.indexOf(","));Stringy=value.substring(value.indexOf(",")+1,value.length()-1);System.out.println("x="+x);System.out.println("y="+y);point.setX(Integer.parseInt(x));point.setY(Integer.parseInt(y));return point;
* 将定义类型转换为字符串在前台页面显示--- -通过 ognl 表达式将会使用该方法进行转换* context---actionContext*要转换的对象@Overridepublic String convertToString(Map context,object o)

Point point = (Point)o;return"("+point.getX()+","+point.getY()+")";
2xwork- conversion properties 配置文件
cn.sxt.entity.Pointacn.sxt.converter.PointConverter
3Action 代码不変,struts.xml 配置不変
4jsp 页面

<form action= "point. action" method= "post"〉点:<input type="text" name= "point"/><br〉<input type= "submit" value="提交"/></form>

相关文章
|
5月前
|
Java
类型转换
Java中的类型转换是将一个数据类型转换为另一个数据类型的过程。在Java中,有两种类型转换:自动类型转换和强制类型转换。 自动类型转换是指Java自动将小的数据类型转换为大的数据类型,而不需要使用强制类型转换符。 强制类型转换是指Java使用强制类型转换符(如:())将一个数据类型转换为另一个数据类型。强制类型转换只能在两种数据类型之间进行转换,而且只能从大类型转换到小类型。如果从一个小类型转换到一个大类型,那么这个转换是自动进行的,不需要使用强制类型转换符。
24 0
|
6月前
|
存储 安全 编译器
类型转换(C++)
类型转换(C++)
49 0
|
1月前
|
C++
c++类型转换
c++类型转换
59 1
|
5月前
|
安全 编译器 C语言
|
8月前
|
程序员 编译器 C++
C++ 几种类型转换
C++ 几种类型转换
|
10月前
|
程序员
|
安全 程序员 C++
C++——类型转换
C++——类型转换
C++——类型转换
|
C语言 C++
【C++中的类型转换】C语言类型转换与C++类型转换对比,以及4种C++类型转换详解
【C++中的类型转换】C语言类型转换与C++类型转换对比,以及4种C++类型转换详解
255 0
【C++中的类型转换】C语言类型转换与C++类型转换对比,以及4种C++类型转换详解
|
Java
类型转换专题
将数据类型中,取值范围小的数据,给取值范围大的类型赋值,可以直接赋值。
106 0
类型转换专题
|
SQL 分布式计算 大数据
有类型转换_as | 学习笔记
快速学习有类型转换_as
52 0
有类型转换_as | 学习笔记