搭建SSM项目时遇到的一些问题

来源:互联网 发布:everything微课软件 编辑:程序博客网 时间:2024/05/03 06:30

1.Mybatis配置




项目报错如上图,原因是:src/main/java下的xml文件没有随同java编译后的class文件一同copy到相应的class目录,需要在pom.xml上加上如下配置:

<resources>  <resource>    <directory>src/main/java</directory>    <includes>      <include>**/*.properties</include>      <include>**/*.xml</include>    </includes>    <filtering>false</filtering>  </resource></resources>

2.加入@ResponseBody注解,使得返回json字符串。spring-mvc.xml里两种配置方式:

  配置方式一:

  <!--避免IE执行AJAX时,返回JSON出现下载文件 -->    <bean id="mappingJacksonHttpMessageConverter"          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->    <bean            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->            </list>        </property>    </bean>

配置方式二:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <bean                        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                    <property name="supportedMediaTypes">                        <list>                            <value>application/json;charset=UTF-8</value>                        </list>                    </property>                </bean>                <bean class="org.springframework.http.converter.StringHttpMessageConverter">                    <property name="supportedMediaTypes">                        <list>                            <value>text/html;charset=UTF-8</value>                            <value>text/xml;charset=UTF-8</value>                            <value>application/json;charset=UTF-8</value>                        </list>                    </property>                </bean>            </list>        </property>    </bean>

在由配置方式一转为配置方式二的时候,必须添加<mvc:annotation-driven /> 注解,否则会报吐下错误: