》#千锋逆战#
修改使用了SpringMVC的问号传参,删除操作使用了路径传参。
对比问号传参与路径传参:
问号传参,需要使用问号来拼接参数,在接受方,使用request.getParameter(“key”)来获取问号所传递过来的值,如果数据类型不为String,还需要手动转换。可以传递多个值,如果使用多个值,使用&来拼接,不会改变路径级别
路径传参,使用路径符号来传递参数,优点,可以不用做类型转换来直接获取其值。
路径传参也可以使用统配规则,如果同时统配和具体的url都满足,则以最具体的url来处理该请求。
代码实现:
Emp.java
package com.qfedu.bean;
public class Emp {
private int eid;
private String name;
private double salary;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Emp{");
sb.append("eid=").append(eid);
sb.append(", name='").append(name).append('\'');
sb.append(", salary=").append(salary);
sb.append('}');
return sb.toString();
}
public Emp() {
}
public Emp(int eid, String name, double salary) {
this.eid = eid;
this.name = name;
this.salary = salary;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
EmpController.java
RequestMapping
可以通过method来区分不同的请求方式
@RequestMapping(value = “/updateEmp”, method = RequestMethod.POST)代表处理post请求
@RequestMapping(value = “/updateEmp”, method = RequestMethod.GET)代表处理get请求
GETMapping,可以简化代码,专门用来处理get请求(4.3以后的Spring版本可用)
PostMapping,可以简化代码,专门用来处理post请求(4.3以后的Spring版本可用)
package com.qfedu.controller;
import com.qfedu.bean.Emp;
import com.qfedu.service.EmpService;
import com.qfedu.service.impl.EmpServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/emp")
public class EmpController {
private EmpService empService = new EmpServiceImpl();
@RequestMapping("/emps")
public String getUserPage(Model model, HttpSession session){
List<Emp> list = empService.getAllEmps();
model.addAttribute("list",list);
session.setAttribute("list",list);
return "users.jsp";
}
@RequestMapping("/getEmpById")
public String getEmpById(HttpServletRequest request,Model model){
String seid = request.getParameter("eid");
int eid=seid==null?0:Integer.parseInt(seid);
Emp emp = empService.getEmpById(eid);
model.addAttribute("emp",emp);
return "updateEmp.jsp";
}
//@RequestMapping("/updateEmp")
@PostMapping("/updateEmp")
public String updateEmp(Emp emp){
boolean flg = empService.updateEmp(emp);
if (flg) {
return "redirect:/emp/emps";
}
return "";
}
@GetMapping("/deleteById/{eid}")
public String deleteById(@PathVariable int eid){
boolean flg = empService.deleteById(eid);
if(flg) {
return "redirect:/emp/emps";
}
return "";
}
}
EmpService.java
package com.qfedu.service;
import com.qfedu.bean.Emp;
import java.util.List;
public interface EmpService {
List<Emp> getAllEmps();
Emp getEmpById(int eid);
boolean updateEmp(Emp emps);
boolean deleteById(int eid);
}
EmpServiceImpl.java
package com.qfedu.service.impl;
import com.qfedu.bean.Emp;
import com.qfedu.dao.EmpDao;
import com.qfedu.dao.impl.EmpDaoImpl;
import com.qfedu.service.EmpService;
import java.util.List;
public class EmpServiceImpl implements EmpService {
private EmpDao empDao = new EmpDaoImpl();
@Override
public List<Emp> getAllEmps() {
return empDao.getAllEmps();
}
@Override
public Emp getEmpById(int eid) {
return empDao.getEmpById(eid);
}
@Override
public boolean updateEmp(Emp emps) {
return empDao.updateEmp(emps);
}
@Override
public boolean deleteById(int eid) {
return empDao.deleteById(eid);
}
}
EmpDao.java
package com.qfedu.dao;
import com.qfedu.bean.Emp;
import java.util.List;
public interface EmpDao {
List<Emp> getAllEmps();
Emp getEmpById(int eid);
boolean updateEmp(Emp emps);
boolean deleteById(int eid);
}
EmpDaoImpl.java
package com.qfedu.dao.impl;
import com.qfedu.bean.Emp;
import com.qfedu.dao.EmpDao;
import java.util.ArrayList;
import java.util.List;
public class EmpDaoImpl implements EmpDao {
private static List<Emp> emp = new ArrayList();
static {
for (int i = 0; i < 50; i++) {
emp.add(new Emp(i+1,"name"+i,8900+i*100));
}
}
@Override
public List<Emp> getAllEmps() {
return emp;
}
@Override
public Emp getEmpById(int eid) {
return emp.get(eid-1);
}
@Override
public boolean updateEmp(Emp emps) {
try{
emp.set(emps.getEid()-1,emps);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
@Override
public boolean deleteById(int eid) {
try {
emp.remove(eid-1);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
}
user.jsp
<c:if test="${list != null}">
<table border="1" align="center" width="80%">
<tr>
<th>eid</th>
<th>name</th>
<th>salary</th>
<th>manage</th>
</tr>
<c:forEach items="${list}" var="e">
<tr>
<td> ${e.eid}</td>
<td> ${e.name}</td>
<td> ${e.salary}</td>
<td> <a href="/emp/getEmpById?eid=${e.eid}">update</a> <a href="/emp/deleteById/${e.eid}">delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
updateEmp.jsp
<form method="post" action="/emp/updateEmp">
eid:<input type="text" name="eid" value="${emp.eid}" readonly="readonly"/><br/>
name:<input type="text" name="name" value="${emp.name}"/><br/>
salary:<input type="text" name="salary" value="${emp.salary}"/><br/>
<input type="submit" value="submit"/><br/>
</form>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<!--<property name="suffix" value=".jsp" />-->
</bean>
<!--过滤静态资源-->
<mvc:default-servlet-handler />
<!--上下文组件扫描-->
<context:component-scan base-package="com.qfedu.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--bean 的id 属性值不能包含特殊字符
name可以,所以路径需要name来标识一个控制器的路径
指定name对应的路径交给哪个控制器来进行具体处理
-->
<bean name="/ProductInput" class="com.qfedu.controller.ProductInputController"/>
<bean name="/SaveProductController" class="com.qfedu.controller.SaveProductController"/>
</beans>