调用Struts 2.0动作方法而不实际调用

时间:2014-05-14 06:09:36

标签: java ajax jsp struts2

在Struts动作中发现一个奇怪的问题, 场景如下,

当我在employees.jsp中时,它只显示从服务器获取的员工列表,当点击员工div时,我将在javascript方法中创建一个动态表单“employeeDetailsForm”,该方法具有一个隐藏的输入字段员工代码的价值,

当点击员工div时,我将一个动作附加到表单“getEmployeeDetails” 它将提交操作并导航到employeeDetails.jsp,

在employeeDetails.jsp中,我将显示该特定员工的一组详细信息。 为了他的成就列表,我有了投票功能。 所以当我点击upVote时,在js方法中使用ajax调用upVote动作。 当upVote动作成功时,立即再次调用“getEmployeeDetails”动作,这是一种奇怪的行为。

由于显示了许多系统定义的方法,我无法回溯调用堆栈,

我只是交叉检查我的“employeeDetailsForm”是否存在于第二个jsp页面中,以及某个表单是如何在某处提交的?

但是当我在浏览器中检查检查器时,那个表单元素根本就不存在, 我还通过调用返回null的document.getElementById("employeeDetailsFormId")来检查

然后从这个方法被调用的地方?

如何追踪此问题?

更新 - 参考源代码

employees.jsp中的

- 提交表单以获取详细信息的JS方法

var empDetForm = document.createElement("form");
empDetForm.id=“empDetailsId”;
empDetForm.name=“employeeDetailsForm";
empDetForm.method="POST";
empDetForm.action="getEmployeeDetails";

var empCodeinput = document.createElement("input");
empCodeinput.setAttribute("type","hidden");
empCodeinput.id= empCode;
empCodeinput.name=“empCode”;

empDetForm.appendChild(empCodeinput);

empDetForm.submit();

在employeeDetails.jsp中 - 使用ajax调用upvote服务的JS方法

var formdata = "isUpVote="+isUpVote+”&”+”selectedAchievement="+achievementId;
    $.ajax({
              type: 'POST',      
              url: ‘achievementUpVote’,
            contentType: "application/x-www-form-urlencoded",
              async: false,
              //processData:false,
              data :formdata, 
              mimeType:"multipart/form-data",
              cache: false,
              processData:false,
              datatype: "json",
              success: function(response) {
                  alert("success upvote for achievement”);
              },
              error: function(e) { 
                  alert("Fail upvote");
              }
        });

Java struts动作方法

public String getEmployeeDetails() throws JSONException {

        EmployeeDetailsIntf empDetailsImplObj = new EmployeeDetailsIml();
        JSONObject jsonInput = new JSONObject();

        String userName = ApplicationData.getUserName();
        String accessCode = ApplicationData.getAccessCode();

        jsonInput.put(“empId”, getEmpId());
        jsonInput.put("un", userName);
        jsonInput.put("ac", accessCode);

        status = empDetailsImplObj.callGetEmpDetailsService(jsonInput);

        if(status == true){
            return "SUCCESS";
        }
        return "FAIL";
    }

另一个动作

public String achievementRating() {
        String userName =   ApplicationData.getUserName();
        String accessCode = ApplicationData.getAccessCode();
        JSONObject jsonInputData = new JSONObject();
        try {
            jsonInputData.put("un", userName);
            jsonInputData.put("ac", accessCode);
            jsonInputData.put("aid", selectedAchivement);

            EmployeeDetailsIntf empDetailsImplObj = new EmployeeDetailsIml();

            boolean status = empDetailsImplObj.upVoteAchievement(jsonInputData, isUpVote);
            if (status) {
                return "RATE_ACHIEVEMENT_SUCCESS";
            }else{
                return "FAIL";
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return null ; 

    }

struts.xml中

<action name=“getEmployeeDetails” class="com.action.EmployeeDetailsAction"
    method="getEmployeeDetails">
    <result name="SUCCESS”>../empDetails/employeeDetails.jsp</result>
    <result name="FAIL">failure.jsp</result>
</action>
<action name="achievementUpVote" class="com.action.EmployeeDetailsAction"
    method="achievementRating">
    <result name="RATE_ACHIEVEMENT_SUCCESS" type="json"/>
    <result name="FAIL" type="json"/>
    <result name="FAIL">failure.jsp</result>
</action>

0 个答案:

没有答案
相关问题