NutzCN Logo
问答 如果集中处理not atcion match问题?
发布于 3048天前 作者 lambert 2493 次浏览 复制 上一个帖子 下一个帖子
标签:

大神好,我想写一个filter,拦劫所有not atcion match的请求,并统一送到我定义的/NoFound请求中,这要怎么写呢?
HttpServletRequest request = actionContext.getRequest();
String path = actionContext.getPath();
这个path也只是请求的地址,如何让它与合法的请求做对比呢?并如何统一转向呢?请大蛇给指条明路
------初级玩家

14 回复

你需要的是404的时候访问一个入口方法

先建一个notfound空文件,然后在web.xml里面添加

	<error-page>
		<error-code>404</error-code>
		<location>/notfound</location>
	</error-page>

然后检查NutFilter的filter-mapping是不是这样的,关键是要拦截ERROR


<filter-mapping> <filter-name>nutz</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>

然后,你就可以写个入口方法了

public void ErrorModule {
    @At(value="/notfound")
    @Ok("raw")
    public String notfound() {
         return "404了啊啊啊";
    }
}

非常感谢,已经成功了,谢谢大蛇~

@wendal 同时,特别希望大蛇能继续把那本入门的书写完,也特别希望大蛇能把nutz的设计思路及原理性的写成书,像我,就感觉一直是在使用,没有办法更深入的理解它,也许是我太笨了,但至少我有这个心想更明白一些东西,也希望大蛇能供一些思路,让我能从用走向为nutz贡献的地步。

按照以上的配置,我也试了试,结果系统无法启动,然后爆出一下日志
大致判断是发生了错误,跳到404,但是跳的过程又发生异常,无限的404

2017-01-21 22:43:05,962 [ DefaultQuartzScheduler_Worker-8 ] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 
2017-01-21 22:43:05,962 [ DefaultQuartzScheduler_Worker-5 ] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 
2017-01-21 22:43:05,962 [ DefaultQuartzScheduler_Worker-9 ] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 
2017-01-21 22:43:05,962 [ DefaultQuartzScheduler_Worker-4 ] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 
2017-01-21 22:43:06,068 [ DefaultQuartzScheduler_Worker-1 ] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 
一月 21, 2017 10:43:08 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-bio-8001"]
一月 21, 2017 10:43:08 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-bio-8009"]
一月 21, 2017 10:43:08 下午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-bio-8001"]
一月 21, 2017 10:43:08 下午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-bio-8009"]
Exception in thread "http-bio-8001-exec-5" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3332)
	at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)
	at java.lang.StringBuilder.append(StringBuilder.java:202)
	at org.nutz.lang.stream.StringWriter.write(StringWriter.java:23)
	at java.io.Writer.write(Writer.java:192)
	at java.io.Writer.write(Writer.java:157)
	at org.nutz.json.impl.JsonRenderImpl.doIntent(JsonRenderImpl.java:437)
	at org.nutz.json.impl.JsonRenderImpl.appendPairBegin(JsonRenderImpl.java:158)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:168)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:317)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:310)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:132)
	at org.nutz.json.impl.JsonRenderImpl.coll2Json(JsonRenderImpl.java:407)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:124)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:171)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:317)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:310)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:132)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:171)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:317)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:310)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:132)
	at org.nutz.json.impl.JsonRenderImpl.coll2Json(JsonRenderImpl.java:407)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:124)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:171)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:317)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:310)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:132)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:171)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:317)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:310)
一月 21, 2017 10:43:15 下午 org.apache.catalina.core.ContainerBase removeChild
严重: ContainerBase.removeChild: destroy: 
org.apache.catalina.LifecycleException: An invalid Lifecycle transition was attempted ([before_destroy]) for component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] in state [STOPPING]
	at org.apache.catalina.util.LifecycleBase.invalidTransition(LifecycleBase.java:395)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:287)
	at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1030)
	at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1230)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
	at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1030)
	at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1230)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
	at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:594)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
	at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:846)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
	at org.apache.catalina.startup.Catalina.stop(Catalina.java:767)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:728)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:428)

去除
ERROR
后项目正常运作。求解

去除
《dispatcher》ERROR《dispatcher》
后项目正常运作。求解

没添加@At("/notfound") 这样的入口方法? 完整映射路径要匹配

