参数绑定
默认支持的参数
SpringMVC 有支持的默认参数类型,我们直接在形参上给出这些默认类型的声明,就能直接使用了。如下:
1 HttpServletRequest 对象
2 HttpServletResponse 对象
3 HttpSession 对象
4 Model/ModelMap 对象
pojo类型绑定
需求:
根据学院编号查询学院。
mybatis持久层
<resultMap id="facultyCustomMap" type="com.pesystem.vo.FacultyCustom" extends="com.pesystem.mapper.FacultyMapper.BaseResultMap"/>
<select id="selectFacultyByFacultyId" resultMap="facultyCustomMap">
SELECT
<include refid="com.pesystem.mapper.FacultyMapper.Base_Column_List" />
from faculty where
faculty_id = #{facultyCustom.facultyId}
</select>
测试mapper层代码
package com.pesystem.mapper;
import com.pesystem.vo.FacultyCustom;
import com.pesystem.vo.FacultyQueryVo;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:springConfig/spring-dao.xml"})
public class FacultyCustomMapperTest {
//获取日志记录器Logger,名字为本类类名
private static Logger log = Logger.getLogger(String.valueOf(FacultyMapperTest.class));
@Autowired
private FacultyCustomMapper facultyCustomMapper;
@Test
public void testInsert(){
FacultyQueryVo facultyQueryVo = new FacultyQueryVo();
FacultyCustom facultyCustom = new FacultyCustom();
facultyCustom.setFacultyId(3);
facultyQueryVo.setFacultyCustom(facultyCustom);
FacultyCustom facultyCustom1 = facultyCustomMapper.selectFacultyByFacultyId(facultyQueryVo);
log.info(facultyCustom1);
}
}
测试结果:
INFO [main] - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO [main] - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7c16905e, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2a2d45ba, org.springframework.test.context.support.DirtiesContextTestExecutionListener@2a5ca609, org.springframework.test.context.transaction.TransactionalTestExecutionListener@20e2cbe0, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@68be2bc2] INFO [main] - Loading XML bean definitions from class path resource [springConfig/spring-dao.xml]
INFO [main] - Refreshing org.springframework.context.support.GenericApplicationContext@e45f292: startup date [Tue Aug 21 09:43:48 CST 2018]; root of context hierarchy
INFO [main] - Loading properties file from class path resource [db.properties]
INFO [main] - 3默认院系
INFO [Thread-1] - Closing org.springframework.context.support.GenericApplicationContext@e45f292: startup date [Tue Aug 21 09:43:48 CST 2018]; root of context hierarchy
service层
接口和实现
public interface FacultyCustomService {
public FacultyCustom selectFacultyByFacultyId(FacultyQueryVo facultyQueryVo);
}
@Override
public FacultyCustom selectFacultyByFacultyId(FacultyQueryVo facultyQueryVo){
return facultyCustomMapper.selectFacultyByFacultyId(facultyQueryVo);
}
这一层没有什么处理所以就忽略测试
handler
@RequestMapping("/selectFacultyById.action")
public ModelAndView selectFacultyById(FacultyQueryVo facultyQueryVo){
ModelAndView modelAndView = new ModelAndView();
FacultyCustom facultyCustom = facultyCustomService.selectFacultyByFacultyId(facultyQueryVo);
modelAndView.setViewName("selectFaculty");
modelAndView.addObject("facultyCustom",facultyCustom);
return modelAndView;
}
前端jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/8/20
Time: 20:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>综合学院条件</title>
</head>
<script src="js/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function(){
$("#selectFacultiesByName").click(function(){
path="${pageContext.request.contextPath}/faculty/selectFacultiesByName.action";
$('#facultyForm').attr("action", path).submit();
});
$("#selectFacultyById").click(function(){
path="${pageContext.request.contextPath}/faculty/selectFacultyById.action";
$('#facultyForm').attr("action", path).submit();
});
});
</script>
<body>
<form id="facultyForm" action="${pageContext.request.contextPath}/faculty/selectFacultiesByName.action" method="get">
<label>查询条件</label><br/>
<label>模糊查询名字:</label><input type="text" name="facultyCustom.facultyName"><br>
<input type="button" value="模糊查询" name="selectFacultiesByName" id="selectFacultiesByName"><br>
<label>精准id:</label><input type="text" name="facultyCustom.facultyId"><br>
<input type="button" value="精准查询" name="selectFacultyById" id="selectFacultyById">
</form>
</body>
</html>
结果页面
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${facultyCustom!=null} ">
<c:out value="${facultyCustom}"></c:out>
</c:if>
</body>
</html>
测试结果
怎么都不能显示结果,但是去掉判断c:if之后,就能输出。可推出条件错误,发现原来是多了两个空格,导致异常。
整理之后结果:

