当前位置:编程学习 > JS >>

Struts中静态动态ActionForm的使用(一)

目录
 
 
--------------------------------------------------------------------------------
 
登录例子
 
区别
 
时间属性
 
验证机制
 
 
--------------------------------------------------------------------------------
    
    在Struts1中,我们经常接触ActionForm为静态ActionForm。静态是指开发者在使用struts框架时必须写对应的ActionForm。
 
    在介绍动态ActionForm使用之前,我们回忆struts常用的ActionForm的方式,以便做对比。
 
    举个完整的登录例子。
 
    第一:配置web.xml
 
[html] 
<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>  
</servlet>  
<servlet-mapping>  
  <servlet-name>action</servlet-name>  
  <url-pattern>*.do</url-pattern>  
</servlet-mapping>  
 
<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>
</servlet>
<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>    第二:新建jsp页面(在login文件夹)
 
[html] view plaincopyprint?
<form action="../login.do" method="get">  
  用户名: <input name="username" type="text"><br>  
  密     码: <input name="password" type="text"><br>  
<input type="submit" >   
</form>  
 
  <form action="../login.do" method="get">
    用户名: <input name="username" type="text"><br>
    密     码: <input name="password" type="text"><br>
  <input type="submit" > 
  </form>
    第三:新建ActionForm
 
[html] 
package com.login;  
  
import java.util.Date;  
  
import javax.servlet.http.HttpServletRequest;  
  
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 LoginForm  extends ActionForm{  
           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;  
    }  
  private String username;  
  private String password;  
}  
 
package com.login;
 
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
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 LoginForm  extends ActionForm{
           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;
}
  private String username;
  private String password;
}    第四:新建Action
 
[html] 
package com.login;  
  
import java.util.Date;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import org.apache.struts.action.Action;  
import org.apache.struts.action.ActionForm;  
import org.apache.struts.action.ActionForward;  
import org.apache.struts.action.ActionMapping;  
import org.apache.struts.action.DynaActionForm;  
  
public class LoginAction extends Action {  
      
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
          
         LoginForm loginForm=(LoginForm)form;  
          
         boolean flag=false;  
         if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){  
             flag=true;  
         }  
    
         request.setAttribute("username",loginForm.getUsername());  
         if(flag)  
         {  
             return mapping.findForward("success");   
         }else{  
             return mapping.findForward("false");  
         }  
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,