简体   繁体   中英

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.

struts2-jquery plugin is the answer for me. Thank you for all the support guys.

As for the answer do the following changes in index. jsp that's it.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <sj:head jquerytheme="start" jqueryui="true"/>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <h1> this is index page!!</h1>

        <s:form action="firstAction" id="nform" name="nform">
            <s:textfield name="username" id="username" label="Enter ur name"/>
            <sj:submit button="true" targets="ndiv"/>
        </s:form>

        <div id="ndiv">
            initial data
        </div>

    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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