@Override public Items getItemById(int id) { Items items = itemMapper.getItemById(id); return items; } |
@RequestMapping("/itemEdit") public ModelAndView itemEdit(HttpServletRequest request) { //从Request中取id String strId = request.getParameter("id"); Integer id = null; //如果id有值则转换成int类型 if (strId != null && !"".equals(strId)) { id = new Integer(strId); } else { //出错 return null; } Items items = itemService.getItemById(id); //创建ModelAndView ModelAndView modelAndView = new ModelAndView(); //向jsp传递数据 modelAndView.addObject("item", items); //设置跳转的jsp页面 modelAndView.setViewName("editItem"); return modelAndView; } |
如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。 |
@RequestMapping("/itemEdit") public String itemEdit(HttpServletRequest request, Model model) { //从Request中取id String strId = request.getParameter("id"); Integer id = null; //如果id有值则转换成int类型 if (strId != null && !"".equals(strId)) { id = new Integer(strId); } else { //出错 return null; } Items items = itemService.getItemById(id); //创建ModelAndView //ModelAndView modelAndView = new ModelAndView(); //向jsp传递数据 //modelAndView.addObject("item", items); model.addAttribute("item", items); //设置跳转的jsp页面 //modelAndView.setViewName("editItem"); //return modelAndView; return "editItem"; } |
@RequestMapping("/itemEdit") public String itemEdit(Integer id, Model model) { Items items = itemService.getItemById(id); //向jsp传递数据 model.addAttribute("item", items); //设置跳转的jsp页面 return "editItem"; } |
@RequestMapping("/updateitem") public String updateItem(Items items) { itemService.updateItem(items); return "success"; } |
注意:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。 |
<filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
@RequestMapping("/queryitem") public String queryItem(QueryVo queryVo) { System.out.println(queryVo.getItems().getName()); System.out.println(queryVo.getItems().getPrice()); return null; } |
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; } } |
<!-- 加载注解驱动 --> <mvc:annotation-driven conversion-service="conversionService"/> <!-- 转换器配置 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.springmvc.convert.DateConverter"/> </set> </property> </bean> |
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd"> <!-- 扫描带Controller注解的类 --> <context:component-scan base-package="cn.itcast.springmvc.controller" /> <!-- 转换器配置 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.springmvc.convert.DateConverter"/> </set> </property> </bean> <!-- 自定义webBinder --> <bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService" /> </bean> <!--注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer" ref="customBinder"></property> </bean> <!-- 注解处理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 加载注解驱动 --> <!-- <mvc:annotation-driven/> --> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- jsp前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- jsp后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans> |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |