简体   繁体   English

无法从Struts2操作中的Jquery帖子提交值

[英]Unable to get value submitted from Jquery post in Struts2 action

I am new struts and I am not sure what i am missing here. 我是新人,我不确定我在这里缺少什么。 When I submit this form all the fields are null in the struts action class. 当我提交此表单时,struts动作类中的所有字段均为null。 I have added an interceptor "adminHomeInterceptor" to check whether the user is admin or not, This works fine when I remove that interceptor-ref. 我添加了一个拦截器“ adminHomeInterceptor”,以检查用户是否为admin。当我删除该拦截器-ref时,此方法工作正常。 But I have to check for all request whether the user is admin, so cant remove that interceptor. 但是我必须检查所有请求,以确定用户是否为管理员,因此无法删除该拦截器。 Any help would be deeply appreciated. 任何帮助将不胜感激。

Here is my jsp. 这是我的jsp。

    <s:form id="vendorRegister" action="addvendor" method="post" theme="simple">
        <label for="username" class="formLabel">Username/Email</label>
        <s:textfield name="userName" id="txtUname" cssClass="small ui-widget-content ui-corner-all" /><br />
        <label for="phone" class="formLabel">Phone</label>
        <s:textfield name="phone" id="txtPhone" cssClass="small ui-widget-content ui-corner-all" /><br />
        <label for="mobile" class="formLabel">Mobile</label>
        <s:textfield name="mobile" id="txtMobile" cssClass="small ui-widget-content ui-corner-all" /><br />
    <s:submit id="btnRegister" cssClass="button ui-state-default ui-corner-all" value="Add Vendor" />
</s:form>

This is a dynamically loaded form. 这是一个动态加载的表单。

$("#btnRegister").live("click",(function(event){
    event.preventDefault();
    var uname = $( "#txtUname" ),
    phone =  $("#txtPhone"),
    mobile = $("#txtMobile"),   
    if ( // Validation Codes ) {
        $("#btnRegister").attr("disabled", true);
        var url = "../addvendor";
        $.post( url , $("#vendorRegister").serialize(), function(data){
            alert("inside");
        });
    }
}));

My Strust2 Action Class 我的Strust2行动班

public class VendorAction extends ActionSupport{

    private int phone;
    private long mobile;
    private String userName;

    public int getPhone() {
         return phone;
    }
    public void setPhone(int phone) {
        this.phone = phone;
    }
    public long getMobile() {
        return mobile;
    }
    public void setMobile(long mobile) {
        this.mobile = mobile;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String recordVendor(){
        System.out.println(userName);
        System.out.println(mobile);
        return "success";
    }
}

Here is my struts.xml file 这是我的struts.xml文件

     <package name="admin" namespace="/admin" extends="json-default">
    <interceptors>
        <interceptor name="adminHomeInterceptor" class="org.admin.interceptors.AdminHomeInterceptor" />
    </interceptors>
    <action name="addvendor" class="org.vendor.action.VendorAction" method="recordVendor">
        <interceptor-ref name="adminHomeInterceptor" />
        <result type="json" /> 
    </action>
</package>

Are you using struts2-json plugin . 您是否正在使用struts2-json插件。 If you are using it . 如果您正在使用它。 You have to include 你必须包括

<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />  in struts.xml

and define result type json like this at package level 并在包级别定义结果类型json这样

<result-type name="json" class="org.apache.struts2.json.JSONResult" default="false" /> 

Otherewise getters/setters will not work with JSOn you are sending through json. 否则,getter / setter不能与JSOn一起使用,这是通过json发送的。

You have configured only one interceptor for your action , so no other interceptors are applied to the request. 仅为您的操作配置了一个拦截器 ,因此没有其他拦截器应用于该请求。

<action name="addvendor" class="org.vendor.action.VendorAction" method="recordVendor">
  <interceptor-ref name="adminHomeInterceptor" />
  <result type="json" /> 
</action>

Instead, you must create an interceptor stack and then reference that for your action (or all actions). 相反,您必须创建一个拦截器堆栈,然后为您的操作(或所有操作)引用该堆栈。

Example

<interceptors>
  <interceptor name="adminHomeInterceptor" class="org.admin.interceptors.AdminHomeInterceptor" />
  <interceptor-stack name="exampleDefaultStack">
    <interceptor-ref name="exception"/>
    <interceptor-ref name="adminHomeInterceptor"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params">
      <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
      <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
      <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>
  </interceptor-stack>
</interceptors>

Then change the interceptor-ref for your action to: 然后将您的操作的interceptor-ref更改为:

<interceptor-ref name="exampleDefaultStack"/>

Alternatively, you can apply this stack to all actions by default by including the following in the package section: 另外,默认情况下,您可以通过在package部分中包括以下内容,将此堆栈应用于所有操作:

<default-interceptor-ref name="exampleDefaultStack"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM