mvc過濾器
『壹』 springmvc過濾器和攔截器的區別
攔截器與過濾器的區別 :
1. 攔截器是基於java的反射機制的,而過濾器是基於函數回調。
2. 攔截器不依賴與servlet容器,過濾器依賴與servlet容器。
3. 攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求起作用。
4. 攔截器可以訪問action上下文、值棧里的對象,而過濾器不能訪問。
5. 在action的生命周期中,攔截器可以多次被調用,而過濾器只能在容器初始化時被調用一次
攔截器的代碼實現(以struts2為例):
1、在xml文件中如何定義攔截器
<interceptors>
<interceptor name="filterIPInterceptor"
class="com.xxxx.web.FilterIPActionInterceptor" />
<interceptor-stack name="filterIPStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="filterIPInterceptor" />
</interceptor-stack>
</interceptors>
2、怎麼遍別寫自定義攔截器
public class FilterIPActionInterceptor extends AbstractInterceptor
{
/** 日誌控制. */
private final Log log = LogFactory.getLog(getClass());
/**
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
@Override
『貳』 mvc三層框架 自定義過濾器放在哪
自定義Filter需要繼承ActionFilterAttribute抽象類,重寫其中需要的方法,來看下ActionFilterAttribute類的方法簽名。
//表示所有操作-篩選器特性的基類。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
{
protected ActionFilterAttribute();
// 在Action執行之後由 MVC 框架調用。
public virtual void OnActionExecuted(ActionExecutedContext filterContext);
// 在Action執行之前由 MVC 框架調用。
public virtual void OnActionExecuting(ActionExecutingContext filterContext);
// 在執行Result後由 MVC 框架調用。
public virtual void OnResultExecuted(ResultExecutedContext filterContext);
// 在執行Result之前由 MVC 框架調用。
『叄』 SpringMVC的攔截器和過濾器的區別與聯系
1、首先要明確什麼是攔截器、什麼是過濾器
1.1 什麼是攔截器:
攔截器,在AOP(Aspect-Oriented Programming)中用於在某個方法或欄位被訪問之前,進行攔截然後在之前或之後加入某些操作。攔截是AOP的一種實現策略。
在Webwork的中文文檔的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制可以使開發者可以定義在一個action執行的前後執行的代碼,也可以在一個action執行前阻止其執行。同時也是提供了一種可以提取action中可重用的部分的方式。
談到攔截器,還有一個詞大家應該知道——攔截器鏈(Interceptor Chain,在Struts2中稱為攔截器棧 Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯結成一條鏈。在訪問被攔截的方法或欄位時,攔截器鏈中的攔截器就會按其之前定義的順序被調用。
1.2. 攔截器的實現原理:
大部分時候,攔截器方法都是通過代理的方式來調用的。Struts2的攔截器實現相對簡單。當請求到達Struts2的ServletDispatcher時,Struts 2會查找配置文件,並根據其配置實例化相對的攔截器對象,然後串成一個列表(list),最後一個一個地調用列表中的攔截器。
1.3 什麼是過濾器
過濾器是一個程序,它先於與之相關的servlet或JSP頁面運行在伺服器上。過濾器可附加到一個或多個servlet或JSP頁面上,並且可以檢查進入這些資源的請求信息。在這之後,過濾器可以作如下的選擇:
①以常規的方式調用資源(即,調用servlet或JSP頁面)。
②利用修改過的請求信息調用資源。
③調用資源,但在發送響應到客戶機前對其進行修改。
④阻止該資源調用,代之以轉到其他的資源,返回一個特定的狀態代碼或生成替換輸出。
1.4 Servlet過濾器的基本原理
在Servlet作為過濾器使用時,它可以對客戶的請求進行處理。處理完成後,它會交給下一個過濾器處理,這樣,客戶的請求在過濾鏈里逐個處理,直到請求發送到目標為止。例如,某網站里有提交「修改的注冊信息」的網頁,當用戶填寫完修改信息並提交後,伺服器在進行處理時需要做兩項工作:判斷客戶端的會話是否有效;對提交的數據進行統一編碼。這兩項工作可以在由兩個過濾器組成的過濾鏈里進行處理。當過濾器處理成功後,把提交的數據發送到最終目標;如果過濾器處理不成功,將把視圖派發到指定的錯誤頁面。
2、攔截器與過濾器的區別 :
1. 攔截器是基於java的反射機制的,而過濾器是基於函數回調。
2. 攔截器不依賴與servlet容器,過濾器依賴與servlet容器。
3. 攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求起作用。
4. 攔截器可以訪問action上下文、值棧里的對象,而過濾器不能訪問。
5. 在action的生命周期中,攔截器可以多次被調用,而過濾器只能在容器初始化時被調用一次
攔截器的代碼實現(以struts2為例):
1、在xml文件中如何定義攔截器
<interceptors>
<interceptor name="filterIPInterceptor"
class="com.xxxx.web.FilterIPActionInterceptor" />
<interceptor-stack name="filterIPStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="filterIPInterceptor" />
</interceptor-stack>
</interceptors>
2、怎麼遍別寫自定義攔截器
public class FilterIPActionInterceptor extends AbstractInterceptor
{
/** 日誌控制. */
private final Log log = LogFactory.getLog(getClass());
/**
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
@Override
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception
{
String result = null;
// 獲得當前方法名.
String methodName = invocation.getInvocationContext().getName();
String currIp = null;
try
{
if (invocation.getAction() instanceof PortletAction)
{
PortletAction action = (PortletAction) invocation.getAction();
currIp = action.getRequest().getRemoteAddr();
}
String ip = ApplicationResource.getHotValue("ALLOW_CACHE_IP");
if (StringUtils.isBlank(ip) || StringUtils.isBlank(currIp))
{
log.error("允許刷新的IP不存在或當前請求的IP非法.");
throw new NoAllowIPException();
}
else
{
String[] ips = ip.split(",");
boolean errorIp = true;
for (String s : ips)
{
if (s.equals(currIp))
errorIp = false;
}
// 判斷IP
if (errorIp)
throw new NoAllowIPException();
}
result = invocation.invoke();//調用被攔截的方法
}
catch (Exception e)
{
log.error("異常類名:" + invocation.getAction().getClass());
log.error("異常方法:" + methodName, e);
throw e;
}
return result;
}
}
3、怎麼編寫過濾器
1、在web.xml裡面配置自定義的攔截器
<filter>
<filter-name>Redirect Filter</filter-name>
<filter-class>com.xx.filter.RedirectFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Redirect Filter</filter-name>
<url-pattern>/xx/xx/*</url-pattern>
</filter-mapping>
2、如何編寫自定義的攔截器
public class RedirectFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// 獲取URL
Long startTime = null;
if (log.isDebugEnabled())
{
startTime = System.currentTimeMillis();
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
String url = httpRequest.getRequestURL().toString();
if (url == null || url.trim().length() == 0) {
return;
}
if (url.indexOf(luceneCreateMapping) != -1
|| url.indexOf(luceneSearchMapping) != -1) {
doFilterForxxx(request, response, url);
} else {
doxxxx(request, response, url);
}
if (log.isDebugEnabled())
{
long endTime = System.currentTimeMillis();
Thread currentThread = Thread.currentThread();
String threadName = currentThread.getName();
log.debug("[" + threadName + "]" + "< "
+ this.getClass().getName() + " " + url + " "
+ (endTime - startTime) + " ms");
}
// 激活下一個Filter
filterChain.doFilter(request, response);
}
}
『肆』 c#mvc過濾器有哪幾種,應用場景是什麼
mvc裡面的model通常稱為viewmodel 顧名思義用來與view做交互的 三層里的model通常與資料庫專欄位保持一屬致 在三層里用來與資料庫和業務層做交互 兩者都是實體類 DAL操作資料庫封裝成一個model 向上傳遞至BLL 然後在mvc中將model轉換為viewmodel供vi
『伍』 mvc中的action過濾器有哪些
MVC支持的過濾器類型有四種,分別是:Authorization(授權),Action(行為),Result(結果)和Exception(異常)。如下表,
『陸』 mvc的filter有幾類,分別是什麼
(1) 直接實現Filter,這一類過濾器只有CompositeFilter;
(2) 繼承抽象類GenericFilterBean,該類實現了.servlet.Filter,這一類的過濾器只有一個,即DelegatingFilterProxy;
(3) 繼承抽象類OncePerRequestFilter,該類為GenericFilterBean的直接子類,這一類過濾器包括CharacterEncodingFilter、HiddenHttpMethodFilter、HttpPutFormContentFilter、RequestContextFilter和ShallowEtagHeaderFilter;
(4) 繼承抽象類AbstractRequestLoggingFilter,該類為OncePerRequestFilter的直接子類,這一類過濾器包括CommonsRequestLoggingFilter、和。
『柒』 C# MVC Filter過濾器如何停止往下執行
你這是開發mvc可不是開發asp,什麼年代了還Response.Write呢?
設置filterContext的Result為一個ViewResult來阻止執行Action
『捌』 MVC過濾器,怎麼排除某個Action不使用Controller下的過濾器
這個好像沒有什麼辦法吧,除非你在過濾器中做判斷,類似於[AllowAnonymous]。
這個你為什麼不在每個Action上面加過濾器呢?
『玖』 過濾器和SpringMVC的攔截器的區別
過濾器和攔截器的區別:
①攔截器是基於Java的反射機制的,而過濾器是基於函數回調。
②攔截器不依賴與servlet容器,過濾器依賴與servlet容器。
③攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求起作用。
④攔截器可以訪問action上下文、值棧里的對象,而過濾器不能訪問。
⑤在action的生命周期中,攔截器可以多次被調用,而過濾器只能在容器初始化時被調用一次。
⑥攔截器可以獲取IOC容器中的各個bean,而過濾器就不行,這點很重要,在攔截器里注入一個service,可以調用業務邏輯。
寫了點測試代碼,順便整理一下思路,搞清楚這幾者之間的順序:
1.過濾器是JavaEE標准,採用函數回調的方式進行。是在請求進入容器之後,還未進入Servlet之前進行預處理,並且在請求結束返回給前端這之間進行後期處理。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("before...");
chain.doFilter(request, response);
System.out.println("after...");
}
chain.doFilter(request, response);這個方法的調用作為分水嶺。事實上調用Servlet的doService()方法是在chain.doFilter(request, response);這個方法中進行的。
2.攔截器是被包裹在過濾器之中的。
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
a.preHandle()這個方法是在過濾器的chain.doFilter(request, response)方法的前一步執行,也就是在 [System.out.println("before...")][chain.doFilter(request, response)]之間執行。
b.preHandle()方法之後,在returnModelAndView之前進行,可以操控Controller的ModelAndView內容。
c.afterCompletion()方法是在過濾器返回給前端前一步執行,也就是在[chain.doFilter(request, response)][System.out.println("after...")]之間執行。
3.SpringMVC的機制是由同一個Servlet來分發請求給不同的Controller,其實這一步是在Servlet的service()方法中執行的。所以過濾器、攔截器、service()方法,dispatc()方法的執行順序應該是這樣的,大致畫了個圖:其實非常好測試,自己寫一個過濾器,一個攔截器,然後在這些方法中都加個斷點,一路F8下去就得出了結論。
『拾』 MVC過濾器,怎麼排除某個Action不使用Controller下的過濾器
當ActionInvoker在執行目標Action方法之前,會根據Order和Scope屬性對用於封裝ActionFilter的Filter對象進行排序。
然後根據當前ControllerContext和ActionDescriptro創建一個ActionExecutingContext對象,並將其作為參數依次調用所有ActionFilter的OnActionExecuting方法。
在這之後真正的目標Action方法被執行,ActionInvoker隨後執行後續的篩選操作。具體來說,它根據當前ControllerContext、ActionDescriptro以及Action方法執行過程中拋出的異常創建一個ActionExecutedContext對象。該ActionExecutedContext的Cancel屬性為False,如果Action方法返回一個ActionResult對象,該對象將會作為該ActionExecutedContext的Result屬性。
接下來按照相反的次序依次調用ActionFilter對象的OnActionExecuted方法,執行過程中的ActionFilter可以修改ActionExecutedContext的Result屬性。當整個ActionFilter鏈執行結束之後,ActionExecutedContext的Result屬性返回的ActionResult將會作為對當前請求的響應。右圖基本上反映了連同目標Action在內的整個ActionFilter鏈的執行過程。