javajquerystruts2jquery-ajax

Struts action performed by jQuery ajax() Method


In my web-app, after a form validation, perform an Ajax request to my Struts action:

 $.ajax({
                url: "viewResult",
                type:"POST",
                data: "TEST",
                contentType: "application/json; charset=utf-8",
                success: function(res, textStatus, jqXHR) {

.....

Ajax request is successfully performed as well as my execute method in action so I would expect to be redirected to the specified page but this doesn't happen. Below my struts.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    
    <constant name="struts.custom.i18n.resources" value="package" />
    <constant name="struts.custom.i18n.resources" value="label" />
    
    <constant name="struts.action.extension" value="," />
    <constant name="struts.url.escapeAmp" value="false" />
    <constant name="struts.multipart.maxSize" value="50000000" />


    <package name="generic" extends="struts-default,json-default,tiles-default" namespace="/">
    <result-types>
    <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    <result-type name="redirect" class="org.apache.struts2.result.ServletDispatcherResult" />
    </result-types>

    <interceptors>
       <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
       <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
      <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>
            <interceptor name="httpHeaderInterceptor" class="comparatorProject.interceptor.HttpHeaderInterceptor"/>

            <interceptor-stack name="defaultInterceptor">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="annotationWorkflow"/>
                <interceptor-ref name="httpHeaderInterceptor"/>
                <interceptor-ref name="exception" />
                <interceptor-ref name="json">
                    <param name="enableSMD">true</param>
                </interceptor-ref>
            </interceptor-stack>

            <interceptor-stack name="defaultInterceptorExecuteAndWait">
                <interceptor-ref name="defaultInterceptor" />
                 <interceptor-ref name="execAndWait">
                    <!--  override delay and delaySleepInterval parameters of execAndWait to 500ms -->
                    <param name="delay">500</param>
                    <param name="delaySleepInterval">500</param>
                </interceptor-ref> 
            </interceptor-stack>
    
        </interceptors>

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

    </package>

    
    <package name="default" extends="struts-default,json-default,tiles-default" namespace="/">
     <action name="welcome" class="comparatorProject.navigator.actions.WelcomeAction" method="execute">
            <result name="success">jsp/welcome.jsp</result>           
        </action> 
    </package>

    <package name="comparatorProject" extends="struts-default" namespace="/">        
        
        <action name="searchPage">
            <result name="success">jsp/searchPage.jsp</result>
        </action>
          
         <action name="viewResult" class="comparatorProject.navigator.actions.ViewResultAction" method="execute" >
            <result name="success" >jsp/viewResult.jsp</result>
        </action>    
                
    </package>

</struts>

ViewResultAction .java

package comparatorProject.navigator.actions;

import com.opensymphony.xwork2.ActionSupport;

public class ViewResultAction  extends ActionSupport{

    private static final long serialVersionUID = 1L;
    
//  private String url;
    
    public String execute() {    
    
    
        
//      url = "/viewResult" ;
          return SUCCESS;
    }
    
    
//  public String getUrl()  {
//   
//      return url;
//   
//  }

}

and my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Comparator Project</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
    <!-- Restricts access to pure JSP files - access available only via Struts action -->
<security-constraint>
    <display-name>No direct JSP access</display-name>
    <web-resource-collection>
        <web-resource-name>No-JSP</web-resource-name>
        <url-pattern>*.jsp</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>no-users</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <description>Don't assign users to this role</description>
    <role-name>no-users</role-name>
</security-role>    
</web-app>

I've already tried to use:

 <action name="viewResult" class="comparatorProject.navigator.actions.ViewResultAction" method="execute" > <result name="success type="redirect" >${url}</result> </action> 

and

 <action name="viewResult" class="comparatorProject.navigator.actions.ViewResultAction" method="execute" >
 <result name="success" type="dispatcher"> 
<param name="location">jsp/viewResult.jsp</param>
 </result>
 </action> 

Solution

  • For googlers who have to do the same, Ajax jquery is not the right way, this is my solution: in your JSP file

    '<form action='"'yourAction'"' method='"'post'"' name='"formname"' novalidate>

    <button type='"submit" value='"submit"' class='"btn btn-primary"'>Cerca''

    in yours js file, create an eventlistner on submit event like this:

    document.addEventListener('submit', yourFunction, false);

    and in yourFunction add

    document.forms["formname"].submit(); //this send form to struts

    if your validation form or any other conditions are satisfied