1

I have form with submit button , when click button its forward to next page and getting all the data from backend. But based on new requirement, the page need to stay the same page, but backend flow should be same. So i did the button with Ajax call to get the backend data. But its not working as expected. Please check the below code.

<s:form action="product!list" id="searchForm" method="Post" onSubmit="FormSubmitHandler()">
<s:submit action="product" method="list" value="search" />  
</s:form>

Which I have trying to run the page.

<s:form action="product!list" id="searchForm" method="Post" onSubmit="FormSubmitHandler()">
    <s:submit id="search" />
    </s:form>

jQuery code:

$("#search").click(function(){
    jQuery.ajax({
     url : '<s:url action="product" method="list" />',
    success : function(data){alert(data)}
    
    });
    return false;
    });

Console error:

2013-07-01 09:18:02,318 DEBUG apache.struts2.codebehind.CodebehindUnknownHandler - Trying to locate unknown action template with extension .jsp in directory /
2013-07-01 09:18:02,318 DEBUG apache.struts2.codebehind.CodebehindUnknownHandler - Trying to locate unknown action template with extension .vm in directory /
2013-07-01 09:18:02,318 DEBUG apache.struts2.codebehind.CodebehindUnknownHandler - Trying to locate unknown action template with extension .ftl in directory /
2013-07-01 09:18:02,334 WARN            org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
There is no Action mapped for action name pricing. - [unknown location]
        at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
        at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:70)
        at org.apache.struts2.rest.RestActionProxyFactory.createActionProxy(RestActionProxyFactory.java:51)
        at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)
        at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
        at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
        at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
        at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
        at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3288)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
        at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
        at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

Action class:

@Results({@Result(name=pricing.action.PartAction.INPUT,type=ServletDispatcherResult.class,value="/product-search.jsp", params={"location", "/product-search.jsp"}),
@Result(name="success",type=JSONResult.class,value="",params={"root","findList"})

})

@Validation()
public class PartAction extends BaseAction implements Preparable, ServletResponseAware {

public String list(){
    try {
    buildHeaders();

} catch (Exception e) {
    log.error(e);
    super.addActionMessage("No prices were found that match your search criteria.  Please select different options and try again.");
    return SEARCH;
  }
  return LIST;
  }

BaseAction which implemented:

@ParentPackage(value="pricing")
public abstract class BaseAction extends ActionSupport implements ServletRequestAware, SessionAware {
 protected static final String LIST = "list"; 
public String execute() throws Exception {
     
    return SUCCESS;
  }

The above code is not calling the list() method in action class.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2444474
  • 623
  • 8
  • 21
  • 40
  • Anybody have idea about this issue ? – user2444474 Jul 01 '13 at 07:38
  • A couple of remarks: the url looks wrong. Instead it should be like '/product/save' Besides add a error handler in your jQuery code. Doing so you'll have a clue of what's going on. – roland Jul 01 '13 at 07:43
  • 2
    I can't really understand what you are asking :/ Please take 5 minutes to improve the English part of your question (the code seems fine). – Andrea Ligios Jul 01 '13 at 10:16
  • i dont think this is the right way to do this req. can you check struts ajax tags and/or struts2-jquery plugin. – Gnanz Jul 09 '13 at 13:38

1 Answers1

1

Try this script

<script type="text/JavaScript">
  $("#searchForm").submit(function(event) {
    event.preventDefault();
    var $form = $(this);
    var url = $form.attr('action');
    $.post(url).done(function(data) {
      alert(data);
    });
  });
</script>

and change the form something like

<s:url var="formUrl" action="product" method="list" />
<s:form action="%{#formUrl}" id="searchForm" method="POST">
  <s:submit/>
</s:form>

EDIT:

It couldn't be such error from some other code, not used by the code above. In the above code after submit the button the product action is called. Then you should add @Action(name="product") on the class

@Action(name="product")
@Results({
 @Result(name=INPUT,type=ServletDispatcherResult.class, value="/product-search.jsp"),
 @Result(name=SUCCESS,type=JSONResult.class,value="",params={"root","findList"})
})
@Validation()
public class PartAction extends BaseAction { //removed interfaces that aren't implemented

  private List<Object> findList = new List<Object>(); //you could use any type instead of Object

  public List<Object> getFindList(){ //this is needed to JSON result
    return findList;
  }

  public String list(){
    try {
      buildHeaders();    
    } catch (Exception e) {
      log.error(e);
      addActionMessage("No prices were found that match your search criteria.  Please select different options and try again."); //super is not needed use protected modifire
      return INPUT; //the result should be above
    }
    return SUCCESS; //the result should be above
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • what type of button need to be in form ? like or because you have mentioned form.attr('action'). – user2444474 Jul 01 '13 at 14:36
  • it doesn't matter, any of them. The form is an other tag above it. – Roman C Jul 01 '13 at 14:39
  • its not triggering the method...Your note : Struts with codebehind plugin. – user2444474 Jul 01 '13 at 14:57
  • I'm trying auto click the button when page loading. – user2444474 Jul 01 '13 at 14:58
  • when click button , its giving me error"org.apache.struts2.dispatcher.Dispatcher - Could not find action or result".Since this codebehind plugin , so action must be product!list..Hope this is the cause. – user2444474 Jul 01 '13 at 15:31
  • @user2444474 you should look at the HTML rendered in the browser and tell me what is the value in the form `action` attribute. – Roman C Jul 01 '13 at 15:34
  • sorry..nothing happening while rendering html page.Because of this error. – user2444474 Jul 01 '13 at 15:56
  • rendering html is done before you click on submit button, the error actually may happen under different conditions. You didn't post the stacktrace to show the error message and the cause of the error. Also post the action code that shows the action mapping that has been done on it, the result returned by the action. – Roman C Jul 01 '13 at 16:15
  • its triggering , but not running the actual code.pache.struts2.codebehind.CodebehindUnknownHandler - Trying to locate result with extension .jsp in directory / pache.struts2.codebehind.CodebehindUnknownHandler - Loaded template '/part-search.jsp' from servlet context. apache.struts2.dispatcher.ServletDispatcherResult - Forwarding to location /part-search.jsp – user2444474 Jul 02 '13 at 00:45
  • @RomanC Could you please help me with http://stackoverflow.com/questions/18684370/struts2-jquery-ajax-and-client-side-validatio-not-working-toghether - similar issue – Amol Ghotankar Sep 08 '13 at 21:34
  • @AmolGhotankar Didn't you find this solution helpful? – Roman C Sep 09 '13 at 07:05
  • @RomanC Yes this solution is working but client side and ajax validation together is not working, could you please help me with http://stackoverflow.com/questions/18684370/struts2-jquery-ajax-and-client-side-validatio-not-working-toghether – Amol Ghotankar Sep 09 '13 at 07:30