分享

Eclipse 手动添加 struts1.3

 KhanLau 2015-09-19

Eclipse 手动添加 struts1.3(未用到validator验证)

 

1.下载struts1.3 jar包,放到lib目录下,导入到项目中
 下载地址:http://struts./
2.修改 web.xml文件 添加如下的代码

Xml代码  收藏代码
  1. <servlet>  
  2.  <servlet-name>action</servlet-name>  
  3.  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  4.  <init-param>  
  5.   <param-name>config</param-name>  
  6.   <param-value>/WEB-INF/struts-config.xml</param-value>  
  7.  </init-param>  
  8. </servlet>  
  9.   
  10. <servlet-mapping>  
  11.  <servlet-name>action</servlet-name>  
  12.  <url-pattern>*.do</url-pattern>  
  13. </servlet-mapping>  

 
3.创建struts-config.xml文件  目录 WEB-INF
 以下是个struts-config.xml文件的例子
 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.  <!DOCTYPE struts-config PUBLIC  
  4.      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  
  5.      "http://struts./dtds/struts-config_1_3.dtd">  
  6.  <struts-config>  
  7.    <form-beans>  
  8.      <form-bean name="logonForm" type="logon.LogonForm" />  
  9.    </form-beans>  
  10.   
  11.    <action-mappings>  
  12.      <action path="/logon" type="logon.LogonAction"  
  13.        name="logonForm" validate="false" scope="request">   
  14.        <forward name="LogonSuccess" path="/logonSuccess.html" />  
  15.        <forward name="LogonFailure" path="/logonFailure.html" />  
  16.      </action>  
  17.    </action-mappings>  
  18.  </struts-config>  

 


4.创建Form,继承 ActionFrom
5.创建Action,继承 DispatchAction
6.创建相关页面 如 logon.jsp logonSuccess.html  logonFailure.html

 

附:Form Action logon.jsp 代码

1).LogonForm

Java代码  收藏代码
  1. package logon;  
  2.   
  3. import org.apache.struts.action.ActionForm;  
  4.   
  5. public class LogonForm extends ActionForm {  
  6.       
  7.     private String username;  
  8.     private String password;  
  9.     private String logonstatus;  
  10.       
  11.       
  12.     public String getUsername() {  
  13.         return username;  
  14.     }  
  15.     public void setUsername(String username) {  
  16.         this.username = username;  
  17.     }  
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.     public void setPassword(String password) {  
  22.         this.password = password;  
  23.     }  
  24.     public String getLogonstatus() {  
  25.         return logonstatus;  
  26.     }  
  27.     public void setLogonstatus(String logonstatus) {  
  28.         this.logonstatus = logonstatus;  
  29.     }  
  30.       
  31.       
  32.       
  33.       
  34. }  

 

 

2).LogonAction 代码

 

Java代码  收藏代码
  1. package logon;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6.   
  7. import org.apache.struts.action.ActionForm;  
  8. import org.apache.struts.action.ActionForward;  
  9. import org.apache.struts.action.ActionMapping;  
  10. import org.apache.struts.actions.DispatchAction;  
  11.   
  12. public class LogonAction extends DispatchAction {  
  13.   
  14.       
  15.     @Override  
  16.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  17.             HttpServletRequest request, HttpServletResponse response)  
  18.             throws Exception {  
  19.   
  20.         LogonForm logonform =(LogonForm)form;  
  21.         if(logonform.getUsername().trim().length()>0 && logonform.getPassword().trim().length()>0){  
  22.             return mapping.findForward("LogonSuccess");   
  23.         }  
  24.         return mapping.findForward("LogonFailure");  
  25.           
  26.     }  
  27.       
  28. }  

 

 

3).logon.jsp代码

Html代码  收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page  
  2.     language="java" contentType="text/html; charset=GB18030"  
  3.     pageEncoding="GB18030"%>  
  4. <html>  
  5. <head>  
  6. <title>logon</title>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  8. <meta name="GENERATOR"  
  9.     content="Rational? Application Developer? for WebSphere? Software">  
  10. </head>  
  11. <body>  
  12.     <form action="logon.do" method="post">  
  13.         <table>  
  14.             <TR>  
  15.                 <td>UserName</td>  
  16.                 <td><input name="username" type="text" /></td>  
  17.             </TR>  
  18.               
  19.             <TR>  
  20.                 <td>PassWord</td>  
  21.                 <td><input name="password" type="password" ></td>  
  22.             </TR>  
  23.               
  24.             <TR>  
  25.                 <td><input type="submit" value="LOGON" /></td>  
  26.               
  27.                 <td><input type="reset" value="RESET" /></td>  
  28.             </TR>  
  29.           
  30.         </table>  
  31.     </form>  
  32. </body>  
  33. </html>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多