2).在pom.xml的代码如下:
<%-- Created by IntelliJ IDEA. User: AdAir Date: 2020/7/1 0001 Time: 9:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>请求参数绑定</title> </head> <body> <%--自定义类型转换器 --%> <form action="param/saveUser" method="post"> 用户姓名:<input type="text" name="uname" /><br/> 用户年龄:<input type="text" name="age" /><br/> 用户生日:<input type="text" name="date" /><br/> <input type="submit" value="提交" /> </form> </body> </html>3).创建请求参数绑定的处理类代码如下:
package com.txw.controller; import com.txw.domain.Account; import com.txw.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** *请求参数绑定的处理类 * @author Adair */ @Controller @RequestMapping(value = "/param") @SuppressWarnings("all") // 注解警告信息 public class ParamController { /** * 自定义类型转换器 * @param user * @return */ @RequestMapping("/saveUser") public String saveUser(User user){ System.out.println("执行了..."); System.out.println(user); return "success"; } }4).使用TomCat运行结果如图:
5).通过浏览器访问http://localhost:8080/params.jsp结果如图所示:
6).填写如图所示相应的信息,并点击提交。 7).会跳转到如图所示的界面:
8).控制台打印结果如图所示: 注意如果使用2020-02-26格式的话,点击提交会出现如图所示的错误: 并且saveUser()方法没有执行。 3.自定义类型转换器代码编写 1).创建把字符串转换日期的代码如下:
package com.txw.ultis; import org.springframework.core.convert.converter.Converter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * 把字符串转换日期 * @author Adair */ @SuppressWarnings("all") // 注解警告信息 public class StringToDateConverter implements Converter<String,Date>{ /** * String source 传入进来字符串 * @param source * @return */ @Override public Date convert(String source) { // 判断 if(source == null){ throw new RuntimeException("请您传入数据"); } DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { // 把字符串转换日期 return df.parse(source); } catch (Exception e) { throw new RuntimeException("数据类型转换出现错误"); } } }2).在springmvc.xml配置类型转换器的代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.txw"/> <!-- 视图解析器对象 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--文件所在的目录--> <property name="prefix" value="/WEB-INF/pages/"/> <!--文件的后缀名--> <property name="suffix" value=".jsp"/> </bean> <!--配置自定义类型转换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.txw.ultis.StringToDateConverter"/> </set> </property> </bean> <!-- 开启SpringMVC框架注解的支持 --> <mvc:annotation-driven conversion-service="conversionService"/> </beans>3). 使用TomCat运行结果如图: 4).通过浏览器访问http://localhost:8080/params.jsp结果如图所示: 5).填写如图所示相应的信息,并点击提交。 6).提交会跳转到如图所示的界面: 7).控制台打印结果如图所示: 4. 在控制器中使用原生的ServletAPI对象。 只需要在控制器的方法参数定义HttpServletRequest和HttpServletResponse对象。 1).在params.Xml的代码如下:
<%-- Created by IntelliJ IDEA. User: AdAir Date: 2020/7/1 0001 Time: 9:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>请求参数绑定</title> </head> <body> <a href="param/testServlet">Servlet原生的API</a> </body> </html>2).创建请求参数绑定的处理类的代码如下:
package com.txw.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** *请求参数绑定的处理类 * @author Adair */ @Controller @RequestMapping(value = "/param") @SuppressWarnings("all") // 注解警告信息 public class ParamController { /** * 原生的API * @return */ @RequestMapping("/testServlet") public String testServlet(HttpServletRequest request, HttpServletResponse response){ System.out.println("执行了..."); System.out.println(request); HttpSession session = request.getSession(); System.out.println(session); ServletContext servletContext = session.getServletContext(); System.out.println(servletContext); System.out.println(response); return "success"; } }3).使用TomCat运行结果如图:
4).通过浏览器访问http://localhost:8080/params.jsp结果如图所示: 5).如图所示,并点击Servlet原生的API.会跳转到如图所示的界面: 6).控制台打印结果如图所示: