how to use dojo in struts2 submit form data to action display the result in same jsp page

I am trying to submit a form and display the output in the same page.
I am using struts2 and its dojo plugin to solve my need. I was able to display the result in the same page but the action is called automatically once again after the button click action occurs. Hence it will overwrite the output that I just got from the submission of the form.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<sx:head/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
    function enqDetails(){
        alert("ok");
        //document.firstForm.action = 'firstAction';
        //document.firstForm.submit();  
        <s:url id="urlTestAction" action="firstAction"/>
        dojo.event.topic.publish("show_detail");
        document.getElementById('sxDivId1').style.display = 'block';
    }
</script>
</head>
<body>
    <h1> this is index page!!</h1>
    <s:form id="firstForm" name="firstForm">
            <s:textfield name="username" label="enter username"/>
            <s:submit onclick="enqDetails()"></s:submit>
    </s:form>
    <sx:div id="sxDivId1" href="%{urlTestAction}" listenTopics="show_detail" formId="firstForm" loadingText="Please Wait...." showLoadingText="true" cssStyle="display:none;">
    </sx:div> 

</body>
</html>

struts.xml

  <action name="firstAction" class="action.FirstAction" method="first">
    <result name="success">/second.jsp</result>
    <result name="fail">/third.jsp</result>
  </action>
</package>

FirstAction.jsp

package action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FirstAction extends ActionSupport{

    private String username;

    public String first(){
        System.out.println("the username is :"+username);
        if(username.equals("a")){
            return "success";
        }else{
            return "fail";
        }
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

Following is displayed in console. After the action is called with entered text field again the action is called with empty value hence overwriting my output.

output

the username is :a
Jul 30, 2015 12:19:36 PM org.apache.struts2.components.ServletUrlRenderer warn
WARNING: No configuration found for the specified action: '/' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
Jul 30, 2015 12:19:36 PM org.apache.struts2.components.ServletUrlRenderer warn
WARNING: No configuration found for the specified action: '/' in namespace: ''. Form action defaulting to 'action' attribute's literal value.


Source: jsp

Leave a Reply