myeclipse6.5下开发struts2.0

自己考虑网上资料写的个例子,感觉struts2.0好方便。

 

http://struts.apache.org/download.cgi#Struts206
下载Full Distribution:
    * struts-2.0.11.1-all.zip (86mb) [PGP] [MD5]
并解压

 

问题:实现用户登录,并判断是否登录成功

解决方案:

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


第一:手动建立项目结构(类似于MyEclipse创建Web Pro项目的后台操作)


1、新建文件夹结构如下:
  Struts2t
  |______WEB-INF
               |_______classes
               |_______src
               |_______lib

2、复制Tomcat里conf文件夹里的web.xml到WEB-INF文件夹下,并修改web.xml文件
web.xml文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2.   
  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  7.     version="2.5">  
  8.        
  9. </web-app>  

 

3、将刚才下载解压后Struts2下的lib文件夹里
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar

拷贝到Struts2t下lib文件夹里。


4、找到Strust2里src/apps/showcase/src/main/resources(就是解压后里面的实例)的struts.xml文件复制到Struts2t下classes文件夹下,并修改struts.xml文件
struts.xml文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8. </struts>  

 

5、新建并手写一个build.xml(必须已经安装了Ant工具),并将build.xml放置到WEB-INF文件夹下 (MyEclipse内置了Ant)
build.xml文件:

Xml代码 复制代码
  1. <?xml version="1.0"?>  
  2. <project name="struts" basedir="." default="">  
  3.   
  4.     <path id="classpath">  
  5.         <fileset dir="lib">  
  6.             <include name="*.jar"/>  
  7.         </fileset>  
  8.         <pathelement path="."/>  
  9.     </path>  
  10.   
  11.     <target name="compile" description="Compile all source code">  
  12.         <javac destdir="classes" debug="true"  
  13.             deprecation="false" optimize="false" failonerror="true">  
  14.             <src path="src"/>  
  15.             <classpath refid="classpath"/>  
  16.         </javac>  
  17.     </target>  
  18.   
  19. </project>  

 

 

总结:目录结构如下
  Struts2t
  |______WEB-INF
               |_______classes
                
             |______struts.xml
               |_______src
               |_______lib
                               |_______ commons-logging-1.0.4.jar
                               |_______ freemarker-2.3.8.jar
                               |_______ ognl-2.6.11.jar
                               |_______ struts2-core-2.0.11.1.jar
                               |_______ xwork-2.0.4.jar              
               |_______web.xml
               |_______build.xml

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

第二:编写核心代码

1、Struts2核心就是控制器,为Struts2添加核心Filter配置在web.xml文件中(拦截所有Web请求并由FilterDispatcher初始化)
web.xml文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2.   
  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  7.     version="2.5">  
  8.        
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.Struts2.dispatcher.FilterDispatcher</filter-class>  
  12.     </filter>  
  13.        
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.        
  19. </web-app>  




2、编写表现层login.jsp、seccess.jsp、failure.jsp和error.jsp页面并放在与WEB-INF同一级目录下。
login.jsp文件:

Html代码 复制代码
  1. <%@ page language="java" contentType="text/html; charset=GBK" %>  
  2. <html>  
  3.     <head>  
  4.         <title>欢迎登录</title>  
  5.     </head>  
  6.     <body>  
  7.         <form action="Login.action" method="post">  
  8.             <table align="center">  
  9.                 <caption><h1>欢迎登录</h1></caption>  
  10.                 <tr>  
  11.                     <td>用户名:<input type="text" name="username" /></td>  
  12.                 </tr>  
  13.                 <tr>  
  14.                     <td>密码:<input type="password" name="password" /></td>  
  15.                 </tr>  
  16.                 <tr align="center">  
  17.                     <td colspan="2">  
  18.                         <input type="submit" value="登录" />  
  19.                         <input type="reset" value="重填" />  
  20.                     </td>  
  21.                 </tr>  
  22.             </table>  
  23.         </form>  
  24.     </body>  
  25. </html>  
