相信技术的力量

SSM开发Web项目问题解决记录

Invalid content was found starting with element 'init- param'

在配置Spring前端控制器时,如果按照如下方式配置那么<init-param>标签就会有错误提示Invalid content was found starting with element 'init- param' .

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>

</servlet>

解决方案:

<load-on-startup>1</load-on-startup> 放在<init-param>标签的后面即可.即

<servlet>
   ...
    <init-param>
      ...
    </init-param>
     <load-on-startup>1</load-on-startup>
</servlet>

Could not open ServletContext resource

错误信息:Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

web.xml中,加载springmvc的配置文件时,如果不指定contextConfigLocation路径,那么默认加载的是/WEB-INF/servlet名称-serlvet.xml(例如dispatcher-servlet.xml , springmvc-servlet.xml)

FileNotFoundException

错误信息: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

如果通过maven管理web项目,并且把配置文件放在了resources目录下,那么此时配置applicationContext.xml路径时,需要使用如下方式:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <!--<param-value>classpath*:applicationContext.xml</param-value>-->
</context-param>

web.servlet.PageNotFound.noHandlerFound

错误信息:web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/helloworld] in DispatcherServlet with name 'dispatcher'

Stackoverflow问题链接直达 里列举了诸多的可能性原因.我这里当时的原因是:

  1. Spring扫描包配置问题,导致未扫描到指定的Controller
  2. 编译文件输出路径问题

form表单提交时类型转换异常

错误信息:Unable to convert string [${createtime}] to class [java.util.Date] for attribute [value]: [Property Editor not registered with the PropertyEditorManager]

需要自定义参数绑定,完成日期类型的转换

1.定义日期转换器

public class CustomDateConverter implements Converter<String,Date> {

    public Date convert(String source) {

        //实现 将日期串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            //转成直接返回
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果参数绑定失败返回null
        return null;
    }

}

2.dispatcher-servlet.xml中配置转换器

<mvc:annotation-driven conversion-service="conversionService" validator="validator"/> 

<!-- 自定义参数绑定-->
<bean id="conversionService"
      class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <!-- 转换器 -->
    <property name="converters">
        <set>
            <!-- 日期类型转换 -->
            <bean class="controller.converter.CustomDateConverter"/>
        </set>
    </property>
</bean>

完成!

javax.servlet.http.httpservlet 不存在

将tomcat中lib包里的servlet-api.jar(例如D:\Dev\Tomcat9\lib\servlet-api.jar) 复制到项目中 , 并添加依赖 , 重新build即可.

MultipartException:form表单文件上传失败

错误信息: MultipartException: The current request is not a multipart request

解决方案:

需要在form表单上添加enctype="multipart/form-data属性

<form id="test" action="${pageContext.request.contextPath }/test.action" method="post" enctype="multipart/form-data">

js文件加载失败:XXX.js 404 (Not Found)

在dispatcher-servlet.xml中添加静态资源解析

<!-- 静态资源解析 包括 :js、css、img、..-->
<mvc:resources location="/js/" mapping="/js/**"/>

标签onclick属性调用js function()方法无效

错误信息:Uncaught ReferenceError: [
function] is not defined at HTMLInputElement.onclick

解决方法:
先添加jquery依赖,再定义另外的script标签,并在其中声明function

<%--引入js的包--%>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script>
    your function...
</script>

HttpMediaTypeNotSupportedException

错误信息:HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

原因:jackson依赖没有添加完整

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0.pr4</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0.pr4</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0.pr4</version>
</dependency>

配置完整就好了.

Invalid bound statement (not found)

运行项目时报如下异常:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

问题:

Mybatis接口和xml文件绑定异常

解决:

主要检查Mybatis中mapper接口中的方法名和mapper.xml中的id名字是否一致

参考链接:

http://blog.csdn.net/softwarehe/article/details/8889206

⬆️