年前学了注解式开发,年后再写的时候就有点蒙了,主要是注解那里。现在来复习下。
如果在类上面加注解:
@RequestMapping("/Axx")
@Controller
@RequestMapping("/Student")
public class FindListStuWeb {
方法上加注解:
@RequestMapping("/Bxx")
/**
* 模糊查
* @param request
* @param student
* @return
*/
@RequestMapping("/findListStu")
public String findListStu(HttpServletRequest request, Student student){
那么在页面上点击按钮触发的form表单action里面就是:
${pageContext.request.contextPath}/Axx/Bxx/
如果需要传参就直接在后面传值。
方法里面返回页面:
页面:a.jsp
方法:注意方法里最后return的值必须与jsp页面的命名一致
public String findListStu(HttpServletRequest request, Student student){
String sname = student.getSname();
if(sname == null){
sname = "";
}
System.out.println("------------"+sname);
student.setSname("%"+sname+"%");
List<Student> stulist = studentService.FindListStu(student);
request.getSession().setAttribute("stulist",stulist);
return "StudentList";//注意这里return的值需与jsp页面的命名一致
}
前台传参给后台的取值
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<script type="text/javascript">
function add(){
window.location.href="${pageContext.request.contextPath}/Student/pageUrl?pageUrl=StudentEdit";
}
function updateStudent(sid){
window.location.href = "${pageContext.request.contextPath}/Student/findOneStu/"+sid;
}
function deleteStudent(sid){
window.location.href = "${pageContext.request.contextPath}/Student/deleteStu/"+sid;
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/Student/findListStu/" method="post">
请输入姓名: <input name="sname" type="text"/><input value="查询" type="submit">
</form>
<input value="新增" onclick="add()" type="button">
<table border="1" width="100%">
<tr style="text-align: center">
<td>编号</td>
<td>姓名</td>
<td>班级</td>
<td>头像</td>
<td>操作</td>
</tr>
<c:if test="${stulist != null}">
<c:forEach items="${stulist}" var="stu">
<tr style="text-align: center">
<td>${stu.sid}</td>
<td>${stu.sname}</td>
<td>${stu.sclass}</td>
<td><img src="${stu.image}" width="75px" height="75px"></td>
<td>
<button onclick="updateStudent(${stu.sid})">修改</button>
<button onclick="deleteStudent(${stu.sid})">删除</button>
</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
后台接收并取值
/**
* 删除
* @param sid
* @return
*/
@RequestMapping("/deleteStu/{sid}")
public String deleteStu(@PathVariable(value="sid")Integer sid){
int i = this.studentService.deleteByPrimaryKey(sid);
return "redirect:/Student/findListStu";
}