学習struts 2タイプ変換まとめ2
19535 ワード
1タイプ変換はstruts 2を使用して良い変換クラスを提供することが望ましい:StrutsType Converterクラス
StrutsType Converterは抽象クラスです(struts 2-core-2.3.4.1.jarのorg.apache.struts 2.utilに存在するStrutsType Converter.class)
2.1つの変換器で複数のタイプの変換、一括変換を実現
intput.jsp
フォームラベルのnameがpointのlabelの値はpointです.
以下、Point Actionを記述する.JAvaは集合を利用して複数のポイントを集合する
次の2つの作成PointConverter 3.JAva変換クラス
StrutsType Converterは抽象クラスです(struts 2-core-2.3.4.1.jarのorg.apache.struts 2.utilに存在するStrutsType Converter.class)
- package com.test.converter;
-
- import java.util.Map;
-
- import org.apache.struts2.util.StrutsTypeConverter;
-
- import com.test.bean.Point;
-
- public class PointConverter2 extends StrutsTypeConverter {
-
- @Override
- public Object convertFromString(Map arg0, String[] values, Class toClass) {
- // TODO Auto-generated method stub
- Point point =new Point();
- String value=values[0];
-
- String[] result=value.split(",");
-
- point.setX(Integer.parseInt(result[0]));
- point.setY(Integer.parseInt(result[1]));
-
- return point;
- }
-
- @Override
- public String convertToString(Map map, Object o) {
- // TODO Auto-generated method stub
- Point point =(Point) o;
-
- int x=point.getX();
- int y=point.getY();
-
- String result="x:" +x+"y:"+y;
- return result;
- }
-
- }
2.1つの変換器で複数のタイプの変換、一括変換を実現
intput.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="point" label="point"></s:textfield>
- <s:textfield name="point" label="point"></s:textfield>
- <s:textfield name="point" label="point"></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>
フォームラベルのnameがpointのlabelの値はpointです.
以下、Point Actionを記述する.JAvaは集合を利用して複数のポイントを集合する
- package com.test.action;
- import java.util.Date;
- import java.util.List;
-
- 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;
-
- private List<Point> list;
-
-
-
- // 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 List<Point> getList() {
- return list;
- }
- public void setList(List<Point> list) {
- this.list = list;
- }
-
- 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";
- }
-
-
- }
次の2つの作成PointConverter 3.JAva変換クラス