<%@ page language="java" contentType="text/html; charset=GBK" %>
<html>
    <head>
        <title>欢迎登录</title>
    </head>
    <body>
        <form action="Login.action" method="post">
            <table align="center">
                <caption><h1>欢迎登录</h1></caption>
                <tr>
                    <td>用户名:<input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>密码:<input type="password" name="password" /></td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="登录" />
                        <input type="reset" value="重填" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>


success.jsp文件:

Html代码 复制代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <html>  
  3.     <head>  
  4.         <title>成功</title>  
  5.     </head>  
  6.     <body>  
  7.         您已经登录!   
  8.     </body>  
  9. </html>  
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>成功</title>
    </head>
    <body>
        您已经登录!
    </body>
</html>

 
failure.jsp文件:

Html代码 复制代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <html>  
  3.     <head>  
  4.         <title>失败</title>  
  5.     </head>  
  6.     <body>  
  7.         您登录失败!   
  8.     </body>  
  9. </html>  
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>失败</title>
    </head>
    <body>
        您登录失败!
    </body>
</html>

 
error.jsp文件:

Html代码 复制代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <html>  
  3.     <head>  
  4.         <title>异常</title>  
  5.     </head>  
  6.     <body>  
  7.         出现异常...   
  8.     </body>  
  9. </html>  
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>异常</title>
    </head>
    <body>
        出现异常...
    </body>
</html>

 

3、编写POJO(Action)在src下新建文件夹org,在org下新建文件夹jee(这里是建立包名),并新建接口MyAction.java和实现类LoginAction.java放置在src/org/jee文件夹下。

MyAction.java文件:

Java代码 复制代码
  1. package org.jee;   
  2.   
  3. interface MyAction{   
  4.        
  5.     public static final String SUCCESS = "success" ;   
  6.     public static final String FAILURE = "failure" ;   
  7.     public static final String NONE = "none" ;   
  8.     public static final String ERROR = "error" ;   
  9.     public static final String INPUT = "input" ;   
  10.     public static final String LOGIN = "login" ;   
  11.        
  12.     public String execute() ;   
  13. }  
package org.jee;

interface MyAction{
    
    public static final String SUCCESS = "success" ;
    public static final String FAILURE = "failure" ;
    public static final String NONE = "none" ;
    public static final String ERROR = "error" ;
    public static final String INPUT = "input" ;
    public static final String LOGIN = "login" ;
    
    public String execute() ;
}

 
LoginAction.java文件:

Java代码 复制代码
  1. package org.jee;   
  2.   
  3. public class LoginAction implements MyAction{   
  4.        
  5.     // 下面是Action内用于封装用户请求参数的连个属性   
  6.     private String username ;   
  7.     private String password ;   
  8.        
  9.     public String getUsername(){   
  10.         return username ;   
  11.     }   
  12.        
  13.     public void setUsername(String username){   
  14.         this.username = username ;   
  15.     }   
  16.        
  17.     public String getPassword(){   
  18.         return password ;   
  19.     }   
  20.        
  21.     public void setPassword(){   
  22.         this.password = password ;   
  23.     }   
  24.        
  25.     // 处理请求的execute方法   
  26.     public String execute(){   
  27.            
  28.         try{       
  29.             if (getUsername().equals("scott") && getPassword().equals("tiger")){   
  30.                 return "SUCCESS" ;   
  31.             }           
  32.             else{   
  33.                 return "FAILURE" ;   
  34.             }   
  35.         }   
  36.         catch(Exception e){   
  37.             e.printStackTrace() ;   
  38.             return "ERROR" ;   
  39.         }   
  40.            
  41.     }   
  42.        
  43. }  
package org.jee;

public class LoginAction implements MyAction{
    
    // 下面是Action内用于封装用户请求参数的连个属性
    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(){
        this.password = password ;
    }
    
