zoukankan      html  css  js  c++  java
  • Struts2(一)入门小案例

    Struts01---入门小案例

     

    创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码!

    创建两个类实现共同的接口!

    public interface Action { 
    
        String  execute();
    }
    复制代码
    public class LoginAction  implements Action{
    
        public  String  execute(){
            System.out.println("LoginAction......");
            return  "success";
        }
    }
    复制代码
    复制代码
    public class ListAction implements Action {
    
        public  String  execute(){
            System.out.println("ListAction......");
            return  "success";
        }
    }
    复制代码

    想让用户能访问到我们的后台代码,要么使用servlet  要么使用filter!

    使用filter

    创建一个filter用来拦截用户的请求

    复制代码
    public class DoFilter implements Filter {
        //全局的变量
         Map<String,String> map=new  HashMap<String, String>();
        
         //初始化操作
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            System.out.println("DoFilter 初始化了.............................");
         //  key是用户请求的路径  value 是对应的全类名
            map.put("/login","cn.bdqn.action.LoginAction");
            map.put("/list","cn.bdqn.action.ListAction");
        }
    
    
        //真正的处理
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
             //向下转型
            HttpServletRequest httpServletRequest=(HttpServletRequest) request;
            HttpServletResponse httpServletResponse=(HttpServletResponse) response;
            //看一下 各个路径的区别
            System.out.println("getContextPath()==>"+httpServletRequest.getContextPath());//项目名
            System.out.println("getServletPath()==>"+httpServletRequest.getServletPath());//访问的路径
            System.out.println("getRequestURI()==>"+httpServletRequest.getRequestURI());//项目下面的路径
            System.out.println("getRequestURL()==>"+httpServletRequest.getRequestURL());//带协议的完整路径
           //应该使用getServletPath
            String  path=httpServletRequest.getServletPath();
            try {
                if (path.equals("/index.jsp")) {
                    chain.doFilter(request, response);  //放行
                }else{
                    Action action=(Action) Class.forName(map.get(path)).newInstance();
                    action.execute();
                    //跳转到成功界面
                    httpServletRequest.getRequestDispatcher("/success.jsp").forward(request, response);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            
            
        }
        @Override
        public void destroy() {
    
        }
    
    
    }
    复制代码

    前台页面

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <a  href="login">登录 </a>
        <a  href="list">详情 </a>
      </body>
    </html>
    复制代码

    sucess.jsp页面就是一个成功界面!!!省略掉!

     

    我们使用xml文件来代替  map中 所保存的 键值对  信息!

    key:用户的请求

    value:对应的后台实现类 全类名!

  • 相关阅读:
    get started with laravel
    redis消息队列
    javascript模板引擎Mustache
    YIi 权限管理和基于角色的访问控制
    Yii CDbCriteria
    C++ 推断进程是否存在
    IE浏览器开启对JavaScript脚本的支持
    最小公约数(欧几里得算法&amp;&amp;stein算法)
    Nyoj 43 24 Point game 【DFS】
    【蓝桥杯】PREV-5 错误票据
  • 原文地址:https://www.cnblogs.com/xiaobaizhang/p/8915924.html
Copyright © 2011-2022 走看看