从实例理解Struts2

来源:互联网 发布:知乎 杨洋 油腻 编辑:程序博客网 时间:2024/04/29 21:03

       先是一个最最简单的例子,在浏览器中请求一个action,然后返回一个字符串到jsp页面上显示出来。


第一:创建web项目,引入struts2要的jar包,目录如下:

            


第二:web.xml中配置struts2的核心拦截器

<?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">    <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <filter>      <filter-name>struts2</filter-name>      <!--定义struts2的核心Filter的实现类 -->      <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>  


第三:MyEclipse项目中的src根目录下建立一个struts.xml文件

        可以打开struts2安装包里的apps目录下的任意一个jar包,在里面的WEB_INFR/src目录下,寻找struts.xml文件,将该文件复制进项目的src根目录下,将里面的内容清空(只留下<struts>标签和头部标签即可

<?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 2 的Action必须放在包空间下 -->       <package name="strutsqs" namespace="/zd" extends="struts-default">           <!-- 定义action的名字以及action的实现类 -->           <action name="index" class="com.zhudan.test.testaction" method="test" >               <!-- 定义action的处理结果result,result有两个属性,其中name指定返回名称,tyle指定返回的类型 -->                      <result name="suc">/index.jsp</result>           </action>        </package>   </struts>
 主要属性说明:

         package-name:用于区别不同的package;必须是唯一的、可用的变量名;用于其它package来继承;

         package--namespace:用于减少重复代码(和struts1比较);是调用action时输入路径的组成部分;

         package--extends:用于继承其它package以使用里面的过滤器等东东;

         action--name:用于在一个package里区别不同的action;必须是唯一的、可用的变量名;是调用action时输入路径的组成部分;

         action--class:action所在的路径(包名+类名);

         action--method:action所调用的方法名;
还有其它的属性,因为项目里没有用到,就没有解释。如有需要,请查阅相关文档。


第四:编写action类

package com.zhudan.test;import com.opensymphony.xwork2.ActionSupport;public class testaction extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn null;}//调用test方法,返回index.jsp页面public String test() throws Exception {// TODO Auto-generated method stubreturn "suc";}}


第五:简单的jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%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">  </head>    <body>    测试成功!<br>  </body></html>


第六:启动tomact,访问地址:http://localhost:8080/Struts2Test/zd/index.action

        Struts2Test:项目名字
        zd:命名空间,对应namespace里面的字符串。
        index.action:index对应action里面的字符串,“.action”表示请求的是一个action。


第七:运行机制

       1)客户端在浏览器中输入一个url地址;

       2)这个url请求通过http协议发送给tomcat;

       3)tomcat根据url找到对应项目里面的web.xml文件;

       4)在web.xml里面会发现有struts2的配置;

       5)然后会找到struts2对应的struts.xml配置文件;

       6)根据url解析struts.xml配置文件就会找到对应的class;

       7)调用完class返回一个字String,根据struts.xml返回到对应的jsp。


第八:strus2大概流程

       1)  客户端初始化一个指向Servlet容器(例如Tomcat)的请求。

       2)  这个请求经过一系列的过滤器(Filter)。

       3)  接着StrutsPrepareAndExecuteFilter被调用,StrutsPrepareAndExecuteFilter询问ActionMapper来决定这个请是否需要调用某个Action。

       4)  如果ActionMapper决定需要调用某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy。

       5)  ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类。

       6)  ActionProxy创建一个ActionInvocation的实例。

       7)  ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。

       8)  一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。


总结:

       Struts2的核心就是拦截器StrutsPrepareAndExecuteFilter。Struts.xml中所有的package都要extends="struts-default"。同理所有的Java类都要extends自Object一样。struts-default.xml里面就是要做以上事情。

   



0 0
原创粉丝点击