eclipse搭建struts1项目

 

    以前我是做C#开发的,来到现在的公司后转成了java,以前对java没有接触过,一上来就是bme平台,据同事说bme平台是基于struts、spring、ibatis的特性推出的自己定制化的java EE开发框架,由于bme本身的资料并不是很完善,尤其是对于特性本身的来龙去脉,以及为什么要有这样的特性,为什么这样用没有任何介绍,所以感觉理解起来好困难,于是就想着把struts、spring、ibatis都体验一遍。不知道这样的学习思路是否正确,但先试试再说吧。
    首先从struts1开始,上网找了不少资料来搭建struts1开发环境,但总是存在各种各样的问题,要么是信息不全,要么是俺不就搬后还是报异常,郁闷了两天,后来经过多次尝试,终于搭建成功,在这里分享一下(部分代码是引用别人的博客的):
 
步骤1:软件准备(基于xp操作系统的)
jdk 1.6 http://download.oracle.com/otn-pub/java/jdk/6u45-b06/jdk-6u45-windows-i586.exe
tomcat6 http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-6/v6.0.37/bin/apache-tomcat-6.0.37-windows-x86.zip
eclipse Java EE 版本http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR2/eclipse-jee-juno-SR2-win32.zip
eclipse tomcat 插件包tomcatPlugin  http://www.eclipsetotale.com/tomcatPlugin/tomcatPluginV33.zip
struts1 jar包 http://apache.fayea.com/apache-mirror//struts/binaries/struts-1.3.10-all.zip
步骤2:安装软件
关于jdk 、tomcat、eclipse、tomcat插件的安装请参照如下地址的第一章节到第二章节的第1点。
 
步骤3:打开eclipse,创建dynamic web project项目,此处我命名为logintest
步骤4:添加struts库
将struts-1.3.10-all.zip压缩包中的lib文件夹的内容全部拷贝到logintest/WebContent/WEB-INF/lib目录下
步骤5:修改或创建以下各个文件:
            logintest/WebContent/WEB-INF/web.xml
            logintest/WebContent/WEB-INF/struts-config.xml
            logintest/WebContent/index.html
            logintest/WebContent/logon.jsp
            logintest/WebContent/logonSuccess.html
            logintest/WebContent/logonFailure.html
            logintest/src/logon/LogonForm.java (注意:包名是logon)      
            logintest/src/logon/LogonAction.java (注意:包名是logon) 
            各个文件内容如下:
            
logintest/WebContent/WEB-INF/web.xml
<?  xml   version  = "1.0"   encoding =   "UTF-8"  ?>
<  web-app   xmlns:xsi  = "http://www.w3.org/2001/XMLSchema-instance" xmlns  = "http://java.sun.com/xml/ns/javaee"   xmlns:web  = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation  = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id =   "WebApp_ID"    version  = "2.5"   >
     <!-- Standard Action Servlet Configuration -->
 
     < 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-startup >   2 </   load-on-startup  >
     </ servlet   >
   
     < servlet-mapping   >
        <  servlet-name >   action  </ servlet-name   >
        <  url-pattern >   *.do  </ url-pattern   >
     </ servlet-mapping   >
   
   < welcome-file-list   >
     < welcome-file   >  index.html </   welcome-file  >
   </ welcome-file-list   >
 
</  web-app >
logintest/WebContent/WEB-INF/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 =  "logonForm"   type  = "logon.LogonForm"   />
    </ form-beans   >
 
    < action-mappings   >
      < action   path =  "/logon"   type  = "logon.LogonAction"
        name  = "logonForm"   validate =   "false"   scope   =  "request" >
        <  forward   name  = "LogonSuccess"   path =  "/logonSuccess.html"   />
        <  forward   name  = "LogonFailure"   path =  "/logonFailure.html"   />
      </ action   >
    </ action-mappings   >
   </ struts-config   >
logintest/WebContent/index.html
<!  DOCTYPE   html   PUBLIC   "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"   >
<  html   xmlns  = "http://www.w3.org/1999/xhtml"   >
<  head >
   < meta   name =  "generator"   content  = "HTML Tidy for Windows (vers 1st July 2003), see www.w3.org"   />
   < meta   http-equiv =   "Refresh"   content   =  "0;URL=./logon.jsp"   />
 
   < title   ></  title >
</  head >
 
<  body >
</  body >
</  html >
logintest/WebContent/logon.jsp
<%@   page   language =   "java"   contentType   =  "text/html; charset=ISO-8859-1"
     pageEncoding  = "ISO-8859-1"   %>
<!  DOCTYPE   html   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"   >
<  html >
<  head >
<  meta   http-equiv  = "Content-Type"   content =  "text/html; charset=ISO-8859-1"  >
<  title >   logon  </ title   >
</  head >
<  body >
         <  form   action  = "logon.do"   method =  "post" >
               <  table >
                     <  TR >
                           <  td >   UserName  </ td   >
                           <  td ><   input   name   =  "username"   type  = "text"      /></ td   >
                     </  TR >
                  
                     <  TR >
                           <  td >   PassWord  </ td   >
                           <  td ><   input   name   =  "password"   type  = "password"   ></  td >
                     </  TR >
                  
                     <  TR >
                           <  td ><   input   type   =  "submit"   value  = "LOGON"      /></ td   >
                  
                           <  td ><   input   type   =  "reset"   value  = "RESET" /></ td   >
                     </  TR >
            
               </  table >
         </  form >
</  body >
</  html >
logintest/WebContent/logonSuccess.html
<!  DOCTYPE   html   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"   >
<  html >
<  head >
<  meta   http-equiv  = "Content-Type"   content =  "text/html; charset=UTF-8"  >
<  title >   logon  </ title   >
</  head >
<  body >
<  h1 >   login success  </ h1   >
</  body >
</  html >
logintest/WebContent/logonFailure.html
<!  DOCTYPE   html   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"   >
<  html >
<  head >
<  meta   http-equiv  = "Content-Type"   content =  "text/html; charset=UTF-8"  >
<  title >   logon  </ title   >
</  head >
<  body >
<  h1 >   logon failure   </  h1 >
</  body >
</  html >
logintest/src/logon/LogonForm.java
package  logon;
 
import  org.apache.struts.action.ActionForm;
 
public   class   LogonForm  extends  ActionForm
{
 
 
         /**
       *
       */
         private   static   final   long   serialVersionUID  = 8645406869939700822L;
         private  String  username   ;
         private  String  password   ;
         private  String  logonstatus   ;
 
         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  String getLogonstatus()
      {
               return   logonstatus   ;
      }
 
         public   void   setLogonstatus(String logonstatus)
      {
               this  . logonstatus   = logonstatus;
      }
}
logintest/src/logon/LogonAction.java
package  logon;
 
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;
 
public   class   LogonAction  extends  DispatchAction
{
 
         @Override
         public  ActionForward execute(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse  response)
                     throws  Exception
      {
 
            LogonForm logonform = (LogonForm) form;
               if  (logonform.getUsername().trim().length() > 0
                        && logonform.getPassword().trim().length() > 0)
            {
                     return  mapping.findForward( "LogonSuccess"   );
            }
               return  mapping.findForward( "LogonFailure"   );
 
      }
 
}
 
步骤6:创建一个server
如何创建一个server请参照:http://hi.baidu.com/jialy1987/item/8cd7d4fe9e6b745ec9f337a9
 
步骤7:将logintest工程添加到server中然后启动tomcat
 
步骤8:在浏览器中输入:http://localhost:8080/logintest
 
如果能成功看到用户名密码输入界面,说明一切都是OK的。

 

你可能感兴趣的:(eclipse,struts,struts1)