Home Java javaTutorial Determine whether to call AJAX in Java's Struts and use interceptors to optimize it

Determine whether to call AJAX in Java's Struts and use interceptors to optimize it

Jan 09, 2017 pm 02:22 PM

Strut2 Determine whether it is an AJAX call
1. AJAX and traditional Form form
In fact, both are generally POST requests through HTTP. The difference is that after the browser submits the Form, it expects the server to return a complete HTML page. The AJAX call is issued by the XMLHttpRequest object (different browsers may be different). The browser expects the server to return an HTML fragment. Specifically, there are no requirements for JSON, XML, etc. How to use it after returning to the browser is also determined by the JS script itself.

2. Whether the request is AJAX
So for the server side, how to determine whether an HTTP request is an AJAX call? This requires looking at the HTTP Header.

We can judge by x-request-with in the Header. Although different browsers send AJAX requests to different objects, if jQuery is used to send AJAX requests, the identifier has been added when jQuery implements ajax internally. The jQuery source code looks like this: xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
So, if the front-end page of the project sends AJAX requests through jQuery, this judgment is safe.

The following is the Header information carried by the HTTP request.

Normal Form form submission

===MimeHeaders ===
accept = */*
referer =http://localhost:8080/user2/toQueryPage.action
accept-language = zh-CN
user-agent = Mozilla/4.0 (compatible; MSIE8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C;.NET4.0E)
accept-encoding = gzip, deflate
host = localhost:8080
connection = Keep-Alive
cache-control = no-cache
Copy after login

AJAX call (IE)

===MimeHeaders ===
x-requested-with = XMLHttpRequest
accept-language = zh-cn
referer =http://localhost:8080/user2/toQueryPage.action
accept = application/json, text/javascript,*/*; q=0.01
content-type =application/x-www-form-urlencoded
accept-encoding = gzip, deflate
user-agent = Mozilla/4.0 (compatible; MSIE8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C;.NET4.0E)
host = localhost:8080
content-length = 57
connection = Keep-Alive
cache-control = no-cache
Copy after login

##3. Obtained in Action HTTP request header
In the Action class, obtain the HttpServletRequest object through the ServletRequestAware interface, and then obtain the header information we want through the getHeader method.

public abstract class BaseAction
    <ParamVo extends BaseParamVo, ResultVo extends BaseResultVo>
      extends ActionSupport
        implements ServletRequestAware {
    
  private static final String AJAX_RESULT_NAME = "ajaxResult";
  private static final String XHR_OBJECT_NAME = "XMLHttpRequest";
  private static final String HEADER_REQUEST_WITH = "x-requested-with";
    
  /**
   * Request对象,用来判断请求是否是AJAX调用
   */
  private HttpServletRequest request;
    
  private ParamVo paramVo;
    
  private ResultVo resultVo;
    
  @Override
  public String execute() {
    String resultPage = SUCCESS;
    try {
      resultVo = doExecute(paramVo);
    }
    catch (BaseException e) {
      resultPage = ERROR;
    }
      
    if (XHR_OBJECT_NAME.equals(request.getHeader(HEADER_REQUEST_WITH))) {
      resultPage = AJAX_RESULT_NAME;
    }
      
    return resultPage;
  }
}
Copy after login

Struts2 Performance Tuning Interceptor

When we need to implement some small requirements at work, we might as well conduct a simple survey first and see Does the open source framework we are using already have the functions we need, so we don’t have to reinvent the wheel?
Let’s take performance testing as an example to see how to investigate whether the Struts2 framework has this function.

1. struts-default.xml

Because many of the core functions of Struts2 are implemented based on internal interceptors, we first need to see if it has performance tuning-related interceptions device. This requires checking the default configuration file struts-default.xml in strut2-core-2.3.1.2.jar.

<span style="white-space:pre"> </span><interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
      <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
      <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
      <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
      <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
      <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
      <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
      <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
      <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
      <interceptornameinterceptorname="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
      <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
      <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
      <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
      <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
      <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
      <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
      <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
      <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
      <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
      <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
      <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
      <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
      <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
      <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
      <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
      <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
      <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
      <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
      <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
      <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
      <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
      <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
Copy after login

Struts2 is like a treasure chest with many built-in interceptors. You can see that profiling is probably the interceptor that meets our needs. Now open the source code. Find out.


2. ProfilingActivationInterceptor

org.apache.struts2.interceptor.ProfilingActivationInterceptor.java

public class ProfilingActivationInterceptor extendsAbstractInterceptor {
   
  private String profilingKey = "profiling";
  private boolean devMode;
    
  @Inject(StrutsConstants.STRUTS_DEVMODE)
  public void setDevMode(String mode) {
    this.devMode = "true".equals(mode);
  }
   
  @Override
  public String intercept(ActionInvocationinvocation) throws Exception {
    if (devMode) {
      Object val =invocation.getInvocationContext().getParameters().get(profilingKey);
      if (val != null) {
        String sval = (val instanceof String ?(String)val : ((String[])val)[0]);
        boolean enable = "yes".equalsIgnoreCase(sval)|| "true".equalsIgnoreCase(sval);
        UtilTimerStack.setActive(enable);
        invocation.getInvocationContext().getParameters().remove(profilingKey);
      }
    }
    return invocation.invoke();
   
  }
   
}
Copy after login

From source code As can be seen in , as long as the HTTP request parameter sent by the browser contains profiling=true or yes, the performance interceptor will open the Timer tool class and print out the execution time of the Action.

3. struts.xml

Because the profiling interceptor is not included in the default defaultStack, we must first append it to our custom interceptor stack.

<package name="ajax-default" extends="velocity-default">
    <result-types>
      <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
    </result-types>
      
    <interceptors>
      <interceptor-stacknameinterceptor-stackname="ajaxInterceptorStack">
      <interceptor-refnameinterceptor-refname="defaultStack" />
      <interceptor-ref name="profiling"/>
      </interceptor-stack>
    </interceptors>
      
    <default-interceptor-refnamedefault-interceptor-refname="ajaxInterceptorStack" />
      
    <global-results>
      <result name="comAjaxResult" type="json">
        <param name="excludeNullProperties">true</param>
        <param name="root">result</param>
        <param name="ignoreHierarchy">false</param>
      </result>
    </global-results>
  </package>
Copy after login

4. userview.js

You can now modify the AJAX call parameters, add profiling parameters, and start performance tuning.

function searchAllUser(){
  jQuery.ajax({
    type:"post",
    url: "searchAllUser.action",
    processData:true,
    dataType:&#39;json&#39;,
    data:jQuery("#userQueryForm").serialize() + "&profiling=yes",
    success:function(data) {
    if (data.status == 1) {
       alert("创建成功");
       generateTableFromJson("result", data.resultRows);
    } else {
       alert("创建失败");
    }
    }
  });
}
Copy after login

5. The final effect

The printed result is as follows. In addition to the total execution time, the execution time of the Action method and the rendering time of the Result will be listed separately.

Determine whether to call AJAX in Javas Struts and use interceptors to optimize it


For more related articles on determining whether to call AJAX in Java's Struts and using interceptors to optimize it, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles