网络编程

struts2的类型转换——Struts2第二讲

发布制作:admin  发布日期:2011/6/8

注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初学者。

第一步:(这一步和其他一样,这里从简)依旧是新建一个web project,命名为struts2,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。

第二步:将index.jsp改名为input.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。Input.jap的代码如下

lt;%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path= request.getContextPath();String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML4.01 Transitional//EN"><html>

lt;head>

lt;base href="<%=basePath%>">

lt;title>My JSP'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <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"> -->

lt;/head>

lt;body>

lt;h1><font color='red'>请输入坐标,用英文半角逗号隔开</font></h1>

lt;s:form action="pointconverter">

lt;s:textfield name="point1" label="point1"></s:textfield>

lt;s:textfield name="point2" label="point2"></s:textfield>

lt;s:textfield name="point3" label="point3"></s:textfield>

lt;s:submit name="submit"></s:submit>

lt;/s:form>

lt;/body></html>

该文件有两个要注意的地方

1.使用了struts2的标签库<%@ taglib prefix="s" uri="/struts-tags"%>

2.f注意form中的action属性

第三步:在src下新建包com.beam,其中定义point类 point.java代码如下:

ackage com.bean;

ublic class Point{

ublic int getX(){ return x; } public void setX(int x){ this.x= x; } public int getY(){ return y; } public void setY(int y){ this.y= y; } private int x; private int y;}

Action在src下新建包com.action其中新建类PointAction.java代码如下

ackage com.action;

import com.opensymphony.xwork2.ActionSupport;import com.bean.Point;public class PointAction extends ActionSupport{

ublic Point getPoint1(){ return point1; } public void setPoint1(Point point1){ this.point1= point1; } public Point getPoint2(){ return point2; } public void setPoint2(Point point2){ this.point2= point2; } public Point getPoint3(){ return point3; } public void setPoint3(Point point3){ this.point3= point3; }

ublic String execute() throws Exception { return SUCCESS; } private Point point1; private Point point2; private Point point3; }

第五步:配置struts.xml文件代码如下:

lt;?xml version="1.0" encoding="utf-8"?><!DOCTYPE struts PUBLIC

quot;-//Apache Software Foundation//DTD Struts Configuration2.0//EN"

quot;struts.apache.org/dtds/struts-2.0.dtd">

lt;struts>

lt;package name="struts2" extends="struts-default">

lt;action name="pointconverter" class="com.action.PointAction">

lt;result name="success">/output.jsp</result>

lt;result name="input">/input.jsp</result>

lt;/action>

lt;/package>

lt;/struts>

第六步:在WebRoot下新建视图output.jsp依旧运用struts2的标签库代码如下

lt;%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path= request.getContextPath();String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML4.01 Transitional//EN"><html>

lt;head>

lt;base href="<%=basePath%>">

lt;title>My JSP'output.jsp' starting page</title>

lt;meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <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"> -->

lt;/head>

lt;body>

oint1:<s:property value="point1"/><br>

oint2:<s:property value="point2"/><br>

oint3<s:property value="point3"/>

lt;/body></html>

第七步:类型转化器在src目录下新建com.converter包其中新建类PointConverter.java代码如下

ackage com.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;import com.bean.Point;public class PointConverter extends StrutsTypeConverter{

@Override public Object convertFromString(Map arg0, String[] arg1, Class arg2){ Point point= new Point(); String[] values= arg1[0].split(","); int x= Integer.parseInt( values[0].trim()); int y= Integer.parseInt( values[1].trim()); point.setX(x); point.setY(y); return point; }

@Override public String convertToString(Map arg0, Object arg1){

oint point=(Point) arg1; int x= point.getX(); int y= point.getY(); String result="<x="+x+", y="+y+">"; return result; }

}

第八步:使类型转化器和action中的对应point属性关联起来新建一个properties文件这里有两种方法:

第一种是在com.converter包中新建一个PointAction-conversion.properties文件代码如下:

oint1=com.converter.PointConverterpoint2=com.converter.PointConverterpoint3=com.converter.PointConverter

第二种:是在src目录下直接新建一个文件 xwork-conversion.properties代码如下

com.bean.Point=com.converter.PointConverter

把 lt; 这个字符换成 < 就可以