Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Ajax Form Submit

siva

2007-02-01

Replies:

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:
<%@(protected)" %>
<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:
<@(protected) />
<@(protected) />


Thanks
siva





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.