• struts_login实例


    struts_login实例利用struts实现登录功能,在myeclipse中新建项目,项目名struts_login

    第一步:向WEB-INF/lib,文件夹下加入struts1.x包

    第二步:配置web.xml

    可以在struts1.x中找到实例项目的配置,然后复制即可。

      <servlet>
         <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startuup>2</load-on-startuup>
      </servlet>
     
      <servlet-mapping>
          <servlet-name>action</servlet-name>
          <url-pattern>/login</url-pattern>
      </servlet-mapping>

    第三步:在/WEB-INF/,目录下新建struts-config.xml的xml文件,创建的时候使用struts-config.xml模板新建,暂时不填写struts-config.xml的配置信息

    第四步:分析利用struts实现登录的流程,并实现代码

    1,用户在登录页面填写了登录信息,然后提交登录请求

    <body>
       <h1>登录信息</h1>
       <form action="login" method="post">
                用户名:<input type="text" name="username"/><br/>
                 密码:<input type="text" name="password"/><br/>
       <input type="submit" value="提交"/>
       </form>
      </body>

    2,请求到达前段控制器ActionServlet,然后ActionServlet把请求的url与解析的配置信息对比,得到相对应的Action。(在此需要说明的是,struts-config.xml的配置信息在ActionServlet初始化的时候就已经解析了。)

    所以在struts-config.xml中加入action应该接收的url地址,其中action的path必须以"/"开头,故设置path="/login",同时要通过path得到相应处理的Action处理类,在此新建login处理类LoginAction,该类继承Action,并放置在com.lxh.struts包下,所以在src下添加包,并在包下添加类,并设置action的type值,type="com.lxh.struts.LoginAction"


    <struts-config>
     <form-beans>
      <form-bean name="" type=""/>
     </form-beans>

     <action-mappings>
      <action path="/login"
        type="com.lxh.struts.LoginAction"
        name=""
        parameter="methode"
        scope="request"
        validate="true"
        >
       <forward name="success" path="" />
       <forward name="failure" path="" />
      </action>
     </action-mappings>

    </struts-config>

    3,在Action的处理类LoginAction中添加处理方法

    选中LoginAction字符串,右键source-->override/implement method -->execute方法中包含httpServletRequest参数的方法

    4,在LoginAction中调用模型对象来处理封装在ActionForm对象中的数据。

    首先获取封装在ActionForm对象中的数据,而ActionForm是自动封装了表单中的数据,所以添加一个继承了ActionForm的类,该类中只有表单字段,并且含有获取与设置字段的方法

    package com.lxh.struts;

    import org.apache.struts.action.ActionForm;

    public class LoginActionForm extends ActionForm {

     private String username;
     private String password;
     public String getUsername() {
      return username;
     }
     public void setUsername(String username) {
      this.username = username;
     }
     public String getPassword() {
      return password;
     }
     public void setPassword(String password) {
      this.password = password;
     }
     

       public void reset(ActionMapping mapping, HttpServletRequest request) {
         System.out.println("----ActionForm开始收集数据时就执行该方法----"); 
     }
     public ActionErrors validate(ActionMapping mapping,
       HttpServletRequest request) {

      System.out.println("----在服务器上进行表单验证----");
      return super.validate(mapping, request);
     }


    }

    而ActionForm自动获取表单数据的功能是通过struts-config.xml的配置实现的,所以在struts-config.xml中要加入ActionForm的配置信息,form-beans,然后form-beans的name要在action的name中注册。

    <struts-config>
     <form-beans>
      <form-bean name="LoginActionForm" type="com.lxh.struts.LoginActionForm"/>
     </form-beans>

     <action-mappings>
      <action path="/login"
        type="com.lxh.struts.LoginAction"
        name="LoginActionForm"
        parameter="methode"
        scope="request"
        validate="true"
        >
       <forward name="success" path="" />
       <forward name="failure" path="" />
      </action>
     </action-mappings>

    </struts-config>

    然后在LoginAction中获取表单数据

    LoginActionForm laf=(LoginActionForm)form;
      String username=laf.getUsername();
      String password=laf.getPassword();

    之后调用模型处理表单数据,再次模型应该为处理username和password数据的方法,那么新建一个模型类,该模型类在包com.lxh.model下

    package com.lxh.model;

    public class CheckLogin {
     
     public boolean check(String username,String password)
     {
           if(username.equals("jin")&&password.equals("123"))
           {
            return true;
           }else
           {
            return false;
           }
     }

    }

    调用模型处理数据,并返回处理结果

    CheckLogin cl=new CheckLogin();
    boolean flag=cl.check(username, password);

    根据处理结果返回跳转的页面,跳转的页面是通过ActionMapping对象的findForward()方法得到

    if(flag)
      {
       return mapping.findForward("success");
      }else
      {
       return mapping.findForward("failure");
      } 

    因为ActionMapping对象封装了struts-config.xml文件中的信息,所以在struts-config.xml中存在页面跳转信息forward。假设成功则进入LoginSucess.jsp页面,失败则进入Loginerr.jsp页面,那么添加LoginSucess.jsp页面和添加Loginerr.jsp页面。

    <struts-config>
     <form-beans>
      <form-bean name="LoginActionForm" type="com.lxh.struts.LoginActionForm"/>
     </form-beans>

     <action-mappings>
      <action path="/login"
        type="com.lxh.struts.LoginAction"
        name="LoginActionForm"
        parameter="methode"
        scope="request"
        validate="true"
        >
       <forward name="success" path="/LoginSucess.jsp" />
       <forward name="failure" path="/Loginerr.jsp" />
      </action>
     </action-mappings>

    </struts-config>

  • 相关阅读:
    046.Kubernetes集群管理-日常运维
    045.Kubernetes集群存储-CSI存储机制
    044.Kubernetes集群存储-StorageClass
    043.Kubernetes集群存储-共享存储
    CKAD考试心得分享
    050.Kubernetes集群管理-Prometheus+Grafana监控方案
    附015.Kubernetes其他技巧
    041.Kubernetes集群网络-K8S网络策略
    042.Kubernetes集群网络-flannel及calico
    040.Kubernetes集群网络-CNI网络模型
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1952830.html
Copyright © 2020-2023  润新知