IDEA学习SpringMVC遇到错误:The content of element type "web-app" must match "..的解决方案建议

个人在利用IDEA创建maven项目学习SpringMVC的时候,出现了web.xml报错:

The content of element type “web-app” must match “(icon?,display-
name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-
mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-
ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)”.
No grammar constraints (DTD or XML schema) detected for the document.

在查询解决方案之后:
有以下两种方法:

  1. 严格按照报错中的提示配置顺序
  2. 删除删除<!DOCTYPE的内容,因为文档内容标签顺序不符合web-app_2_3.dtd规范

以上相关解决方案来源的博客:https://blog.csdn.net/zkn_CS_DN_2013/article/details/23670647

其中,不知道是为什么,第一种更换配置顺序之后,个人的web.xml配置还是经常会报错,搞了许久,还是没能弄好。而第二种方式中,的确可以解决报错的问题,但却出现了新的问题,没有删除<!DOCTYPE的内容之前,编写< XX-class>< / XX-class>中相关的类名的时候还是会有相关的自动补全提示的,而删除之后就没有补全提示了,虽然说IDEA可以根据ctrl + shirt + N键可以输出类名查到完整的reference全类名,但总觉得不是很满意,毕竟查多了也是会烦死人的,实在太麻烦了;后来才发现,是自己在创建maven项目的时候选错了archetype,其中有两个有关的webapp,建议选择有cocoon的那个web

在这里插入图片描述

建立好项目之后把web.xml中的自带的配置删了之后就是这个样子:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--
  - This is the Cocoon web-app configurations file
  -
  - $Id$
  -->
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <!-- Servlet Filters ================================================ -->

 

</web-app>

而之后就可以导入依赖,在其中配置自己想要的相关配置,不会出现报错,也不会出现没有自动补全提示的情况了。

总的来说有自动补全还是十分方便的,省掉了许多没有必要的查询,只要记住类名就可以自动补全了。

另外加上前端页面输出,而控制台输出乱码的解决方案:
在web.xml中加上:

  <!--防止传入的数据乱码-->
  <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  </filter>

  <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

就可以了。。。。这个是之前遇到乱码之后查到的配置,具体在哪里查到的就不是特别记得清了,所以没有标注出处。这个就当做之后学习的复习回顾吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值