一個簡單的struts構架代碼

先建立下列文件:

一個ActionForm:LoginReForm

一個Action:LoginReAction

一個xml配置文件:struts-config.xml

三個jsp:register.jsp  success.jsp   failure.jsp

下面即是這些文件的相關代碼:

LoginReForm
  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */  
  5. package com.yourcompany.struts.form;   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import org.apache.struts.action.ActionErrors;   
  9. import org.apache.struts.action.ActionForm;   
  10. import org.apache.struts.action.ActionMapping;   
  11.   
  12. /**   
  13.  * MyEclipse Struts  
  14.  * Creation date: 09-11-2007  
  15.  *   
  16.  * XDoclet definition:  
  17.  * @struts.form name="logForm"  
  18.  */  
  19. public class LoginReForm extends ActionForm {   
  20.     /*  
  21.      * Generated Methods  
  22.      */  
  23.   
  24.     /**   
  25.      * Method validate  
  26.      * @param mapping  
  27.      * @param request  
  28.      * @return ActionErrors  
  29.      */  
  30.     private String username;   
  31.     private String password1;   
  32.     private String password2;   
  33.     public String getPassword1() {   
  34.         return password1;   
  35.     }   
  36.   
  37.     public void setPassword1(String password1) {   
  38.         this.password1 = password1;   
  39.     }   
  40.   
  41.     public String getPassword2() {   
  42.         return password2;   
  43.     }   
  44.   
  45.     public void setPassword2(String password2) {   
  46.         this.password2 = password2;   
  47.     }   
  48.   
  49.     public String getUsername() {   
  50.         return username;   
  51.     }   
  52.   
  53.     public void setUsername(String username) {   
  54.         this.username = username;   
  55.     }   
  56.   
  57.     public ActionErrors validate(ActionMapping mapping,   
  58.             HttpServletRequest request) {   
  59.         // TODO Auto-generated method stub   
  60.         return null;   
  61.     }   
  62.   
  63.     /**   
  64.      * Method reset  
  65.      * @param mapping  
  66.      * @param request  
  67.      */  
  68.     public void reset(ActionMapping mapping, HttpServletRequest request) {   
  69.         // TODO Auto-generated method stub   
  70.     }   
  71. }  

 

LoginReAction
  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */  
  5. package com.yourcompany.struts.action;   
  6. import org.apache.struts.action.*;   
  7. import javax.servlet.http.*;   
  8. import java.io.*;   
  9. import javax.servlet.http.HttpServletRequest;   
  10. import javax.servlet.http.HttpServletResponse;   
  11. import org.apache.struts.action.Action;   
  12. import org.apache.struts.action.ActionForm;   
  13. import org.apache.struts.action.ActionForward;   
  14. import org.apache.struts.action.ActionMapping;   
  15.   
  16. import com.yourcompany.struts.form.LoginReForm;   
  17.   
  18. /**   
  19.  * MyEclipse Struts  
  20.  * Creation date: 09-11-2007  
  21.  *   
  22.  * XDoclet definition:  
  23.  * @struts.action validate="true"  
  24.  */  
  25. public class LoginReAction extends Action {   
  26.     /*  
  27.      * Generated Methods  
  28.      */  
  29.   
  30.     /**   
  31.      * Method execute  
  32.      * @param mapping  
  33.      * @param form  
  34.      * @param request  
  35.      * @param response  
  36.      * @return ActionForward  
  37.      */  
  38.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  39.             HttpServletRequest request, HttpServletResponse response) {   
  40.         // TODO Auto-generated method stub   
  41.         LoginReForm rf = (LoginReForm) form;   
  42.   
  43.         String password1 = rf.getPassword1();   
  44.         String password2 = rf.getPassword2();   
  45.         ActionForward forward = new ActionForward();   
  46.         System.out.println("lisabush");   
  47.         if (password1.equals(password2)) {     
  48.         forward = mapping.findForward("suc");   
  49.         }    
  50.         else forward = mapping.findForward("fai");   
  51.         return (forward);   
  52.     }   
  53. }  

 

struts-config.xml
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">   
  3.   
  4. <struts-config>   
  5.   <data-sources />   
  6.   <form-beans >   
  7.     <form-bean name="LoginReForm" type="com.yourcompany.struts.form.LoginReForm" />   
  8.   
  9.   </form-beans>   
  10.   
  11.   <global-exceptions />   
  12.   <global-forwards />   
  13.   <action-mappings >   
  14.     <action   
  15.       attribute="loginRe"  
  16.       input="/register.jsp"  
  17.       name="LoginReForm"  
  18.       path="/loginRe"  
  19.       scope="request"  
  20.       type="com.yourcompany.struts.action.LoginReAction">   
  21.       <forward name="fai" path="/failure.jsp" />   
  22.       <forward name="suc" path="/success.jsp" />   
  23.     </action>   
  24.   
  25.   
  26.   </action-mappings>   
  27.   
  28.   <message-resources parameter="com.yourcompany.struts.ApplicationResources" />   
  29. </struts-config>   
  30.   

 

register.jsp 
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>My JSP 'index.jsp' starting page</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <!--   
  20.     <link rel="stylesheet" type="text/css" href="styles.css">   
  21.     -->   
  22.   
  23.   </head>   
  24.      
  25.   <body>   
  26. <form id="form2" name="input" method="post" action="loginRe.do">   
  27.   <label>用戶名:   
  28.   <input type="text" name="username" />   
  29.   </label>   
  30.   <label>密碼:   
  31.   <input type="text" name="password1" />   
  32.   </label>   
  33.   <label>密碼確認:   
  34.   <input type="text" name="password2" />   
  35.   </label>   
  36.   <input type="submit" name="" value="提交" />   
  37. </form>   
  38.   </body>   
  39. </html>   

 

success.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>My JSP 'success.jsp' starting page</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <!--   
  20.     <link rel="stylesheet" type="text/css" href="styles.css">   
  21.     -->   
  22.   
  23.   </head>   
  24.      
  25.   <body>   
  26.     success!!! <br>   
  27.   </body>   
  28. </html>   

 

failure.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>My JSP 'failure.jsp' starting page</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <!--   
  20.     <link rel="stylesheet" type="text/css" href="styles.css">   
  21.     -->   
  22.   
  23.   </head>   
  24.      
  25.   <body>   
  26.     failure!!! <br>   
  27.   </body>   
  28. </html>   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章