struts2

基于WebWork封装成的框架

特点:

 

1   只有Action组件,没有ActionForm组件。Action组件承担属性信息,并且处理方法。   

2   Action非常灵活,可以使用一个普通的CLASS来充当

3   Action可测性强,Action的execute方法返回String  参数为无参

4   提供了result组件(json  stream   xmlHttpRequest  ...),简化响应处理的实现

5   提供了大量拦截器组件

 

主要构成,看看STRUTS2的MVC实现是怎样的:

 

控制器:  FilterDispatcher,  StrutsPrepareAndExecterFilter

视图: 各种Result   支持视图组件响应  具体见图一中的类型

   struts2标签   提供了分支,循环,显示等功能

模型:  普通的POJO(Action)  更加灵活

    ValueStack组件   用于封装请求相关信息

使用总结:

1  创建web project,引入struts2开发基础包

2  在web.xml中配置控制器,添加控制器的配置文件struts.xml

3  编写jsp,Action组件

4  编写struts.xml配置,定义Action组件和请求的对应

 

struts的配置文件:

struts-default.xml(框架自带,定了一些Result组件)

struts.xml(开发者创建并使用,定义开发者编写的Result组件,拦截器组件,Action组件)

default.properties(框架自带,定义了框架的一些系统参数)

struts.properties(开发者创建并使用,用于覆盖default.properties中的配置。)

struts-plugin.xml(用于整合插件的配置)

 

struts2中的Action相关配置

1<package>元素的namespace属性    用于指定package中action对应的请求路径。

2<action>中的method属性   用于指定action中的业务方法名,默认为execute。

3<param>元素   用于为属性指定值,它包在action里面就是为action指定值,在result里面,就是为result指定值。

4通配符的使用   

<action name="opt_*" method='{1}' class="he.action.OptAction">
<result name='success' type='dispatcher'>/ok.jsp</result>
<result name='fail' type='dispatcher'>/fail.jsp</result>
</action>

5   动态方法调用

<a href='opt!update'>update</a><br>
<a href='opt!delete'>delete</a><br>
<a href='opt!add'>add</a><br>

 

常使用到的API

ValueStack   ActionContext   ServletActionContext

ValueStack主要存储请求信息,内部结构分为root栈(Action压到栈底)和Context区域(Map结构,也叫变量存储区域key和value对应)

ActionContext提供了一些方法,ActionContext.getContext().get("application");以此获取context区域的信息。返回Map<key,value>

HttpServletRequest  request = ServletActionContext.getRequest();返回原始对象。

 

struts.xml文件实例,注意里面的一些技巧:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package namespace="/" name="struts2-demo1" extends="struts-default">
<!-- 跳转到login.jsp页面 -->
<action name="toLogin">
<result >/WEB-INF/index.jsp</result>
</action>
<!-- 跳转到opt.jsp页面 -->
<action name="toOpt">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 登录示例 -->
<action name="login" class="tarena.action.Login" method="check">
<!-- 为Login对象中的dir属性赋值 -->
<param name="dir">D:\\images</param>
<!-- <result>/ok.jsp</result> -->
<result name="success" type="dispatcher">
<param name="location">/WEB-INF/ok.jsp</param>
</result>
<result name="login">/WEB-INF/index.jsp</result>
</action>
<!-- 通配符使用示例 -->
<action name="opt_*" method="{1}" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 动态方法调用示例 -->
<action name="opt" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>


</package>
</struts>

 

下面是一个简单的例子:

package he.action;

public class Login {
    String name;
    String pwd;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String execute(){
        System.out.print(name+"  \n"+pwd);
        if("scott".equals(name)&&"1234".equals(pwd)){
            return "success";
        }
        return "fail";
    }

}
Login.java
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <package name="struts2-demo1" extends="struts-default">
        <action name="login" class="he.action.Login">
            <result name='success' type='dispatcher'>/ok.jsp</result>
            <result name='fail' type='dispatcher'>/fail.jsp</result>
        </action>
    </package>
</struts>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
  <filter>
          <filter-name>Struts2Controller</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
          <filter-name>Struts2Controller</filter-name>
          <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form action='login' method='post'>
    username:<input type="text" name='name' id='name' /><br>
    password:<input type='password' name='pwd' id='pwd'/><br/>
    <input type='submit' value='OK' />
    
    </form>
  </body>
</html>
index.jsp

 

 

 

 

posted on 2015-12-24 22:55  编世界  阅读(413)  评论(0编辑  收藏  举报