关于springmvc+mybatis 在web-inf下jsp超链接传参问题

1.jsp页面必须添加指令

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

2。必要传参格式

<spring:url value="/teacher/delInfo/${teacher.tid }" var="deleteURL" />

<a href="${deleteURL }">delete</a></td>

3.el表达式设为解析

4. 控制器接受格式,必须使用model,各参数包括model必须是方法的参数

@RequestMapping(value="/delInfo/{id}")

public String del(@PathVariable ("id") int id,Model model) {
boolean bool=teacherService.delTeacher(id);
if(bool==false) {
System.out.println("删除失败");
}else {
System.out.println("删除成功");
}

List<Teacher> list=teacherService.findAllTeacher();
model.addAttribute("allTeacherInfo", list);
return "index";

}


代码示例:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>index</title>
</head>
<body>
<h3>index</h3>
<table border="1">
<thead>添加</thead>
<c:forEach items="${allTeacherInfo}" var="teacher">
<tr>
<td>${teacher.tid}</td>
<td>${teacher.tname}</td>
<td>${teacher.taddress}</td>
<td>
<spring:url value="/teacher/delInfo/${teacher.tid }" var="deleteURL" />
<a href="${deleteURL }">delete</a></td>
<td>
<spring:url value="/teacher/editInfo/${teacher.tid}" var="editURL" />
<a href="${editURL }">edit</a></td>
</tr>
</c:forEach>
</table>
</body>

</html>


代码示例 edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%-- <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>   --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<spring:url value="/teacher/doeditInfo/${id}" var="saveURL" />


<form action="${saveURL }" method="post">


<input type="text" name="tname"/><br>


<input type="text" name="taddress"/><br>
<input type="submit"><input type="reset">
</form>
</body>

</html>


代码示例  TeacherController.jsp

package com.slsd.controller;


import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.slsd.entity.Teacher;
import com.slsd.service.ITeacherService;


@Controller
@RequestMapping("/teacher")
public class TeacherController {
@Resource
private ITeacherService teacherService;


/*
* // /teacher/test?id=1

* @RequestMapping(value = "/test", method = RequestMethod.GET) public String
* test(HttpServletRequest request, Model model) { int tid =
* Integer.parseInt(request.getParameter("id")); System.out.println("tid:" +
* tid); Teacher teacher = null; if (tid == 1) { teacher = new Teacher();
* teacher.setTid(1); teacher.setTname("zs"); }

* model.addAttribute("teacher", teacher); return "index"; }

* // /teacher/showTeacher?id=1

* @RequestMapping(value = "/showTeacher", method = RequestMethod.GET) public
* String toIndex(HttpServletRequest request, Model model) { int tid =
* Integer.parseInt(request.getParameter("id")); System.out.println("tid:" +
* tid); Teacher teacher = this.teacherService.findTeacherById(tid);
* model.addAttribute("teacher", teacher); return "showTeacher"; }

* // /teacher/showTeacher2?id=1

* @RequestMapping(value = "/showTeacher2", method = RequestMethod.GET) public
* String toIndex2(@RequestParam("id") String id, Model model) { int tid =
* Integer.parseInt(id); System.out.println("tid:" + tid); Teacher teacher =
* this.teacherService.findTeacherById(tid); model.addAttribute("teacher",
* teacher); return "showTeacher"; }

* // /teacher/showTeacher3/{id}

* @RequestMapping(value = "/showTeacher3/{id}", method = RequestMethod.GET)
* public String toIndex3(@PathVariable("id") String id, Map<String, Object>
* model) { int tid = Integer.parseInt(id); System.out.println("tid:" + tid);
* Teacher teacher = this.teacherService.findTeacherById(tid);
* model.put("teacher", teacher); return "showTeacher"; }


* // json格式


* // 文件上传、
*/


@RequestMapping(value="/showAllTeacher")
public String toIndex(Model model) {
List<Teacher> list=teacherService.findAllTeacher();
model.addAttribute("allTeacherInfo", list);
return "index";
}

@RequestMapping(value="/delInfo/{id}")
public String del(@PathVariable ("id") int id,Model model) {
boolean bool=teacherService.delTeacher(id);
if(bool==false) {
System.out.println("删除失败");
}else {
System.out.println("删除成功");
}

List<Teacher> list=teacherService.findAllTeacher();
model.addAttribute("allTeacherInfo", list);
return "index";
}

@RequestMapping(value="/editInfo/{id}")
public String edit(@PathVariable ("id") int id,Model model) {
System.out.println("逐步跳转");
model.addAttribute("id",id);
return "edit";
}

@PostMapping(value="/doeditInfo/{id}")
public String doedit(@PathVariable ("id") int id,@RequestParam("tname") String tname,@RequestParam("taddress") String taddress,   Model model) {
Teacher teacher=new Teacher(id,tname,taddress);
// teacher.setTid(id);

System.out.println(teacher.getTid());
System.out.println(teacher.getTname());
System.out.println(teacher.getTaddress());
boolean bool=teacherService.editTeacher(teacher);
if(bool==false) {
System.out.println("xiugai失败");
}else {
System.out.println("修改成功");
}

List<Teacher> list=teacherService.findAllTeacher();
model.addAttribute("allTeacherInfo", list);
return "index";
}

}



猜你喜欢

转载自blog.csdn.net/weixin_42327945/article/details/80698664
今日推荐