这是我的博客网址
https://blog.csdn.net/qq_43910202
在我做项目的过程中,我遇到一个相对棘手的难题。我想利用form表单,向后台提交数据的时候,总会跳转到另外一个页面,让我很苦恼。
这是我做的一个请假系统(内容是我自己编的,不是真实的信息,只作为参考)
当我点击选项并提交之后系统会跳转到一个页面,这个问题苦恼了好久。
这是前端页面,我用jsp写的,请大家参考。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--这里引入c标签--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师管理</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body style = "background-image:url(../static/1.jpg)">
<table class="table table-striped table-bordered">
<caption><h2>欢迎进入教师管理</h2>已经请假的学生名单<h5></h5></caption>
<tr><th>ID</th><th>学生姓名</th><th>学生性别</th><th>手机号</th><th>请假原因</th><th>请假时间</th><th>请假状态</th><th>操作</th></tr>
<c:forEach items="${data}" var="item">
<tr>
<td>${item.id}</td>
<td>${item.studentName}</td>
<td>${item.studentSex}</td>
<td>${item.studentPhone}</td>
<td>${item.studentReason}</td>
<td>${item.studentTime}</td>
<td>${item.judge}</td>
<td> <form action="<%=request.getContextPath() %>/leave/shi" method="post" >
<p>是否销假</p>
<b>是</b><input type="radio" name="choice" value="${item.studentName}1">
<b>否</b><input type="radio" name="choice" value="${item.studentName}0">
<input type="submit" value="提交">
</form>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
接下来是Controller文件
redirect是我用来解决问题的方法,后面加的是路径。这种重定向的方法可以帮我解决页面跳转的问题。
/*
* 学生已经请假的
* */
@RequestMapping("findByAlready")
public String findByAlready(Model model,Leave leave) {
List<Leave> data=leaveService.findListByAlready();
System.out.println(data+"~~");
model.addAttribute("data",data);
return "leaveAlready";
}
/*
* 是否批准
* */
@RequestMapping("/shi")
public String findListByAlready(@RequestParam("choice") String choice) {
System.out.println("~~"+choice+"~~");
if(!choice.equals("")&&choice.length()>1) {
if(choice.charAt(choice.length()-1)=='1') {//销假
System.out.println("销假成功!");
int num = leaveService.disAgree(choice.substring(0,choice.length()-1));
System.out.println(num);
}
}
return "redirect:/leave/leave/findByAlready";//redirect是重定向
}
/*Author:严天贺
*
* */
这是我成功之后的页面,路径依然没变。
小结:
做项目时,每个人都会遇到不同的Bug,只要思路开阔,会有很多种方法,帮助我们处理这些Bug,千万不能崩溃。一旦我们崩了,那自己的系统也会崩掉。