- 问题背景:使用Idea学习struts进行数据类型转换(String->Point)时,产生ognl.NoConversionPossible 错误。
- 相关代码
- com/typetsf/action/TestAction.java中
package com.typetsf.action;
import com.opensymphony.xwork2.ActionSupport;
import java.awt.Point;
import java.util.Date;
import java.util.List;
public class TestAction extends ActionSupport {
private int age;
private String username;
private Date myDate;
private List<String> likes;
private Point point;
@Override
public String execute() throws Exception {
//System.out.println("("+point.getX()+","+point.getY()+")");
System.out.println(point+"还是不可以吗?");
return "success";
}
public TestAction() {
}
public TestAction(int age, String username, Date myDate, List<String> likes, Point point) {
this.age = age;
this.username = username;
this.myDate = myDate;
this.likes = likes;
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 getMyDate() {
return myDate;
}
public void setMyDate(Date myDate) {
this.myDate = myDate;
}
public List<String> getLikes() {
return likes;
}
public void setLikes(List<String> likes) {
this.likes = likes;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
@Override
public String toString() {
return "TestAction{" +
"age=" + age +
", username='" + username + '\'' +
", myDate=" + myDate +
", likes=" + likes +
", point=" + point +
'}';
}
}
- com/typetsf/action/TestAction-conversion.properties中
point=com.typetsf.convert.PointTypeConvert
- com/typetsf/convert/PointTypeConvert.java中
package com.typetsf.convert;
import org.apache.struts2.util.StrutsTypeConverter;
import java.awt.Point;
import java.util.Map;
/**
* 自定义的类型转换器PointTypeConvert
* 实现页面中输入的字符串x ,y 到Point(x,y)类型的转换
*/
//从String到Object的类型转换
public class PointTypeConvert extends StrutsTypeConverter {
/**
*自定义的类型转换器需要实现的方法--可以完成从string——x,y到object--Point类型
* @param context--strusts的上下文
* @param values -- 页面表单提交过来的数据
* @param type--要转换的类型
* @return
*/
public Object convertFromString(Map context, String[] values, Class type) {
Point point=new Point();
String str=values[0];
String strValue[]=str.split(",");
point.x=Integer.parseInt(strValue[0]);
point.y=Integer.parseInt(strValue[1]);
return point;
}
@Override
public String convertToString(Map map, Object o) {
return o.toString();
}
}
- web/pages/typetsf/show.jsp中
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: 戴尔
Date: 2022/9/16
Time: 9:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>类型转换器</h3>
<ol>
<li>
age:<s:property value="age"></s:property>
</li>
<li>
username:<s:property value="username"></s:property>
</li>
<li>
myDate:<s:date name="myDate" format="yyyy/mm/dd hh:mm:ss"></s:date>
</li>
<li>
likes:<s:property value="likes"></s:property>
</li>
<li>
point:<s:property value="point"></s:property>
</li>
</ol>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: ����
Date: 2022/9/13
Time: 13:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>魔仙堡歌曲</title>
</head>
<body>
<!--第六章 数据类型转换-->
<a href="${pageContext.request.contextPath}/test6/testAction.action?age=12&username=zhang&myDate=1999-08-09 12:22:12&likes=java&likes=net&p=(1,2)&p=(2,3)">类型转换器的应用</a>
</body>
</html>
<?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="in6" namespace="/test6" extends="struts-default">
<!--自定义类型转换器样例配置-->
<action name="testAction" class="com.typetsf.action.TestAction" method="execute" >
<result name="success">/pages/typetsf/show.jsp</result>
</action>
</package>
</struts>
- 运行结果及报错内容
- System.out.println(point+"还是不可以吗?");point打印为null

- 运行后:<s:property value="point"></>后显示ognl.NoConversionPossible

- 我的解答思路
好像是properties文件没用用到 - 我想要达到的结果
打印不为空,运行出point