发表于: 2020-03-15 22:11:49

1 1322


今日做的内容不多


校验器、当名字和科目不存在时返回错误界面

public String findStudentByName(Model model, HttpServletRequest request,
String name,String subject,String school,
@Validated Student student,BindingResult result) throws Exception {
if (name.length() == && result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
for (ObjectError error : errors) {
log.error(error.getDefaultMessage());
//将错误信息传至页面
       model.addAttribute("allErrors", errors);
log.error("执行了if语句块");
return "errorFind";
}
try {
List<Student> students = Collections.singletonList(studentService.findStudentByName(name));
model.addAttribute("student", students);
log.info(name);
log.info(student);
return "findStudent";
catch (Exception e) {
e.printStackTrace();
return "errorStatus";
}
}
@NotBlank(message = "姓名不能为空")
private String name;

private long qq;
@NotBlank(message = "科目不能为空")
<%@ page contentType="text/html;charset=UTF-8language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--error接受错误信息--%>
<c:if test="${allErrors!=null}">
<c:forEach items="${allErrors}var="Error">
${Error.defaultMessage}
   </c:forEach>
</c:if>
<href="<c:url value="/student/findStudent?id=${student.id}"/>">返回</a>
</body>
</html>
<!-- validator校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- 校验器,使用Hibernate校验器 -->
   <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!-- 指定校验使用的资源文件,在文件中配置效验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties文件 -->
</bean>

2.分页查询

<p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
       页,总 ${pageInfo.total } 条记录</div></p>
<href="<c:url value="/student/findAllStudent?pageNo=${pageInfo.firstPage}"/>">${pageInfo.firstPage}</a>
<c:if test="${pageInfo.pages>1}">
<href="<c:url value="/student/findAllStudent?pageNo=${pageInfo.firstPage+1}"/>">${pageInfo.firstPage+1}</a>
</c:if>
<c:if test="${pageInfo.hasPreviousPage }">
<href="<c:url value="/student/findAllStudent?pageNo=${pageInfo.pageNum-1}"/>">上一页</a>
</c:if>
<c:if test="${pageInfo.hasNextPage }">
<href="<c:url value="/student/findAllStudent?pageNo=${pageInfo.pageNum+1}"/>">下一页</a>
</c:if>
<c:if test="${pageInfo.pages>2}">
<href="<c:url value="/student/findAllStudent?pageNo=${pageInfo.lastPage}"/>">${pageInfo.lastPage}</a>
</c:if>
@RequestMapping(value = "findAllStudent" ,method =RequestMethod.GET)
public String findAllStudent(Model model,@RequestParam(defaultValue = "1",required = true,
value = "pageNo") Integer pageNo) {
//每页显示记录数为5
   Integer pageSize=5;
PageHelper.startPage(pageNo,pageSize);
List<Student> students = studentService.findAllStudent();
PageInfo<Student> pageInfo =new PageInfo<Student>(students);
model.addAttribute("pageInfo", pageInfo);
return "allStudent";
}

今日问题 暂无


返回列表 返回列表
评论

    分享到