文章目录
1. 日期格式处理
由于SpringMVC的转换器只能将 yyyy/mm/dd
形式的日期输入转为 Date
类型,使用户的输入不能自由,于是对于这种类型我们使用自定义的转换器。
1.1 在控制器中使用对象接收数据
- 添加
lombok
依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
- 定义一个
Book
对象
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Book {
private String name;
private String author;
private double price;
private Date date;
}
1.2 自定义日期转换器
新建一个类来存储日期转换器,内容如下:
public class MyDateConvert implements Converter<String, Date> {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public Date convert(String s) {
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
1.3 配置自定义转换器
在SpringMVC的配置文件 spring-servlet.xml
中将自定义的日期转换器进行配置,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--IoC采用注解配置-->
<context:annotation-config/>
<context:component-scan base-package="com.mr"/>
<!-- 使用自定义的转换器 -->
<mvc:annotation-driven conversion-service="converterFactory"/>
<!-- 将自定义的转换器进行配置 -->
<bean id="converterFactory" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.mr.utils.MyDateConvert"/>
</set>
</property>
</bean>
</beans>
1.4 测试
测试类如下所示,
@RequestMapping("/add")
public void addBook(Book book){
System.out.println("---book add");
System.out.println(book.getDate());
System.out.println(book.getAuthor());
System.out.println(book.getName());
System.out.println(book.getPrice());
}
在网址 http://localhost:8080/springmvc/book-add.jsp
中输入信息并提交后,可以看到控制台输出如下:
2. 解决中文乱码问题
2.1 前端编码
-
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
-
HTML页面:
<meta charset="UTF-8">
2.2 服务器编码
-
tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
2.3 设置SpringMVC的编码方式
-
在web.xml中配置SpringMVC编码过滤器的编码方式
<filter> <filter-name>EncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3. 统一异常处理
在我们的应用系统运行的过程中,可能由于运行环境、用户操作、资源不足等各方面的原因导致系统出现异常(HTTP状态异常、Exception);如果系统出现了异常,这些异常将会通过浏览器呈现给用户,而这种异常的显示是没有必要,因此我们可以在服务器进行特定的处理——当系统出现异常之后,呈现给用户一个统一的、可读的的异常提示页面。
3.1 HTTP异常状态统一处理
HTTP Status 404
-
创建一个用于进行异常提示的页面:404.jsp
-
在web.xml中进行配置:
<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
3.2 Java代码异常的统一处理
方式一
-
创建异常提示页面:err.jsp
-
在web.xml中进行配置
<error-page> <exception-type>java.lang.NumberFormatException</exception-type> <location>/err.jsp</location> </error-page>
方式二
-
使用异常处理类进行统一处理
@ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(NullPointerException.class) public String nullHandler(){ return "/err1.jsp"; } @ExceptionHandler(NumberFormatException.class) public String formatHandler(){ return "/err2.jsp"; } }