使用下面的类进行接收,接收参数方法同Servlet中接收参数方法一致
HttpServletRequest request
HttpServletResponse response,
HttpSession session
//前端请求的url地址
@RequestMapping(value = "/parm/parm1.action")
public ModelAndView parmTransport(HttpServletRequest request,
HttpServletResponse response,
HttpSession session){
String name = request.getParameter("name");
System.out.println(name);
return null;
}
即前端传入的参数名与后端接收参数的方法名中的形参的name一致,可以直接接收参数
后端代码,前端使用input标签传参,确保input标签的name与后台接收参数的name一致
支持的类型如下:
整型:Integer、int
字符串:String
单精度:Float、float
双精度:Double、double
布尔类型:Boolean,Boolean
@RequestMapping(value = "/parm/parm2.action")
public ModelAndView parmTransport2(String name ){
System.out.println(name);
return null;
}
即前端传入的参数名称与后端接收参数名称不一致,使用下面的注解可以实现数据匹配
@RequestParam
value属性表示前端传入的值,required表示是否为空,defaultValue 属性表示默认值
//参数名不一致时
@RequestMapping(value = "/parm/parm3.action")
public ModelAndView parmTransport3(@RequestParam(value = "name2",required = true) String name ){
System.out.println(name+);
return null;
}
后端代码
//绑定pojo类型,pojo包装pojo
@RequestMapping(value = "/product/toEdit.action")
public ModelAndView toEdit(Integer id){
Product productById = productService.getProductById(id); //根据id查找product实体
ModelAndView modelAndView = new ModelAndView();
Order order = new Order(); //新建一个order实体,并赋值
order.setId(55);
productById.setOrder(order); //为product实体中的order实体赋值
//item保持一致,与前端页面
modelAndView.addObject("item",productById);
modelAndView.setViewName("/static/editProduct.jsp"); //返回前端显示
return modelAndView;
}
前端代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
修改商品信息
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.action
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
endocdingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
endocdingFilter
/*
package com.hxy.converter;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter {
@Override
public Date convert(String str) {
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
try {
date = simpleDateFormat.parse( str );
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
查询商品列表
//获取前端传入的数组对象
@RequestMapping(value = "/product/bothDelete.action")
public ModelAndView bothDelete(Integer[] ids){
for (Integer i :ids){
System.out.println(i);
}
//模拟假数据测试
List list = new ArrayList();
list= productService.getProductList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("productList",list);
modelAndView.setViewName("/static/productList1.jsp");
return modelAndView;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
查询商品列表
//批量传入对象
@RequestMapping(value = "/product/bothUpdate.action",method = {RequestMethod.GET,RequestMethod.POST})
public ModelAndView bothupdate(QueryVo queryVo){
for (Product i :queryVo.getProductList()){
System.out.println(i);
}
//模拟假数据测试
List list = new ArrayList();
list= productService.getProductList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("productList",list);
modelAndView.setViewName("/static/productList2.jsp");
return modelAndView;
}
@RequestMapping(value = {"/product/productList2.action","/productList2.action"})
@Controller
@RequestMapping("/product/")
public class ProductController1 {
@Autowired
private ProductService productService;
@RequestMapping(value = "productList1.action")
public ModelAndView itemList(){
List list = new ArrayList();
list= productService.getProductList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("productList",list);
modelAndView.setViewName("/static/productList1.jsp");
return modelAndView;
}}
@RequestMapping(value = "/product/bothUpdate.action",method = {RequestMethod.GET,RequestMethod.POST})