Struts2 JasonInterceptor内容类型必须是'application / json'或'application / json-rpc'

时间:2012-03-29 11:06:30

标签: json struts2 autocomplete

任何人都可以告诉我在什么情况下我会在日志中得到以下错误?

  

Struts2 JasonInterceptor内容类型必须是'application / json'或'application / json-> RPC”。忽略内容类型为application / x-www-form-urlencoded

的请求

我还注意到在IE中它显示如下调试消息(不确定这2条消息是否相关):

  

DEBUG:请考虑使用mimetype text / json-comment-filtered来避免JSON端点的潜在>安全问题   DEBUG:[SyntaxError:语法错误]

我特意改变了我的s:form enctype属性,如下所示,仍然无法摆脱这条消息:

<s:form id="dealerForm" action="AjaxAutocompleterAction" 
enctype="text/json-comment-filtered">
</s:form>

这也是(再没有工作)

<s:form id="dealerForm" action="AjaxAutocompleterAction" 
enctype="application/json">
</s:form>

任何想法?

* ** * ** * ** * *** 更新 - 1 * ** * ** * ** * ** * ** * *

有关我正在使用的代码的更多信息如下:

我的 AjaxAutocompleter.jsp 包含链接的自动填充程序:

<s:form id="dealerForm" action="AjaxAutocompleterAction"
enctype="text/json-comment-filtered">
<sx:autocompleter id="dealer" name="dealer" searchType="substring"
    label="Dealer" list="dealerList" listKey="name" listValue="name"
    showDownArrow="false" valueNotifyTopics="/notifyBranch"
    errorNotifyTopics="/error" beforeNotifyTopics="/before"
    forceValidOption="true" loadMinimumCount="3" />
<sx:autocompleter id="branch" name="branch" searchType="substring"
    label="Branch" list="branchList" showDownArrow="false"
    listenTopics="/notifyBranch" formId="dealerForm"
    formFilter="function(paramName){return true;}"
    valueNotifyTopics="/notifyRep" beforeNotifyTopics="/before"
    afterNotifyTopics="/after" forceValidOption="true"
    loadMinimumCount="0" loadMinimumCount="3" />
<sx:autocompleter id="representative" name="representative"
    searchType="substring" label="Rep" list="repList"
    showDownArrow="false" forceValidOption="true" loadMinimumCount="0"
    formId="dealerForm" formFilter="function(paramName){return true;}"
    listenTopics="/notifyRep" beforeNotifyTopics="/before"
    afterNotifyTopics="/after" loadMinimumCount="3" />
<textarea name="mytextarea" id="mytextarea" rows="25" cols="190"></textarea>
</s:form>

的struts.xml

<package name="ajax" extends="json-default">
    <action name="AjaxAutocompleterAction" class="com.frk.gid.action.AjaxAutocompleterAction">
        <interceptor-ref name="json"/>
        <interceptor-ref name="params">
            <param name="ordered">true</param>
        </interceptor-ref>
        <interceptor-ref name="prepare" />
        <result type="json" />
        <result name="success">/AjaxAutocompleter.jsp</result>
    </action>
</package>

我的模型类

public class Representative {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class Branch {
    private String name;
    private List<Representative> representatives;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setRepresentatives(List<Representative> representatives) {
        this.representatives = representatives;
    }

