(一)最简单的Struts2实例

前提环境:Linux(Ubuntu8)/Windows(XP)+JDK6/J2EE5(包括JDK6)+Ant1.7+Tomcat6.0+UltraEdit/EditPlus(java、javac、ant工具配置) (这些安装和配置在本人的blog中有详细介绍的文章)

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 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">
    
</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 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>

</struts>

 

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

<?xml version="1.0"?>
<project name="struts" basedir="." default="">

    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="."/>
    </path>

    <target name="compile" description="Compile all source code">
        <javac destdir="classes" debug="true"
            deprecation="false" optimize="false" failοnerrοr="true">
            <src path="src"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</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 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>




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

<%@ 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文件:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>成功</title>
    </head>
    <body>
        您已经登录!
    </body>
</html>

 
failure.jsp文件:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>失败</title>
    </head>
    <body>
        您登录失败!
    </body>
</html>

 
error.jsp文件:

<%@ 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文件:

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文件:

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 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="struts2t" extends="struts-default">
        <-- 处理Login.action的请求,实现类为org.jee.LoginAction -->
        <action name="Login" class="org.jee.LoginAction">
            <-- 定义处理结果和资源之间映射关系 -->
            <result name="ERROR">/error.jsp</result>
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</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、

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文件:

struts.locale=en_GB
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值