基于Struts自定义MVC-1

时间:2022-04-15 20:03:33

                     自定义MVC        
数据库:Oracle
表:User(id,uname,upwd)
自定义Struts框架

一、定义Action接口

1             import javax.servlet.http.*;
2
3             public interface Action {
4                 /**
5                  * 执行方法
6                  * @return
7                  */
8                 String execute(HttpServletRequest request,HttpServletResponse response);
9             }

二、根据特定功能实现Action接口(这里用User表添加用户)

 1         import javax.servlet.http.*;
 2
 3         import com.biz.UserBiz;
 4         import com.biz.impl.UserBizImpl;
 5         import com.entity.User;
 6         import com.mystruts.Action;
 7         /**
 8          * 注册Action 完成注册功能  调用业务层  返回页面
 9          * @author Administrator
10          *
11          */
12         public class RegAction implements Action {
13
14             @Override
15             public String execute(HttpServletRequest request,
16                     HttpServletResponse response) {
17                 //获取请求内容
18                 String uname = request.getParameter("uname");
19                 String upwd = request.getParameter("upwd");
20                 //创建User 对象  创建UserBizImpl对象
21                 User user = new User(uname,upwd);
22                 UserBiz userBiz = new UserBizImpl();
23                 //调用新增用户的方法
24                 if(userBiz.reg(user)){
25                     request.getSession().setAttribute("login", user);
26                     return "login.jsp";
27                 }else{
28                     request.getSession().setAttribute("msg", "注册失败!");
29                     return "register.jsp";
30                 }
31             }
32
33         }

三、ActionFilter(负责请求的转发,将用户的请求交给相应的action进行处理)

 1   import java.io.IOException;
 2
 3         import javax.servlet.Filter;
 4         import javax.servlet.FilterChain;
 5         import javax.servlet.FilterConfig;
 6         import javax.servlet.ServletException;
 7         import javax.servlet.ServletRequest;
 8         import javax.servlet.ServletResponse;
 9         import javax.servlet.http.HttpServletRequest;
10         import javax.servlet.http.HttpServletResponse;
11
12         import com.action.RegAction;
13         /**
14          * ActionFilter  负责请求的转发   将用户的请求交给相应的Action进行处理
15          * @author Administrator
16          *
17          */
18         public class ActionFilter implements Filter {
19
20             private FilterConfig config;
21
22             @Override
23             public void destroy() {
24                 // TODO Auto-generated method stub
25
26             }
27
28             @Override
29             public void doFilter(ServletRequest arg0, ServletResponse arg1,
30                     FilterChain chain) throws IOException, ServletException {
31                 //强转对象
32                 HttpServletRequest req = (HttpServletRequest)arg0;
33                 HttpServletResponse resp = (HttpServletResponse)arg1;
34                 //获取Action 去执行相应的功能
35                 Action action = getAction(req);
36                 //调用execute方法执行
37                 String result = null;
38                 try{
39                     result = action.execute(req, resp);
40                 }catch(Exception ex){
41                     ex.printStackTrace();
42                 }
43                 //正常得到处理后的返回值  进行页面跳转
44                 if(null!=result){
45                     req.getRequestDispatcher(result).forward(req, resp);
46                 }
47             }
48
49             @Override
50             public void init(FilterConfig conf) throws ServletException {
51                 // TODO Auto-generated method stub
52                 this.config = conf;
53             }
54
55             //得到请求地址  通过请求地址得到对应action   通过request得到请求信息
56             public Action getAction(HttpServletRequest request){
57                 //获取上URI
58                 String uri = request.getRequestURI();
59                 //获取请求的路径
60                 String contextPath = request.getContextPath();
61                 //截取获得action路径
62                 String actionPath = uri.substring(contextPath.length());
63                 //获取action名称
64                 String actionName = actionPath.substring(1,actionPath.lastIndexOf('.')).trim();
65                 //根据Action名称创建指定Action
66                 Action action = null;
67                 if("reg".equals(actionName)){
68                     action = new RegAction();
69                 }
70
71                 return action;
72             }
73
74
75         }

四、register.jsp页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3         <html>
 4           <head>
 5
 6             <title>注册页面</title>
 7           </head>
 8
 9           <body>
10             <form action="reg.action" method="post">
11                 username:<input name="uname" /><br/>
12                 password:<input name="upwd" type="password"/><br/>
13                 <input type="submit" value="注册"/>
14                 <input type="reset" value="重置"/>
15             </form>
16           </body>
17         </html>

五、配置web.xml

 1               <filter>
 2                 <filter-name>openSessionInView</filter-name>
 3                 <filter-class>com.web.OpenSessionInViewFilter</filter-class>
 4               </filter>
 5               <filter-mapping>
 6                 <filter-name>openSessionInView</filter-name>
 7                 <url-pattern>/*</url-pattern>
 8               </filter-mapping>
 9               <!-- 配置ActionFilter过滤器  -->
10               <filter>
11                 <filter-name>actionFilter</filter-name>
12                 <filter-class>com.mystruts.ActionFilter</filter-class>
13               </filter>
14               <filter-mapping>
15                 <filter-name>actionFilter</filter-name>
16                 <url-pattern>*.action</url-pattern>
17               </filter-mapping>

上面就完成了一个简单的自定义MVC框架  下一篇将继续深入讲解自定义MVC