大神好,我想写一个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
后项目正常运作。求解
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;
}
}
添加回复
请先登陆