You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Siva Gurusamy <si...@ivycomptech.com> on 2007/02/01 11:36:42 UTC

Ajax Form Submit

HI,

I am having a problem in using ajax in Struts 2 Application.

Can someone explain me how to resolve this issue.

I have a form and an ajax div in the page. On submit of the form I wanted to
reload ajax div. But result was, after doing submit form, 'undefined' text
was appeared in targets (resultDivId) for a second and then browsers url got
changed to form submit url. Hence, the result which needs to appear in
targets(resultDivId) appeared in entire browser.

My Code:

JSP:
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Ajax Examples</title>
    <!--// START SNIPPET: common-include--> 
    <s:head theme="ajax"/>
    <!--// END SNIPPET: common-include-->
</head>

<body>
				<div id="createResult"></div>

				<s:form id="createId" action="createajax"
namespace="/" method="POST" theme="ajax">
					 <s:textfield label="AccountName"
name="accountName"/>
					 <s:submit value="Save" name="Save"
resultDivId="createResult" notifyTopics="topicCreated" theme="ajax"
errorText="Error" showErrorTransportText="true"/>
					 <!--s:submit action="!cancel"
value="Cancel" name="Cancel"
					   onclick="form.onsubmit=null"/-->
				</s:form>
	
				<h1>Users</h1>
			    <s:div
            		id="userlists"
		            cssStyle="border: 200x solid blue"
        		    href="ajaxList.action"
		            theme="ajax"
					delay="1"
		            errorText="There was an fatal error"
        		    showErrorTransportText="true"
					listenTopics="topicCreated"
		            loadingText="Loading users...">Loading...
			    </s:div>

</body>
</html>

Struts.xml
    <package name="example" extends="struts-default" namespace="/">
		<interceptors>
			<interceptor-stack name="ajaxStack">
				<interceptor-ref name="createSession" />
				<interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servlet-config"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="debugging"/>
                <interceptor-ref name="model-driven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="static-params"/>
                <interceptor-ref name="params"/>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param
name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param
name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
			</interceptor-stack>

		</interceptors>

		<default-interceptor-ref name="defaultStack"/>

		<action name="createajax"
class="com.app.view.action.UserAction" method="create">
			<result type="freemarker">createerror.ftl</result>
			<interceptor-ref name="ajaxStack"/>
        </action>
</package>

UserAction.create method

	public String create()	{
		// This is just preparing the form prior to saving the new
user
		actionType = CREATE;
		System.out.println("--------> Going to create new user
(name)=" + accountName);
		User u = new User();
		u.setUserId(userId);
		u.setAccountName(accountName);

		try {
			userService.createUser(u);
		} catch (Exception e)	{
			e.printStackTrace();
			System.out.println("Adding error to
ActionError...");
			//addActionError(e.toString());
		}

		return SUCCESS;
	}

createerror.ftl:
<@s.actionerror />
<@s.fielderror />


Thanks
siva





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Ajax Form Submit

Posted by Musachy Barroso <mb...@wfscorp.com>.
Try using "targets" instead of "resultDivId", which is deprecated, but 
should work anyway. Are there any javascript errors on the page when 
this happens?

regards
musachy

 Siva Gurusamy wrote:
> HI,
>
> I am having a problem in using ajax in Struts 2 Application.
>
> Can someone explain me how to resolve this issue.
>
> I have a form and an ajax div in the page. On submit of the form I wanted to
> reload ajax div. But result was, after doing submit form, 'undefined' text
> was appeared in targets (resultDivId) for a second and then browsers url got
> changed to form submit url. Hence, the result which needs to appear in
> targets(resultDivId) appeared in entire browser.
>
> My Code:
>
> JSP:
> <%@taglib prefix="s" uri="/struts-tags" %>
> <html>
> <head>
>     <title>Ajax Examples</title>
>     <!--// START SNIPPET: common-include--> 
>     <s:head theme="ajax"/>
>     <!--// END SNIPPET: common-include-->
> </head>
>
> <body>
> 				<div id="createResult"></div>
>
> 				<s:form id="createId" action="createajax"
> namespace="/" method="POST" theme="ajax">
> 					 <s:textfield label="AccountName"
> name="accountName"/>
> 					 <s:submit value="Save" name="Save"
> resultDivId="createResult" notifyTopics="topicCreated" theme="ajax"
> errorText="Error" showErrorTransportText="true"/>
> 					 <!--s:submit action="!cancel"
> value="Cancel" name="Cancel"
> 					   onclick="form.onsubmit=null"/-->
> 				</s:form>
> 	
> 				<h1>Users</h1>
> 			    <s:div
>             		id="userlists"
> 		            cssStyle="border: 200x solid blue"
>         		    href="ajaxList.action"
> 		            theme="ajax"
> 					delay="1"
> 		            errorText="There was an fatal error"
>         		    showErrorTransportText="true"
> 					listenTopics="topicCreated"
> 		            loadingText="Loading users...">Loading...
> 			    </s:div>
>
> </body>
> </html>
>
> Struts.xml
>     <package name="example" extends="struts-default" namespace="/">
> 		<interceptors>
> 			<interceptor-stack name="ajaxStack">
> 				<interceptor-ref name="createSession" />
> 				<interceptor-ref name="exception"/>
>                 <interceptor-ref name="alias"/>
>                 <interceptor-ref name="servlet-config"/>
>                 <interceptor-ref name="prepare"/>
>                 <interceptor-ref name="i18n"/>
>                 <interceptor-ref name="chain"/>
>                 <interceptor-ref name="debugging"/>
>                 <interceptor-ref name="model-driven"/>
>                 <interceptor-ref name="fileUpload"/>
>                 <interceptor-ref name="static-params"/>
>                 <interceptor-ref name="params"/>
>                 <interceptor-ref name="conversionError"/>
>                 <interceptor-ref name="validation">
>                     <param
> name="excludeMethods">input,back,cancel,browse</param>
>                 </interceptor-ref>
>                 <interceptor-ref name="workflow">
>                     <param
> name="excludeMethods">input,back,cancel,browse</param>
>                 </interceptor-ref>
> 			</interceptor-stack>
>
> 		</interceptors>
>
> 		<default-interceptor-ref name="defaultStack"/>
>
> 		<action name="createajax"
> class="com.app.view.action.UserAction" method="create">
> 			<result type="freemarker">createerror.ftl</result>
> 			<interceptor-ref name="ajaxStack"/>
>         </action>
> </package>
>
> UserAction.create method
>
> 	public String create()	{
> 		// This is just preparing the form prior to saving the new
> user
> 		actionType = CREATE;
> 		System.out.println("--------> Going to create new user
> (name)=" + accountName);
> 		User u = new User();
> 		u.setUserId(userId);
> 		u.setAccountName(accountName);
>
> 		try {
> 			userService.createUser(u);
> 		} catch (Exception e)	{
> 			e.printStackTrace();
> 			System.out.println("Adding error to
> ActionError...");
> 			//addActionError(e.toString());
> 		}
>
> 		return SUCCESS;
> 	}
>
> createerror.ftl:
> <@s.actionerror />
> <@s.fielderror />
>
>
> Thanks
> siva
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org