MVC的action层
action层是业务层的一部分,应用对应的Service层,跳转到指定的页面,也可以接受页面传递的请求数据。
创建ActionForward类,config.xml(config.properties)连接到指定的ActionForward,再传到ActionServlet,ActionServlet使用ActionForward提供的的路径,将控制传递给下一个步骤。
MVC的servlet层
servlet层是属于控制层最底端的部分,可以接收客户端的请求和对应请求作出对应的响应。
config.properties(在WEB-INF文件下,创建后缀名为properties的文件夹)
##\u914D\u7F6E\u5B8C\u6210\u5177\u4F53\u670D\u52A1\u7684action
add=com.zuoy.action.AddAction
list=com.zuoy.action.ListAction
delete=com.zuoy.action.DeleteAction
update=com.zuoy.action.UpdateAction
byid=com.zuoy.action.ByIdAction
ActionServle(继承HttpServlet ,并获取doget,dopost的方法)
public class ActionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求路径 分析路径
String uri=req.getRequestURI();
System.out.println(uri);
String path=uri.substring(uri.lastIndexOf("/")+1, uri.indexOf("."));
//获取配置文件对象中的值
String value=(String)ps.get(path);
try {
//通过类名加载类
Class c=Class.forName(value);
//实例化Action对象
Object obj = c.newInstance();
//调用Action的执行的execute的方法
Action action=(Action)obj;
ActionForward forward=action.execute(req, resp);
//执行跳转
forward.forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建一个配置文件对象用来保存读取出来的配置文件信息
private Properties ps=new Properties();
@Override
public void init() throws ServletException {
//super.init();
try {
ps.load(this.getServletContext().getResourceAsStream("/WEB-INF/config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.ActionForward
public class ActionForward {
//数据显示的地址
private String path;
//跳转方式 是否重定向 默认就是不重定向(默认请求转发)
private boolean isRedirect =false;
public ActionForward(String path, boolean isRedirect) {
super();
this.path = path;
this.isRedirect = isRedirect;
}
//跳转的方法
public void forward(HttpServletRequest req,HttpServletResponse resp) {
try {
if(isRedirect) {//重定向 增删改
resp.sendRedirect(path);
}
else {//请求转发
req.getRequestDispatcher(path).forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在action层中创建一个Action(接口)类(在里面写上执行的方法)
public interface Action {
//执行的方法
public ActionForward execute(HttpServletRequest req,HttpServletResponse resp);
}
再创建其他(增删查改)类(实现 Action(接口类))
//查询
public class ListAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) {
BaseDao bdao = new BaseDao();
创建一个学生对象
Student stu=new Student();
List<Object> list=bdao.listAll(stu);
//System.out.println(list.size());
req.setAttribute("list", list);
return new ActionForward("list.jsp", false);
}
}
//添加
public class AddAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) {
try {
req.setCharacterEncoding("utf-8");
BaseDao bdao = new BaseDao();
Student stu=new Student();
stu.setId(0);
stu.setName(req.getParameter("name"));
stu.setAge(Integer.parseInt(req.getParameter("age")));
stu.setSex(req.getParameter("sex"));
Date date=new Date();
stu.setDate(date);
bdao.add(stu);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ActionForward("add.jsp", false);
}
}
//删除
public class DeleteAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) {
BaseDao bdao = new BaseDao();
int id=Integer.parseInt(req.getParameter("id"));
Student stu = new Student();
stu.setId(id);
bdao.delete(stu);
return new ActionForward("list.do", false);
}
}
//byid
public class ByIdAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) {
BaseDao bdao = new BaseDao();
Student stu=new Student();
stu.setId(Integer.parseInt(req.getParameter("id")));
Object list = bdao.byId(stu);
req.setAttribute("list", list);
System.out.println(list);
return new ActionForward("update.jsp", false);
}
}
//修改
public class UpdateAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) {
try {
req.setCharacterEncoding("utf-8");
BaseDao bdao = new BaseDao();
Student stu = new Student();
stu.setId(Integer.parseInt(req.getParameter("id")));
stu.setName(req.getParameter("name"));
stu.setAge(Integer.parseInt(req.getParameter("age")));
stu.setSex(req.getParameter("sex"));
stu.setDate(new Date());
bdao.update(stu);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new ActionForward("list.do", true);
}
}