    public List<Representative> getRepresentatives() {
        return representatives;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class Dealer {
    private String name;
    private List<Branch> branches;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setBranches(List<Branch> branches) {
        this.branches = branches;
    }

    public List<Branch> getBranches() {
        return branches;
    }

    @Override
    public String toString() {
        return name;
    }
}

我的准备方法

@Override
public void prepare() throws Exception {
    logger.info("Prepare Started ...");
    ServletActionContext.getResponse().setContentType(
            "text/json-comment-filtered");
    dealerList = new ArrayList<Dealer>();
    int branchCounter = 0;
    int repCounter = 0;

    for (int i = 0; i < 5; i++) {
        Dealer d = new Dealer();
        List<Branch> branches = new ArrayList<Branch>();
        for (int j = 0; j < 3; j++) {
            Branch b = new Branch();
            b.setName("BRANCH-" + branchCounter++);
            List<Representative> representatives = new ArrayList<Representative>();
            for (int k = 0; k < 2; k++) {
                Representative rep = new Representative();
                rep.setName("REP-" + repCounter++);
                representatives.add(rep);
            }
            b.setRepresentatives(representatives);
            branches.add(b);
        }
        d.setName("DEALER-" + i);
        d.setBranches(branches);
        dealerList.add(d);
        if (this.dealer == null && i == 0) {
            setDealer(d.getName());
        }

        // Populate DBR Hierarchy for the selected dealer.
        if (this.dealer != null && this.dealer.equals(d.getName())) {
            branchList = new ArrayList<String>();
            int bCount = 0;
            for (Branch b : branches) {
                branchList.add(b.getName());
                if (this.branch == null && bCount++ == 0) {
                    setBranch(b.getName());
                }
                if (this.branch != null && this.branch.equals(b.getName())) {
                    repList = new ArrayList<String>();
                    for (Representative r : b.getRepresentatives()) {
                        repList.add(r.getName());
                    }
                    if ((this.representative == null && repList.size() > 0)
                            || (this.representative != null && !repList
                                    .contains(this.representative))) {
                        setRepresentative(repList.get(0));
                    }
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

好的..终于经过多次努力,我确实设法自己解决了这个问题。线索在于我的自动完成定义,每当我使用href时,我总是必须使用dataFieldName。

另外,我无法加载第一级层次结构(在我的案例中为Dealer)以使用href -i必须显式调用该操作并使用局部变量。

我发布了修改后的代码,因此它可以为其他学习者派上用场:-)玩得开心!!!

我更新的jsp

<s:url id="dbrList" action="AjaxAutocompleterAction" 
encode="text/json-comment-filtered"/>
<s:action name="AjaxAutocompleterAction" id="myDBRAction" />
<sx:autocompleter id="dealerList" name="dealer" searchType="substring"
    label="Dealer" showDownArrow="false" valueNotifyTopics="/notifyBranch"
    errorNotifyTopics="/error" beforeNotifyTopics="/before"
    forceValidOption="true" loadMinimumCount="1"
    list="%{#myDBRAction.dealerList}" />

<sx:autocompleter id="branchList" name="branch" searchType="substring"
    label="Branch" list="branchList" showDownArrow="false"
    listenTopics="/notifyBranch" formId="dealerForm"
    formFilter="function(paramName){return true;}"
    valueNotifyTopics="/notifyRep" beforeNotifyTopics="/before"
    afterNotifyTopics="/after" errorNotifyTopics="/error"
    forceValidOption="true" loadMinimumCount="1" href="%{dbrList}"
    list="branchList" dataFieldName="branchList" />
<sx:autocompleter id="repList" name="representative"
    searchType="substring" label="Rep" list="repList"
    showDownArrow="false" forceValidOption="true" formId="dealerForm"
    formFilter="function(paramName){return true;}"
    listenTopics="/notifyRep" beforeNotifyTopics="/before"
    errorNotifyTopics="/error" afterNotifyTopics="/after"
    loadMinimumCount="1" href="%{dbrList}" dataFieldName="repList" />

我更新的struts.xml

<package name="ajax" extends="json-default">
    <result-types>
        <result-type name="json"     class="org.apache.struts2.json.JSONResult" />
    </result-types>
    <action name="AjaxAutocompleterAction" class="com.frk.gid.action.AjaxAutocompleterAction">
        <interceptor-ref name="json">
            <param name="contentType">application/json</param>
        </interceptor-ref>
        <interceptor-ref name="jsonValidation" />
        <interceptor-ref name="params">
            <param name="ordered">true</param>
        </interceptor-ref>
        <interceptor-ref name="prepare" />
        <result name="success" type="json">
            <param name="contentType">text/html</param>
        </result>
    </action>
</package>