黑马程序员技术交流社区

标题: 【成都JavaEE】SpringMVC学习笔记,易错点记录 [打印本页]

作者: 陌上桑桑    时间: 2019-7-11 14:28
标题: 【成都JavaEE】SpringMVC学习笔记,易错点记录
1.spring引入servlet/jsp等tomcat自带jar包时,配置文件须申明scope为provided,
只起编译作用,否则程序可能无法正常运行报错!!!
如:
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

2.springMVC 配置文件中如果使用前后缀,return的数据只能包含中间值,如:“success”,解析器自动解析;
        错误写法:"/success.jsp"
        在有前后缀的情况下,若要使用绝对路径进行转发/重定向,则forward或redirect不能省略!如:"forward:/success.jsp"

3.springMVC 中使用@ResponseBody响应乱码问题解决:
(1)单一解决:在@RequestMapping注解中设置contentType:
如: @RequestMapping(value = "/quick4",produces = "application/json;charset=utf-8")

(2)配置解决所有:在spring-mvc配置文件注解自动扫描中添加配置:
    <!--解决响应体中文乱码问题-->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

或者:
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=utf-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2