zoukankan      html  css  js  c++  java
  • 第一个Struts1步骤

    一.前端 :登陆页面

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%> 

    <%
    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%>">
       
       
      </head>
      <body>
      <html:errors/>

     <form action="loginAction.do" method="post"> 
        请您登陆,输入用户名和密码 <br>
        用户名:<input type="text" name="username"><html:errors property="user.username.null"/><br/>
        密码 :<input type="text" name="password"><html:errors property="user.password.null"/><br/>
        <input type="submit" value="提交">&nbsp;<input type="reset" value="重置"><br>
        </form>
      </body>
    </html>

    后台步骤:

    一。Action类
    1. extends DispatchAction;

    2.重写ActionForward execute方法

     Users user=(Users)form;//强制类型转换
     return mapping.findForward("SUCCESS");

    action类如下:

    package com.action;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;

    import com.entity.Users;
    public class LoginAction extends DispatchAction{
     public ActionForward execute(ActionMapping mapping,ActionForm form,
       HttpServletRequest request,HttpServletResponse response)
     throws Exception{
      Users user=(Users)form;
      return mapping.findForward("SUCCESS");
     }
    }


     
    二。实体类extends ActionForm如下:

    package com.entity;

    import org.apache.struts.action.ActionForm;

    public class Users 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;
     }
     
    }


    三。配置struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

    <struts-config>
      <form-beans>
      <form-bean name="users" type="com.entity.Users">
      </form-bean>
      </form-beans>
     
      <global-exceptions/>
      <global-forwards />


       <action-mappings>
       <action path="/loginAction" type="com.action.LoginAction" name="users">
       <forward name="SUCCESS" path="/success.jsp"></forward>
       </action>
       </action-mappings>


    </struts-config>

    ---------------------------------------------------------------------------------------------------------------------------------------

    验证的两种方式

    方法一。实体类继承ActionForm并重写验证方法

    1.实体Users类如下:

    package com.entity;

    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;

    public class Users 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 ActionErrors validate(ActionMapping mapping,
                javax.servlet.http.HttpServletRequest request){
      ActionErrors error=new ActionErrors();//继承自ActionMessages类,信息集合
      if(this.getUsername()==null||this.getUsername().trim().equals("")){
       error.add("user.username.null", new ActionMessage("user.username.null"));
      }
      if(this.getPassword()==null||this.getPassword().trim().equals("")){
       error.add("user.password.null", new ActionMessage("user.password.null"));
      }
      
      return  error;
      
     }
     
    }

    2.LoginAction类

    public class LoginAction extends DispatchAction{
     public ActionForward execute(ActionMapping mapping,ActionForm form,
       HttpServletRequest request,HttpServletResponse response)
     throws Exception{
      
      return mapping.findForward("SUCCESS");
     }
    }

    方法二.Action类中写验证

    LoginAction类代码如下:

    public class LoginAction extends DispatchAction{
     public ActionForward execute(ActionMapping mapping,ActionForm form,
       HttpServletRequest request,HttpServletResponse response)
     throws Exception{
      Users user=(Users)form;
      ActionMessages messages=new ActionMessages();//信息集合
      if(user.getUsername()==null||user.getUsername().trim().equals("")){
       messages.add("user.username.null", new ActionMessage("user.username.null"));//国际化
       //this.addErrors(request, messages);
       //return mapping.findForward("INPUT");
      }
      if(user.getPassword()==null||user.getPassword().trim().equals("")){
       messages.add("user.password.null", new ActionMessage("user.password.null"));
       //this.addErrors(request, messages);
       //return mapping.findForward("INPUT");
      }
      if(messages.size()>0){
       this.addErrors(request, messages);
       return mapping.getInputForward();
       //return mapping.findForward("INPUT");
       
      }
      return mapping.findForward("SUCCESS");
     }
    }

    3.struts-config.xml中<action>内容修改如下:

     <action input="/index.jsp" path="/loginAction" type="com.action.LoginAction" name="users">
       <forward name="SUCCESS" path="/success.jsp"></forward>
       </action>

  • 相关阅读:
    apktool 在mac下的使用 -反编译安卓apk文件
    通过Stetho在Chrome上调试Android App
    Android Studio 遇到 No Debuggable Applications 的解决方案
    安装Eclipse Maven插件的方法
    Android如何实现点击一次返回键返回桌面而不是退出应用
    安卓7.0遇到 android.os.FileUriExposedException: file:///storage/emulated.. exposed beyond app through Intent.getData()
    由Memcached使用不当而引发性能问题的两个经验总结
    对MySql查询缓存及SQL Server过程缓存的理解及总结
    由Java中toString()方法引发的无意识的递归想到的
    为什么不能把委托(delegate)放在一个接口(interface)当中?
  • 原文地址:https://www.cnblogs.com/ximencuixue/p/3252405.html
Copyright © 2011-2022 走看看