2012-03-29 34 views
0

任何人都可以在什么情况下启发我我会在日志中出现以下错误?Struts2 JasonInterceptor内容类型必须是'application/json'或'application/json-rpc'

Struts2 JasonInterceptor Content type must be 'application/json' or 'application/json-> rpc'. Ignoring request with content type application/x-www-form-urlencoded

我还注意到,在IE它示出了调试消息如下(不知道这些2个消息是相关的):

DEBUG: please consider using a mimetype of text/json-comment-filtered to avoid potential >security issues with JSON endpoints DEBUG: [SyntaxError: Syntax error]

我特别改变了S:形式enctype属性如下仍然想不出摆脱这个消息:

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

,这也(didnt再次工作)

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

有什么想法?

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

约我一起工作的代码

详细信息如下:

AjaxAutocompleter.jsp包含链接autocompleters:

<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)); 
        } 
       } 
      } 

     } 
    } 
+0

你能描述究竟你在做什么? – 2012-03-29 11:30:21

+0

我正在尝试一个struts2 **链接** autocompleter - 在autocompleter1中选择一个值时,值将在autocompleter2中刷新。网络中有一些可用的例子,但是由于上面描述的这个问题,无法让它们工作。 – Vikram 2012-03-29 13:07:26

+0

不知道为什么你在准备方法中做了这么繁重的工作,还有更多的不确定如何使用json interceptor ref stack,而同样的方法可以用于简单的方式。请参阅此处获取更多详细信息http://stackoverflow.com/questions/ 8886713/struts2-json-interceptor-not-populating-my-action-class – 2012-03-29 14:11:36

回答

0

好。最后多斗争,我还是设法解决这个自己。线索在于我的autocompleter定义,我必须在每次使用href时都使用dataFieldName。

此外,我无法加载第一级层次结构(在我的情况下是经销商)与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> 
相关问题