26 lines
941 B
Java
26 lines
941 B
Java
package controller;
|
|
|
|
import jakarta.servlet.http.HttpServlet;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
|
|
public class BaseController extends HttpServlet {
|
|
protected void service(HttpServletRequest req, HttpServletResponse resp){
|
|
resp.addHeader( "Access-Control-Allow-Origin","*");
|
|
String uri = req.getRequestURI();
|
|
|
|
uri = uri.substring(uri.lastIndexOf("/")+1);
|
|
//使用反射获取方法来执行
|
|
try {
|
|
Method method = this.getClass().getDeclaredMethod(uri, HttpServletRequest.class, HttpServletResponse.class);
|
|
method.setAccessible(true);
|
|
method.invoke(this, req, resp);
|
|
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|