如何创建struts2第一个程序及其原理解释




任何技术的学习都是需要大量的精力的,也许我们前面学习java基础和jsp+servlet花费了大量时间,但是请相信这些都是值得的,当我们学习一样东西到达一定境界时,我们就可以学习那些框架技术了,不得不说这个世界上有许多牛逼的大神,也多亏了他们我们做开发不用从底层做起,这就是技术的魅力,我们需要其实现某一件功能时,往往不关注其底层的实现,我们要的时间,所以框架诞生了..首先感谢那些大神

好了,废话不多说.

想要学习struts2就先要了解其原理



上图来源于Struts2官方站点,是Struts 2 的整体结构。 
一个请求在Struts2框架中的处理大概分为以下几个步骤 
1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 
2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 
3 接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action 
4 如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy 
5 ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类 
6 ActionProxy创建一个ActionInvocation的实例。 
7 ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。 
8 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可 能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2 框架中继承的标签。在这个过程中需要涉及到ActionMapper 

在上述过程中所有的对象(Action,Results,Interceptors,等)都是通过ObjectFactory来创建的。

简单来说就是 客户端请求--struts2拦截器--ActionMapper确定要调用的action--action处理之后--视图--用户显示


接着我们来创建第一个struts2应用程序:

首先导入jar包(如果jar包都不会找的话,不学也罢):第一个应用程序基础jar包:


2:在web.xml中配置struts2的拦截器,以便于用户的请求可以交给struts2的框架处理,这点很重要,如果不配置的话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">
  <display-name></display-name>	
  
      <filter>
      <!-- -struts2的拦截器的类全名称很重要,一定要配置 -->
        <filter-name>Struts2.0</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
 
        <filter-name>Struts2.0</filter-name>
              <!-- -过滤路径-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
  
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3:接着在项目路径src下导入struts.xml配置文件,这个是struts2自己的配置文件,用户在这里注册action和修改系统配置,实现action和视图的处理:

<?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 >
<constant name="struts.multipart.maxSize" value="1000000000"/>
<!-- 修改默认上传大小 -->
   <package name="base" extends="struts-default">
       	<global-results>
			<result name="success">/WEB-INF/page/err.jsp</result>
		</global-results>
    </package>



    <!-- Add packages here 
   <include file="com/leige/demo/struts-Elployee.xml"></include>
<include file="com/leige/demo/struts-hello.xml"></include>
<include file="com/leige/demo/UpLoad.xml"></include>
 <include file="com/leige/demo/struts-PrivilegeIn.xml"></include>
  <include file="com/leige/demo/struts-User.xml"></include>
  <include file="com/leige/demo2/struts-Login.xml"></include>
    -->
<package name="demo" extends="base" namespace="/">
<action name="hello_*" class="com.leige.demo.Hello" method="{1}">
<result name="success">

/show.jsp
</result>
</action>
</package>
</struts>





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值