    // 处理请求的execute方法
    public String execute(){
        
        try{    
            if (getUsername().equals("scott") && getPassword().equals("tiger")){
                return "SUCCESS" ;
            }        
            else{
                return "FAILURE" ;
            }
        }
        catch(Exception e){
            e.printStackTrace() ;
            return "ERROR" ;
        }
        
    }
    
}

 


4、在struts.xml里配置Action,修改classes文件
struts.xml文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <!-- Struts2的Action必须放在指定的包空间下定义-->  
  7.     <package name="struts2t" extends="struts-default">  
  8.         <-- 处理Login.action的请求,实现类为org.jee.LoginAction -->  
  9.         <action name="Login" class="org.jee.LoginAction">  
  10.             <-- 定义处理结果和资源之间映射关系 -->  
  11.             <result name="ERROR">/error.jsp</result>  
  12.             <result name="SUCCESS">/success.jsp</result>  
  13.         </action>  
  14.     </package>  
  15. </struts>  

 
5、将Struts2t整个文件夹拷贝到Tomcat/webapps文件夹下

总结:目录结构如下
  Struts2t
  |______WEB-INF
               |_______classes
                              |______org
                                           |_____jee
                                                      |______MyAction.java
                                                      |______LoginAction.java
                              |______struts.xml

               |_______src
                              |______org
                              |_____jee
                                         |______MyAction.java
                                         |______LoginAction.java
                |_______lib
                              |_______ commons-logging-1.0.4.jar
                              |_______ freemarker-2.3.8.jar
                              |_______ ognl-2.6.11.jar
                              |_______ struts2-core-2.0.11.1.jar
                              |_______ xwork-2.0.4.jar    
               |_______web.xml
               |_______build.xml
 |______login.jsp
 |______success.jsp
 |______failure.jsp
 |______error.jsp

 

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

三、测试

1、启动Tomcat6

2、http://localhost:8080/struts2 (进行测试)

3、

Java代码 复制代码
  1. 2008-5-27 15:48:23 org.apache.struts2.config.Settings getLocale   
  2. 警告: Settings: Could not parse struts.locale setting, substituting default VM locale  
2008-5-27 15:48:23 org.apache.struts2.config.Settings getLocale
警告: Settings: Could not parse struts.locale setting, substituting default VM locale


解决方法:创建struts.properties这个文件,放在src目录下就可以了

struts.properties文件:

Java代码 复制代码
  1. struts.locale=en_GB  
struts.locale=en_GB

 

 

 

 

以上代码有几个小错误。

 

struts.xml

 

 

 

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <!-- Struts2的Action必须放在指定的包空间下定义--> 
    <package name="strutst2" extends="struts-default"> 
     
        <action name="Login" class="org.jee.LoginAction"> 
           <result name="FAILURE">/failure.jsp</result> 
            <result name="ERROR">/error.jsp</result> 
            <result name="SUCCESS">/success.jsp</result> 
        </action> 
    </package> 
</struts> 

 

 

web.xml

 

<?xml version="1.0" encoding="ISO-8859-1"?> 
 
<web-app xmlns="http://java.sun.com/xml/ns/javaee
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    version="2.5"> 
      
    <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter> 
      
    <filter-mapping> 
        <filter-name>struts2</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
      
</web-app> 

 

 

LoginAction.java

package org.jee;

public class LoginAction implements MyAction{  
   
    // 下面是Action内用于封装用户请求参数的连个属性  
    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 ;  
    }  
      
    // 处理请求的execute方法  
    public String execute(){  
          
        try{      
            if (getUsername().equals("scott") && getPassword().equals("tiger")){  
                return "SUCCESS" ;  
            }          
            else{  
                return "FAILURE" ;  
            }  
        }  
        catch(Exception e){  
            e.printStackTrace() ;  
            return "ERROR" ;  
        }  
          
    }  
      

 

这些都是小错误,我已经改过来了。代码可以上CSDN上下载。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值