SpringMVC前后端参数传递与接收SpringMVC前后端参数传递与接收
前端JSP使⽤了JSTL语⾔与OGNL表单式,需要对其有⼀定的了解
(1)默认⽀持参数类型
使⽤下⾯的类进⾏接收,接收参数⽅法同Servlet中接收参数⽅法⼀致
HttpServletRequest request
HttpServletResponse response,
HttpSession session
//前端请求的url地址
@RequestMapping(value = "/parm/parm1.action")
public ModelAndView parmTransport(HttpServletRequest request,
HttpServletResponse response,
HttpSession session){
String name = Parameter("name");
System.out.println(name);
return null;
}
(2)绑定简单类型
即前端传⼊的参数名与后端接收参数的⽅法名中的形参的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;
}
(2)绑定简单类型---传⼊参数名与接收参数名不⼀致
即前端传⼊的参数名称与后端接收参数名称不⼀致,使⽤下⾯的注解可以实现数据匹配
@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;
}
(3)绑定POJO---实体类,解决多参数问题,且pojo可以嵌套,通过OGNL表达式赋值后端代码
//绑定pojo类型,pojo包装pojo
@RequestMapping(value = "/product/toEdit.action")
public ModelAndView toEdit(Integer id){
Product productById = ProductById(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="java.sun/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="java.sun/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品信息</title>
</head>
<body>
<form id="itemForm" action="${tPath }/product/updateProduct.action" method="post">  <input type="hidden" name="id" value="${item.id}" /> 修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品⽣产⽇期</td>
<td><input type="text" name="createTime"
value="<fmt:formatDate value="${ateTime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
</tr>
<tr>
<td>商品简介</td>
<td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td>商品简介</td>
<td><textarea rows="3" cols="30" name="order.id">${der.id}</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
(4)解决前端传⼊参数中⽂乱码问题-----WEB-ING下的l配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_4_0.xsd"        version="4.0">
<!--配置SpringMvc-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置Spring容器创建-->
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<!--解决乱码问题-->
<filter>
<filter-name>endocdingFilter</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>endocdingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(5)⾃定义参数绑定-----⽇期格式转换
1:⾃定义converter转换器
package verter;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.pe.TypeFactory;
import verter.Converter;
SimpleDateFormat;
import java.util.Date;
springmvc常用标签public class DateConverter implements Converter<String ,Date> {
@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;
}
}
2:l配置⽂件中配置转换器
<!--配置转换器-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<bean name="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">
<list>
<bean name="converter" class="verter.DateConverter"></bean>
</list>
</property>
</bean>
⾼级参数绑定
(1)绑定数组----前端批量传⼊数据到后台
前端代码:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。