Struts2_01_开发过程与实例说明

一、开发Struts2应用的主要工作

  1.Model层开发

  2.在web.xml中配置FilterDispatcher

  3.开发Action类

  4.拦截器(Interceptor)的配置或自定义开发

  5.开发视图

二、实例说明

简单步骤:

  •   下载并导入所需jar包;
  •   开发Model层业务逻辑;
  •   开发视图文件;
  •   开发Action类;
  •   在struts.xml中配置Action类;
  •   在jsp中对应Action;
  •   在web.xml中配置FilterDispatcher;

1.到官网下载并导入Struts2框架的主要核心jar包

   struts2必须导入的常用的五个jar包:

    * xwork-2.0.4.jar ;
    * struts2-core-2.0.11.1.jar ;
    * freemarker-2.3.8.jar;
    * ognl-2.6.11.jar;
    * commons-logging-api-1.1.jar;

这几个文件是使用struts2时必须导入的,这是我使用时的版本,可能现在版本有所更新。

 

2.开发Model层业务逻辑

public class LoginService {  
     public boolean login(String cutname,String pwd){  
         if(cutname.equals("zhangsan")&&pwd.equals("123")){  
             return true;  
         }else{  
             return false;  
         }  
     }  
} 

 

3.开发视图文件

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    <html>  
      <head>      
        <title>index starting page</title>  
      </head>  
        
      <body>  
     <!-- action 还没有指定 --> <s:form action=" "> <s:textfield name="custname" label="用户名"></s:textfield><br> <s:password name="pwd" label="密 码"></s:password><br> <s:submit value="Login"></s:submit> </s:form> </body> </html>

欢迎页面将显示登录用户的用户名,使用EL显示请求参数的值。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
        <title>welcome page</title>  
  </head>  
  <body>  
    Welcome,${param.custname }  
  </body>  
</html> 

 

4.开发Action类,调用业务逻辑,返回结果视图

package com.struts.action;  
import com.struts.service.LoginService;  
  
public class LoginAction {  
    private String custname;  
    private String pwd;  
    public String getCustname() {  
        return custname;  
    }  
    public void setCustname(String custname) {  
        this.custname = custname;  
    }  
    public String getPwd() {  
        return pwd;  
    }  
    public void setPwd(String pwd) {  
        this.pwd = pwd;  
    }  
      
    public String execute(){  
        LoginService ls = new LoginService();  
        boolean flag = ls.login(custname, pwd);  
        if(flag){  
            return "success";  
        }else{  
            return "fail";  
        }  
    }  
} 

 

5.在struts.xml中配置Action类

struts2的主配置文件使用struts.xml,它必须放在classes目录下,在MyEclipse中的就是src下。配置如下:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC   
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"   
    "http://struts.apache.org/dtds/struts-2.1.dtd">  
  
<struts>  
<package name="user"   extends="struts-default">  
    <action name="Login" class="com.struts.action.LoginAction" >  
        <result name="success" >/welcome.jsp</result>  
        <result name="fail">/login.jsp</result>  
    </action>  
</package>  
</struts>  

 

6.在jsp中对应Action

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    <html>  
      <head>      
        <title>index starting page</title>  
      </head>  
        
      <body>  
     <!-- action 指向LoginAction -->
        <s:form action="Login">  
            <s:textfield name="custname" label="用户名"></s:textfield><br>  
            <s:password name="pwd" label="密 码"></s:password><br>  
            <s:submit value="Login"></s:submit>  
        </s:form>  
      </body>  
    </html> 

 

7.在web.xml中配置FilterDispatcher

struts2通过过滤器来替代struts1.x中的actionServlet配置。在web.xml中的配置如下:

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.5"   
        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">  
      
      <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>  
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
        </filter-class>  
      </filter>  
      
      <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
      </filter-mapping>  
    </web-app>  

 

三、Demo大致运行过程如下:

  1.访问:http:localhost:8080/struts2/index.jsp;

容器执行index.jsp后,输出到客户端的仍然是解析后的HTML代码,其中表单的action值被解析为:action="/struts2/Login.action"。

  2.用户输入用户名和密码,点击登陆按钮后,则向服务器发送请求,请求URL根据表单的action生成:http://localhost:8080/struts/Login.action;

  3.web.xml中对/*的URL都配置了过滤器FilterDispatcher,所以该请求将被FilterDispatcher过滤;

  4.FilterDispatcher调用ActionMapper,ActionMapper判断URL的资源后缀。

Login.action的后缀是.action,因此ActionMapper认为需要调用Struts2框架的Action类;

  5.FilterDispatcher将请求处理交给ActionProxy,ActionProxy认为需要调用的Action的名字是URL中去掉.action后缀的字符串,即Login;

  6.ActionProxy通过Configuration查找struts.xml,找到name=Login的Action配置;

  7.ActionProxy实例化ActionInvocation;

  8.ActionInvocation实例调用与Action有关的拦截器,以及Action类的execute方法。

  9.Action执行结束后,根据struts.xml中配置的action的result,将导航到指定的URL。

  

 

 

 

posted @ 2012-08-25 12:19  終囿┅兲  阅读(340)  评论(0编辑  收藏  举报