这里以转换点坐标为例
1.首先建一个input.jsp文件
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <!-- struts 标签库的引入 -->
- <%@ taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'input.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">
- -->
- </head>
- <body>
- <h1>输入一个点的坐标,以逗号隔开</h1>
- <!-- --------- 这是普通方式进行表单输入
- <form action="" method="post">
- point: <input type="text" name="point" size="20"><br>
- username: <input type="test" name="username" size="20"><br>
- bithday: <input type="text" name="bithday"><br>
- date: <input type="text" name="date" size="20"> <br>
- <input type="submit" value="submit">
- </form>
- -->
- <!-- 用struts2 标签库的标签进行设置表单 -->
- <s:form action="converterAction.action" >
- <s:textfield name="point" label="point"></s:textfield>
- <s:textfield name="point2" label="point2"></s:textfield>
- <s:textfield name="username" label="username"></s:textfield>
- <!--
- <s:password name="password" label="password"></s:password>
- -->
- <s:textfield name="age" label="age"></s:textfield>
- <s:textfield name="birthday" label="birthday"></s:textfield>
- <s:submit name="submit" label="submit"></s:submit>
- <!--
- <s:reset name="resert" label="resert"></s:reset>
- -->
- </s:form>
- </body>
- </html>
2.定义一个action处理类名为PointAction.java
- package com.test.action;
- import java.util.Date;
- import com.opensymphony.xwork2.ActionSupport;
- import com.sun.org.apache.regexp.internal.recompile;
- import com.test.bean.Point;
- //要实现复杂功能,action都要继承 ActionSupport这个父类,所以以后尽量都要继承这个父类
- public class PointAction extends ActionSupport{
- private Point point;
- private Point point2;
- private int age;
- private String username;
- private Date birthday;
- public Point getPoint() {
- return point;
- }
- public void setPoint(Point point) {
- this.point = point;
- }
- public Point getPoint2() {
- return point2;
- }
- public void setPoint2(Point point2) {
- this.point2 = point2;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String execute() {
- return "success";
- }
- }
3.接下来配置struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts SYSTEM "struts-2.3.dtd">
- <!-- PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd" -->
- <struts>
- <package name="struts2" extends="struts-default">
- <action name="helloword" class="com.test.action.helloWorld">
- <result name="success">/helloworld.jsp</result>
- </action>
- <action name="logon" class="com.test.action.logonAction">
- <result name="success" >/result.jsp</result>
- </action>
- <action name="converterAction" class="com.test.action.PointAction">
- <result name="success">/output.jsp</result>
- </action>
- </package>
- </struts>
*****记住:该xml文件中action中name="converterAction"要与上面的input.jsp文件中action="converterAction.action" 名字相同,否则会出错。
4.接下来就是进行类型转换了,一般struts2类型转换都是与PoiontAction类分离
先建一个自定义类Point
- package com.test.bean;
- public class Point
- {
- private int x;
- private int y;
- public 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;
- }
- @Override
- public String toString()
- {
- String result = "x: " + x + " y: " + y;
- return result;
- }
- }
5.接下来定义转换处理类
- package com.test.converter;
- import java.util.Map;
- //import javax.persistence.criteria.CriteriaBuilder.In;
- import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
- import com.test.bean.Point;
- //该类要想使用,必须和struts2进行关联,必须继承一个父类
- //该父类存在于,struts2所添加的ognl包中,为DefaultTypeConverter类
- //并实现 convertValue(Map,Object,class)类
- public class PointConverter extends DefaultTypeConverter{
- @Override
- public Object convertValue(Map<String, Object> context, Object value,
- Class toType) {
- //这是两个方向,这是类方向
- if (Point.class==toType) {
- String[] str=(String[])value;
- String firstValue=str[0];
- String[] resultValue=firstValue.split(",");
- Point point=new Point();
- point.setX(Integer.parseInt(resultValue[0]));
- point.setY(Integer.parseInt(resultValue[1]));
- return point;
- }
- else if (String.class==toType) {
- // Point point=new Point();
- // int x=(int) point.getX();
- // int y=(int) point.getY();
- Point point=(Point)value;
- int x=(int)point.getX();
- int y=(int)point.getY();
- String result="x:"+x+"y:"+y;
- return result;
- }
- return null;
- }
- }
然后建一个file,就是配置文件PointAction-conversion.properties进行把转换处理类和PointAction类关联起来,每写一个这样的配置文件,名字都和action类的名字相同,后面(-conversion.properties)的不变
该配置文件内容如下:
- point=com.test.converter.PointConverter
- point2=com.test.converter.PointConverter
- #point=com.test.converter.PointConverter
其中,point为要转换的类型,与PointAction类中的point相同,=右面就是和Pointaction分离开的转换类了