jjzjj

java - 错误 : Could not find action or result No result defined for action action. 部分和结果 {"col1":"col1","col2":"col2"}

coder 2024-03-05 原文

我没有从服务器收到 JSON 类型数据的响应。

我正在使用 JSON 插件。

jQuery( "#dialog-form" ).dialog({ 
    autoOpen: false,
    height: 500,
    width: 750,
    modal: true,
    buttons :{
        "Search" : function(){
            jQuery.ajax({type : 'POST',
            dataType : 'json',
             url : '<s:url action="part" method="finder" />',
         success : handledata})
        }
    }
});
var handledata = function(data)
{
    alert(data);
}

如果 dataType = 'json' 我没有得到任何响应,但是如果我没有提到任何 dataType,我会得到页面的 HTML 格式.

public String list(){
    JSONObject jo = new JSONObject();
    try {
        Iterator it = findList.iterator();
        while(it.hasNext()){
             SearchResult part = (SearchResult) it.next();
             jo.put("col1",part.getcol1());
             jo.put("col2",part.getcol2());
        }
        log.debug("--------->:"+jo.toString());
    } catch (Exception e) {
        log.error(e);
    }
    return jo.toString();
}

struts.xml:

<package name="default" namespace="/ajax" extends="json-default">
  <action name="finder" 
       class="action.Part" method="finder" name="finder">
       <result type="json" />
  </action>
</package>

JSP 页面:

<div id="dialog-form" >
    <form action="" id="channelfinder">
        <textarea id="products" name="prodnbr"<s:property value='prodNbr'/>   
    </form>
</div>

控制台错误:

org.apache.struts2.dispatcher.Dispatcher - Could not find action or result No result defined for action action.Part and result {"col1":"col1","col2":"col2"}

web.xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
     <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <display-name>/parts</display-name>
      <description>Parts List Web App</description>

    <filter>
          <filter-name>struts-cleanup</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
        </filter>

        <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
        </filter>

       <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.action</param-value>
        </init-param>
    </filter>


    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/errorPage.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/errorPage.jsp</location>
    </error-page>

  <!-- Spring -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  </web-app>

我没有获取 jQuery 成功的数据。 请纠正我,这里有什么问题吗?

最佳答案

关于Struts2-JSON插件

Struts2 JSON Plugin以特定方式工作:

The JSON plugin provides a "json" result type that serializes actions into JSON.

它将整个Action序列化成JSON,除了

  • transient 属性
  • 没有 Getter 的属性

如果你不想序列化整个Action,而只是你选择的一个对象,你可以指定一个Root Object:

Use the "root" attribute(OGNL expression) to specify the root object to be serialized.

可以在 struts.xml 中完成像这样:

<result type="json">
    <param name="root">
        objectToBeSerialized
    </param>
</result>

而 Action 应该有:

private CustomObject objectToBeSerialized;

public CustomObject getObjectToBeSerialized(){
    return this.objectToBeSerialized;
}

其中 CustomObject 可以是原始类型、字符串、数组等...

以这种方式使用它(它的构建方式),你可以返回 SUCCESSERROR像在任何其他 AJAX Struts2 Action 中一样,在不破坏框架约定的情况下,像任何其他字段一样从 AJAX jQuery 调用的回调函数访问序列化的 JSON 对象(如果使用 rootObject,var handledata = function(data) 的“数据”将是您的对象,否则就是你的 Action)。


关于您的具体案例

在您的情况下,假设您的对象结构如下所示

row1 [col1, col2], 
row2 [col1, col2], 
rowN [col1, col2]

您可以创建一个包含两列的对象列表:

值对象

public class MyRow implements Serializable {
    private static final long serialVersionUID = 1L;

    private String col1; 
    private String col2;

    // Getters
    public String getCol1(){ 
        return this.col1; 
    }
    public String getCol2(){ 
        return this.col2; 
    }
}

Action 类

public class PartAction implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private List<MyRow> rows;   

    // Getter
    public  List<MyRow> getRows() { 
        return this.rows; 
    } 

    public String finder() {
        String result = Action.SUCCESS;
        rows = new ArrayList<MyRow>();

        try {
            Iterator it = findList.iterator();
            while(it.hasNext()) {
                SearchResult part = (SearchResult) it.next();
                MyRow row = new MyRow();
                row.setCol1(part.getcol1());
                row.setCol2(part.getcol2());
                rows.add(row);
            }
        } catch (Exception e) {
            result = Action.ERROR;
            log.error(e);
        }
        return result;
    }  
} 

Struts.xml

<package name="default" namespace="/ajax" extends="json-default">
    <action name="finder" class="action.Part" method="finder" name="finder">
        <result type="json" >
            <param name="root">
                rows
            </param>
        </result>
  </action>
</package>

要在 AJAX 回调函数中测试它,只需使用 $.each :

var handledata = function(data) {
    $.each(data, function(index) {
        alert(data[index].col1);
        alert(data[index].col2);
    });     
}

当然你可以使用 List<List<String>>而不是自定义对象或您更喜欢的任何其他对象结构:这只是为了让您了解。

关于java - 错误 : Could not find action or result No result defined for action action. 部分和结果 {"col1":"col1","col2":"col2"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17093862/

有关java - 错误 : Could not find action or result No result defined for action action. 部分和结果 {"col1":"col1","col2":"col2"}的更多相关文章

随机推荐