Struts2 json和ajax集成不起作用

时间:2014-10-13 05:47:04

标签: struts2 struts2-jquery struts2-json-plugin

这里我尝试将Struts 2与JSON,jQuery和ajax集成它正在调用action类但我没有得到json响应。我在struts-xml中添加了所有json插件和json-defult

Ajax功能

function callajax() {
    alert(1);
    $.getJSON('edituser.action', function(data) {
        alert(data);
    });
}

行动类。

package com.hyva.accounts.master.useraccountsetup;

import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author ranjeeth.g
 */
public class MasterUserSetUpActionEdit extends ActionSupport {

    private String username = "ranjith";
    private String password = "1234";
    private String fullname = "Ranjith";
    private String emailaddress = "Ranjith";
    private String telephone = "8762232246";

    @Override
    public String execute() {
//        System.out.println("session = " + session.isOpen());
        return "success";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmailaddress() {
        return emailaddress;
    }

    public void setEmailaddress(String emailaddress) {
        this.emailaddress = emailaddress;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}

Struts的XML

<action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
   </action>

更新了代码;

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="accounts" namespace="/" extends="hibernate-default,json-default">    
        <action name="login" class="com.hyva.accounts.userlogin.LoginAction" >
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/index.jsp</result>
            <result name="error">/login.jsp</result> 
            <result name="input">/login.jsp</result>          
        </action> 

        <action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
        </action>

        <action name="userlist"  class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionList">
            <result name="success">/setup_useracount_list.jsp</result>
        </action>

        <action name="adduser"  class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpAction">
            <interceptor-ref name="jsonValidationWorkflowStack"/>
            <result name="input">/setup_useracount_list.jsp</result> 
            <result name="success">/setup_useracount_list.jsp</result>
        </action>
        <action name="countryList" method="list" class="com.hyva.accounts.master.country.CountryAction">
            <result name="success">/setup_country_list.jsp</result>
        </action>
        <action name="addCountry" method="saveOrUpdate" class="com.hyva.accounts.country.CountryAction">
            <result name="success" type="redirect">countryList</result>
        </action>         
    </package>
</struts>

如果我正在调用此网址

java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils
    org.apache.struts2.json.SerializationParams.<init>(SerializationParams.java:57)
    org.apache.struts2.json.JSONResult.writeToResponse(JSONResult.java:214)
    org.apache.struts2.json.JSONResult.execute(JSONResult.java:204)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:361)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)

2 个答案:

答案 0 :(得分:1)

1)根据here

中的struts2版本包含json

2)您的包裹应延伸json-default,如下所示

<package name="example1" extends="struts-default, json-default">
<action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
   </action>
</package>

3)检查您对json结果的操作,例如地址bas http://localhost:8080/edituser

中的操作名称

它将显示json对浏览器的响应。

答案 1 :(得分:0)

包括Apache的commons-lang-2.x.jar

然后,由于2.1.8发生了重大变化,请注意将2.1.6与2.1.8混合使用非常,非常糟糕

对所有struts库使用相同的版本。 Struts-core,Struts-json,其他如果有的话(struts-spring-plugin,struts-config-browser-plugin,struts-junit-plugin等)。除struts-jquery-plugin,frremarker和ognl之外的所有内容(必须根据Struts dependecies page中指定的正确版本选择btw。)

然后,由于安全问题(以及其他原因),尝试升级到最新版本(2.3.16.3)是根本性的。如果您采用这种方式,那么请同时包含Apache的commons-lang-2.x.jar和Apache的commons-lang3-3.x.jar(如果只有后者不够,因为遗留代码)。由于他们处于不同的包装中,因此可以共同使用。