point: [x=0 , y=0] 得不到值(struts2类型转换,初学struts2)

point: [x=0 , y=0] 得不到值(struts2类型转换,初学struts2)
悬赏:5 发布时间:2009-02-17 提问人:guochongcan (初级程序员)
input.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
<s:form action="pointConverter" method="post">
<s:textfield name="point" label="point"></s:textfield><br>
<s:textfield name="age" label="age"></s:textfield><br>
<s:textfield name="username" label="username"></s:textfield><br>
<s:textfield name="birthday" label="birthday"></s:textfield><br>
<s:submit></s:submit>
</s:form>
</body>
</html>


output.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'output.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>
point: <s:property value="point"/><br>
age: <s:property value="age"/><br>
username: <s:property value="username"/><br>
birthday: <s:property value="birthday"/><br>
</body>
</html>


PointAction.java


package com.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.bean.Point;

public class PointAction extends ActionSupport{

private Point point;
private int age;
private String username;
private Date birthday;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
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;
}
@Override
public String execute() throws Exception {

return SUCCESS;
}


}


Point.java


package com.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;
}


}


PointConverter.java


package com.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;

import com.bean.Point;

public class PointConverter extends DefaultTypeConverter {

@Override
public Object convertValue(Map context, Object value, Class toType) {
if(Point.class == toType){

Point point = new Point();

String[] str = (String[])value;

String[] paramValues = str[0].split(",");

int x = Integer.parseInt(paramValues[0]);
int y = Integer.parseInt(paramValues[1]);

point.setX(x);
point.setY(y);

return point;

}
if(String.class == toType){

Point point = new Point();

int x = point.getX();
int y = point.getY();

String result = "[x=" + x + " , y=" +y + "]";
//System.out.println(result);
return result;
}
return null;
}


}


struts.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>

<action name="pointConverter" class="com.action.PointAction">
<result name="success">/output.jsp</result>
</action>
</package>
</struts>


PointAction-conversion.properties

point=com.converter.PointConverter


得到的结果是

point: [x=0 , y=0]
age: 88
username: zhangsan
birthday: 08-11-11


point: [x=0 , y=0] 输入值了(34,23),为什么得不到值呢?请各位帮忙看看


自己找到答案了,再写在这后面。

找到了
PointConverter.java 里面

if(String.class == toType){

Point point = new Point();

又new 了一个Point
直接Point point = (Point)value;
就行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值