开发者学堂课程【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>
(2)point 代码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)pointactionimport 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>
(5)index<%@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>
运行结果
注:这种方式不符合数学逻辑
三、使用类型转换
(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,Class,toClass
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);
(4)xwork- 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()+")";
(2)xwork- conversion properties 配置文件cn.sxt.entity.Pointacn.sxt.converter.PointConverter
(3)Action 代码不変,struts.xml 配置不変
(4)jsp 页面
<form action= "point. action" method= "post"〉点:<input type="text" name= "point"/><br〉<input type= "submit" value="提交"/></form>