version 60

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<display-name>www</display-name>


	<filter>
		<filter-name>whale</filter-name>
		<filter-class>org.nutz.mvc.WhaleFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>
				http.hidden_method_param=_method
				http.method_override=true
				enc.input=utf-8
				enc.output=utf-8
			</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>whale</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<context-param>
		<param-name>nutz-iocby</param-name>
		<param-value>
			*org.nutz.ioc.loader.json.JsonLoader,
			nutz-cfg.js,
			nutz-daos.js,
			nutz-upload.js,
			*org.nutz.ioc.loader.annotation.AnnotationIocLoader,

			gd,

			dev,
			dlc,
			hgh,
			khn,
			lyi,
			nkg,
			pek,
			pvg,
			szx,
			tsn,
			urc,
			wnz,
			yox
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.nutz.mvc.NutMvcListener</listener-class>
	</listener>



	<listener>
		<listener-class>pek.listener.ListenerConfig</listener-class>
	</listener>

	<filter>
		<filter-name>FilterEncoding</filter-name>
		<filter-class>pek.filter.FilterEncoding</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>FilterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- 获取浏览者的IP -->
	<filter>
		<filter-name>filterIP</filter-name>
		<filter-class>hgh.filter.FilterIP</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>filterIP</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- 反黑客 -->
	<filter>
		<filter-name>antiHarker</filter-name>
		<filter-class>hgh.filter.FilterAntiHarker</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>antiHarker</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- 确定浏览者浏览的网站 -->
	<filter>
		<filter-name>domain</filter-name>
		<filter-class>pek.filter.FilterDomain</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>domain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!--format-->
	<filter>
		<filter-name>format</filter-name>
		<filter-class>dev.filter.FilterFormatValue</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>format</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- sitemap.xml
	<filter>
		<filter-name>sitemap</filter-name>
		<filter-class>pek.filter.FilterSitemap111</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>sitemap</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>-->


	<!-- nutz -->
	<filter>
		<filter-name>nutz</filter-name>
		<filter-class>org.nutz.mvc.NutFilter</filter-class>
		<init-param>
			<param-name>modules</param-name>
			<param-value>pek.Mvc</param-value>
		</init-param>
		<init-param>
			<param-name>ignore</param-name>
			<param-value>^(.+[.])(jsp|png|gif|jpg|js|css|jspx|jpeg|html)$</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>nutz</filter-name>
		<url-pattern>/*</url-pattern>

		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>ERROR</dispatcher>

	</filter-mapping>




	<filter>
		<filter-name>robots</filter-name>
		<filter-class>pek.filter.FilterRobots</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>robots</filter-name>
		<url-pattern>/robots.txt</url-pattern>
	</filter-mapping>



	<error-page>
		<error-code>403</error-code>
		<location>/403</location>
	</error-page>


	<error-page>
		<error-code>404</error-code>
		<location>/404</location>
	</error-page>



	<session-config>
		<session-timeout>193</session-timeout>
	</session-config>

</web-app>

class


package dev.page.body; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Ok; /** */ @IocBean @At ( "" ) public class Body40X { final public static String URL = ""; final private static Logger log = Logger.getLogger ( BodyIndex.class ); /** * @return */ @At ( "/404" ) @Ok ( "raw" ) public Map<String , Object> index404 () { log.info ( "404" ); Map<String , Object> map = new HashMap<> ( 1 ); return map; } /** * @return */ @At ( "/403" ) @Ok ( "raw" ) public Map<String , Object> index403 () { log.info ( "404" ); Map<String , Object> map = new HashMap<> ( 1 ); return map; } }

把@At ( "" )去掉.

看看映射日志, 看看index404实际的映射路径

还有一种可能性是, 需要加个 404 空文件

另外, 你贴的报错信息,是Json循环依赖导致挂, 并非404

'/404' >> (Body40X.java:28).index404 : Map | @Ok(b:/404.html) @Fail(null ) | by 0 Filters | (I:UTF-8/O:UTF-8)
主要是添加了ERROR后,就报了这个问题,看了下日志也没有其他的异常爆出来,删掉ERROR后,项目正常运作,主要是这样一个场景,把有些旧的方法给删了,导致google找不到页面了,就想用404填上,一搜还真有这个方案,按照上面的方法加入后就成这个样子了

爆出的异常都不知道哪里有问题,很是纳闷,整个项目运行到这个异常之前都没有一个WARN

发现问题了

环境
开发环境是JDK8,TOMCAT8,NETBEANS8.2
NETBEANS启动项目后,会时不时向项目发起 一个 /netbeans-tomcat-status-test GET请求,但是项目本身是没有这个处理办法,就开始触发404,可能是项目还在初始化的过程中,出发这个404就会爆出上面的异常,不知道算不算BUG

解决方案
对于/netbeans-tomcat-status-test 添加了一个类来处理这个请求,项目正常运作,同时404页正常捕捉到了

添加回复
请先登陆
回到顶部