MOMOGONG 2022-09-16 20:29
浏览 9
已结题

IDEA struts2 局部转换 ognl.NoConversionPossible

  1. 问题背景:使用Idea学习struts进行数据类型转换(String->Point)时,产生ognl.NoConversionPossible 错误。
  1. 相关代码
  • 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>

  • web/index.jsp中
<%--
  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>
  • 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="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>
  1. 运行结果及报错内容
  • System.out.println(point+"还是不可以吗?");point打印为null

img

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

img

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

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已结题 9月16日
      • 修改了问题 9月16日
      • 创建了问题 9月16日

      悬赏问题

      • ¥15 simulink代码问题
      • ¥15 用51单片机普中开发板实现数字时钟
      • ¥15 有没有大佬能写一个按键LED灯交替闪烁的keil5里的编程
      • ¥15 用python解决excel文件读写
      • ¥15 分别用到哪些协议?如何实现的通信过程?
      • ¥30 nginx代理第三方接口
      • ¥20 求求了有没有人帮我看看怎么判断稳定性
      • ¥50 求解R语言的数据可视化问题
      • ¥15 两块fpga数据传输时钟同步问题
      • ¥15 pycharm 导入paddle模块后找不到enable_static属性