struts+filter = error ? (倾囊求助)

meconsea 2004-09-21 02:13:01
在用基本的MVC时候,我在web.xml中配置字符过滤器filter,tomcat能正常起来,一切都能正常使用.
部分代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>web.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

而我在用struts框架时候在web.xml中加入
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>web.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
tomcat启动的时候就抛出异常:
异常信息如下:
----------------------------------------------------------------------------
2004-9-21 14:11:40 org.apache.commons.digester.Digester error
严重: Parse Error at line 47 column 11: Element type "filter" must be declared.
org.xml.sax.SAXParseException: Element type "filter" must be declared.
...............................................
严重: Parse Error at line 48 column 18: Element type "filter-name" must be decla
red.
............................................................
严重: Parse Error at line 49 column 19: Element type "filter-class" must be decl
ared.
.....................................................
2004-9-21 14:11:40 org.apache.commons.digester.Digester error
严重: Parse Error at line 55 column 19: Element type "filter-mapping" must be de
clared.
...........................................
2004-9-21 14:11:40 org.apache.commons.digester.Digester error
严重: Parse Error at line 56 column 18: Element type "filter-name" must be decla
red.
......................................
2004-9-21 14:11:40 org.apache.commons.digester.Digester error
严重: Parse Error at line 60 column 11: The content of element type "web-app" mu
st match "(icon?,display-name?,description?,distributable?,context-param*,servle
t*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*
,taglib*,resource-ref*,security-constraint*,login-config?,security-role*,env-ent
ry*,ejb-ref*)".
org.xml.sax.SAXParseException: The content of element type "web-app" must match
"(icon?,display-name?,description?,distributable?,context-param*,servlet*,servle
t-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,
resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-r
ef*)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown S
ource)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)

at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(
Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Un
known Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.commons.digester.Digester.parse(Digester.java:1543)
at org.apache.catalina.startup.ContextConfig.applicationConfig(ContextCo
nfig.java:282)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:63
9)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfi
g.java:243)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:166)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3
567)
at org.apache.catalina.startup.HostConfig.checkWebXmlLastModified(HostCo
nfig.java:614)
at org.apache.catalina.startup.HostConfig.run(HostConfig.java:854)
at java.lang.Thread.run(Thread.java:534)
---------------------------------------------------------------------
我该怎么描述filter.我如何去处理他?如果在struts中怎么解决request引起的字符乱码问题?
...全文
98 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
meconsea 2004-09-21
不管用的,是因为 xml的问题格式和declared的问题!
回复
majy 2004-09-21
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

改成
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
回复
meconsea 2004-09-21
up!!!!!!!!!!!!
回复
zcjl 2004-09-21

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
改成
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

ps:你这个web.xml是怎么生成的?自己写的还是根据现有的拿来修改?
回复
meconsea 2004-09-21
高手,我按你写的这么个web.xml,问题依旧阿?
只是顺序的问题?
回复
zcjl 2004-09-21
果然是这个问题
org.xml.sax.SAXParseException: The content of element type "web-app" must match
"(icon?,display-name?,description?,distributable?,context-param*,servlet*,servle
t-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,
resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-r
ef*)"

这个错误信息看到没?
说明你的web.xml没有通过http://java.sun.com/j2ee/dtds/web-app_2_2.dtd的验证(关于xml的dtd格式验证,请自己google一下)
正确的顺序应该按给出的提示信息那样
所以,你的web.xml应该这样配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>HelloApp Struts Application</display-name>

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>web.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

</web-app>
回复
meconsea 2004-09-21
怎么处理 "filter-mapping" must be declared???
回复
ma 2004-09-21
我也遇到同样的问题,即struts不能同filter一起使用,大家帮忙!
回复
meconsea 2004-09-21
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>HelloApp Struts Application</display-name>

<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>web.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
回复
zcjl 2004-09-21
贴出web.xml
帮你看看
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2004-09-21 02:13
社区公告
暂无公告