C#實用於like語句的SQL格局化函數。本站提示廣大學習愛好者:(C#實用於like語句的SQL格局化函數)文章只能為提供參考,不一定能成為您想要的結果。以下是C#實用於like語句的SQL格局化函數正文
Strut2斷定能否是AJAX挪用
1. AJAX與傳統Form表單
現實上,二者普通都是經由過程HTTP的POST要求。差別是閱讀器提交Form表單後,希冀辦事器前往一個完全的HTML頁面。而AJAX挪用是由XMLHttpRequest對象(分歧閱讀器能夠紛歧樣)收回,閱讀器希冀辦事器前往HTML片斷便可,詳細是JSON、XML等都沒有請求。前往到閱讀器後若何應用,也是由JS劇本本身決議的。
2. 要求是否是AJAX
那末關於辦事器端,若何斷定一個HTTP要求是否是AJAX挪用?這須要看HTTP的Header。
我們可以經由過程Header中的x-request-with來斷定。雖然分歧閱讀器發送AJAX要求的對象分歧,然則假如應用jQuery發送AJAX要求的話,jQuery外部完成ajax的時刻,曾經參加了標識。jQuery源碼中是如許的:xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
所以,假如項目標前台頁面都是經由過程jQuery發送AJAX要求的話,如許斷定是平安的。
上面是HTTP要求攜帶的Header信息。
通俗Form表單提交
===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
AJAX挪用(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
3. 在Action中取得HTTP要求頭
在Action類中,經由過程ServletRequestAware接口取得HttpServletRequest對象,再經由過程getHeader辦法獲得我們想要的頭信息。
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;
}
}
Struts2機能調優攔阻器
當我們在任務中須要完成某些小需求時,無妨先輩行下簡略的調研,看看正在應用的開源框架能否曾經具有了我們須要的功效,如許就不消反復創造輪子了。
上面以機能測試為例,看看若何查詢拜訪Struts2框架能否具有這類功效。
1. struts-default.xml
由於Struts2的很多焦點功效都是基於外部攔阻器來完成的,所以我們起首要看看它能否有機能調優相干的攔阻器。這就須要檢查strut2-core-2.3.1.2.jar中的默許設置裝備擺設文件struts-default.xml了。
<span > </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" />
Struts2像個百寶箱一樣內置了許多攔阻器,可以看到profiling極可能就是相符我們需求的攔阻器,那如今就翻開源碼一探討竟。
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();
}
}
從源碼中可以看到,只需閱讀器發過去的HTTP要求參數中包括profiling=true或許yes,機能攔阻器就會開啟Timer對象類,打印出Action的履行消費時光。
3. struts.xml
由於profiling攔阻器沒有包括到默許的defaultStack中,所以我們要先將它追加到我們自界說的攔阻器棧中。
<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>
4. userview.js
如今便可以修正AJAX挪用參數,追加上profiling參數便可以開端機能調優了。
function searchAllUser(){
jQuery.ajax({
type:"post",
url: "searchAllUser.action",
processData:true,
dataType:'json',
data:jQuery("#userQueryForm").serialize() + "&profiling=yes",
success:function(data) {
if (data.status == 1) {
alert("創立勝利");
generateTableFromJson("result", data.resultRows);
} else {
alert("創立掉敗");
}
}
});
}
5. 終究後果
打印成果就是上面如許。除總履行時光外,Action辦法的履行時光和Result的襯著時光都邑分離列出。