简单类型绑定
需求:
根据学院编号查询学院。(只需要传入facultyId过来)
mapper只是controller输入参数区别比较大,这里只用mapper的单表实现。就没有测试,主要展示controller
controller
@RequestMapping("/selectFacultyById2.action")
public String selectFacultyById2(Model model, @RequestParam("facultyCustom.facultyId") Integer facultyId){
Faculty faculty = facultyService.selectFacultyByFacultyId(facultyId);
model.addAttribute("facultyCustom",faculty);
return "selectFaculty";
}
测试结果:

这里还发生了一个小插曲,传入参数我用成了ModelAndView导致数据一直存入不进去,还是没认真啊
这里最好使用@RequestParam因为一张form表单,我们很多时候有多种功能,如果pojo和简单类型参数一起用,肯定需要@RequestParam注解,前端设置name最好优先满足pojo~~
自定义参数绑定
需求添加学生信息:
学生信息里面包括姓名学号生日等等。主要是使用生日完成自定义参数
表现层实现:
普通的表单提交

controller层:
先不使用自定义的参数绑定

参数没有绑定成功。
自定义参数绑定实现日期类型绑定
将String 转为java.util.Date
向处理器适配器中注入自定义参数绑定组件
在controller包下面创建自定义参数绑定包,创建类。该类需要实现Converter<E,T>接口。注意导入的包
import org.springframework.core.convert.converter.Converter;
类创建详细如下:
package com.pesystem.controller.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CustomDateConverter implements Converter<String,Date> {
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
在处理器映射器中,我们需要注入
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!--自定义参数绑定-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<!--创建自定义绑定-->
<bean class="com.pesystem.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>
测试结果:

参数绑定--数组
需求同时查询多个院系,只实现controller层,完成参数绑定即可。
数组和简单类型参数绑定没有太大区别,主要是controller层的形参用数组类型,前端用多选框将多个id传过就可以了。
参数绑定--List
需求批量添加学院
扩展vo类
package com.pesystem.vo;
import com.pesystem.po.Student;
import java.util.List;
public class StudentQueryVo {
private StudentCustom studentCustom;
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public StudentCustom getStudentCustom() {
return studentCustom;
}
public void setStudentCustom(StudentCustom studentCustom) {
this.studentCustom = studentCustom;
}
}
controller层
@RequestMapping("/addFaculties.action")
public ModelAndView addFaculties(FacultyQueryVo facultyQueryVo){
System.out.println(facultyQueryVo);
return null;
}
jsp
最主要就是设置name的时候一定要设置好下标,当然也可通过c:foreach或者js,jquery设置name的名字。只要数组名对应,下标不错,属性正确那么还是没什么问题。
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/8/21
Time: 17:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/faculty/addFaculties.action">
<table>
<tr>
<td>学院名称</td>
<td><input type="text" name="faculties[0].facultyName"></td>
</tr>
<tr>
<td>学院名称</td>
<td><input type="text" name="faculties[1].facultyName"></td>
</tr>
</table>
<button type="submit" value="提交"/>
</form>
</body>
</html>
测试结果:

网友评论