@Override
public class ShopAction extends ActionSupport {
public String execute(){
try {
shopService.addShop(shop);
return result = SUCCESS;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
}
return result = SUCCESS;
}

public String showShop() throws Exception{
shop = shopService.getShopDAO().findById(shopId);

return SUCCESS;
}

public String getShopList(){
if (page == null) {
System.out.println("page is null!!!!!!");
return ERROR;
}
if(shop == null){
System.out.println("shop is null");
}
page = shopService.getShopDAO().getShopListByTags(shop.getType(), shop.getArea(), page);
return SUCCESS;
}
}
struts.xml<package name="ajax" namespace="/" extends="json-default">
<interceptors>
<interceptor-stack name="myJson">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myJson"/>
<action name="addShop" class="ShopAction">
<result type="json"></result>
</action>
</package>$(document).ready(function(){
$('#submit').click(function(){
var params = {
'shop':{
'name':'噶',
'address':'haha',
'area':'haha',
'type':'haha',
'briIntro':'ahha',
'phone':'027-12345678'
}
};
params = JSON.stringify(params);
submit(params);
});
});function submit(params){
$.ajax({
type:"post",
dataType:"text",
data:params,
url:"addShop.action",
contentType:"application/json",
success:function(json){
alert(json);
var obj = $.parseJSON(json);
var result = obj.result;
alert(result);
},
error:function(){
alert("error");
}
});
}
js提交ajax请求到后台之后
控制台输出这样的结果:
page is null!!!!!!很明显服务端在执行execute()之后又执行了getShopList()
为什么会这样?怎么样才能让他不执行getShopList()AjaxStrutsJSON

解决方案 »

  1.   

    估计你那个ajax 同时触发了 表单提交的请求。。
      

  2.   

    return result = SUCCESS;为什么这样写?
    可以在点击提交的时候,配置你要访问哪个方法。
      

  3.   

    到我的博客下载那个项目实例吧,,,里面都是用struts+ajax应用
      

  4.   


    因为result是我要返回到ajax的属性,
    其实是这样
    result = SUCCESS;
    return SUCCESS;我已近在struts.xml里面配置了啊,默认访问的是execute()方法啊
      

  5.   

    我已经找到答案了
    在struts.xml里面我们配置了<action name="addShop" class="ShopAction">
                <result type="json"></result>
            </action>所以在执行action之后struts会对action中的属性进行序列化,返回json
    这个序列话是默认对所有get方法进行执行,所以我的getShopList方法会在最后的时候被执行解决方案有两个,一个是命名方法的时候尽量避免使用get开头
    第二个是在struts.xml中给result中加一个includeProperties属性,使得struts只序列化你执行的属性,只执行对应属性的get方法,例如我的配置是<action name="addShop" class="ShopAction">
        <result type="json">
            <param name="includeProperties>result</param>
        </result>
    </action>
      

  6.   

    光从贴出来的这点内容看,没看出会去调用getShopList,和拦截器有关?
      

  7.   

    getShopList,换个名字,不要get开头,struts+ajax他会自动把你所有get开头的返回值封装到json中,所以你的get执行了,你的方法不要get开头就行,如果你硬是要get开头请加上@JSON(serialize=false)
      

  8.   

    我晕,这还真没试过,json响应你可以指定下具体的返回熟悉嘛,结果Action你一个output熟悉都没定义,result元素也没指定返回属性;
    你那个struts-xml配置package不是extends了json-default嘛为嘛还要指定调用json拦截器呢,直接就可以用了嘛,json-default不是去extends了default-stack,难道你的jar包有点变态
      

  9.   

    <action name="xxxAction" class="xxxxx" method="execute">试试