struts开发流程

1,引入jar包

2,配置web.xml

3,开发action类

4,配置struts.xml

 

版本: 2.3

引入jar文件

commons-fileupload-1.2.2.jar   【文件上传相关包】

commons-io-2.0.1.jar

struts2-core-2.3.4.1.jar           【struts2核心功能包】

xwork-core-2.3.4.1.jar           【Xwork核心包】

ognl-3.0.5.jar        【Ognl表达式功能支持表】

commons-lang3-3.1.jar          【struts对java.lang包的扩展】

freemarker-2.3.19.jar            【struts的标签模板库jar文件】

javassist-3.11.0.GA.jar           【struts对字节码的处理相关jar】

配置web.xml

Tomcat启动-》加载自身web.xml---》加载所有项目的web.xml

通过在项目的web.xml中引入过滤器,

-》Struts的核心功能的初始化,通过过滤器完成

》 filter 【

init/      启动执行

doFilter/   访问执行

destroy

<!-- 引入struts核心过滤器 -->

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

struts2-core-2.3.4.1.jar

StrutsPrepareAndExecuteFilter  即为核心过滤器

注意:

使用的struts的版本不同,核心过滤器类是不一样的!

  • 开发Action

注意:

1. action类,也叫做动作类; 一般继承ActionSupport类

即处理请求的类  (struts中的action类取代之前的servlet)

2. action中的业务方法,处理具体的请求

-》 必须返回String

》 方法不能有参数

public class HelloAction extends ActionSupport {

// 处理请求

public String execute() throws Exception {}

}

  • 配置struts.xml

  引入struts.xml文档配置总struts.xml文件(src目录下)

  <struts>

    <include file="其他struts.xml文件路径"></include>

  </struts>

  当前包新建struts.xml文件配置action

  <struts name="名字任意" extends="struts-default"><package>>action name="与请求路径中名字一致" class="要处理的action类" method="处理的方法|"><result name="返回的标记">结果页面</result></action></package></struts>

struts执行l流程

  

  服务器启动:

    1. 加载项目web.xml

    2. 创建Struts核心过滤器对象, 执行filter --> init()

      struts-default.xml,    核心功能的初始化

      struts-plugin.xml,     struts相关插件

      struts.xml    用户编写的配置文件

  访问:

    3. 用户访问Action, 服务器根据访问路径名称,找对应的aciton配置, 创建action对象

    4.执行默认拦截器栈中定义的18个拦截器

    5. 执行action的业务处理方法

  

每个 action 都必须有一个 name 属性, 该属性和用户请求 servletPath 之间存在着一一对应关系

二:创建action方法

  1.继承actionsupport

  2.实现action接口

  3.不继承任何类不实现任何接口

    class ActionTest{

      private Strign name;

      public void setName(String name){

          this.name = name;

      }

    public String login(){

      return "success";

    }

  }

    url中后跟上?name=xxx

三 通配符的使用

    在struts中可以用*与}{1}优化配置

四 struts中路径匹配原则

    1、获得请求路径的URI,例如url是:
         /Struts2_01/hello_a/a/b/helloWorld.action
    2、首先查询namespace为/hello_a/a/b的package,
          如果存在这个package,则在这个package中查询名字为helloWorld的
          action,如果不存在这个package则转步骤3
    3、查询namespace为/hello_a/a的package,
          如果存在这个package,则在这个package中寻找名字为helloWorld的
          action,如果不存在这个package,则转步骤4
    4、查询namespace为/hello_a的package,
          如果存在这个package,则在这个package中寻找名字为helloWorld的
          action,如果仍然不存在这个package,则转步骤5
    5、查询默认的namaspace的package
          查询名字为helloWorld的action(默认的命名空间为空字符串“/” )
          如果还是找不到,页面提示404找不到action的异常。

五 修改默认后缀

  struts2中默认是.action

  如何修改默认访问后缀

    1,struts的action访问后缀在哪里定义

      struts-core-2.3.4-1.jar'/org.apache.struts2/defaule.properties

      struts.action.extension=action

    2,在struts全局配置中通过常量修改

      <constant name=:"struts.action.extension"  value=“”action,,”></constant> //后缀为action或是没有

          value="action"//后缀只能是action value="action,do,"//后缀为action或是do或是没有

六  动态方法调用

    首先开启动态方法

          该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为 false
              <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

    在地址栏中后面输入actionname+! 方法(即为动态调用方法)

七 全局跳转视图配置

   <global-result>

      <result name = "success">/index.jsp</result>  

   </global=result>   //寻找的时候先去action中找然后在去全局配置中找

            //放置的时候要防止所有的action前面

八 配置各项默认值

    class默认访问struts-default中配置(default-cllss-ref class="com.opensymphony.xwork2.ActionSupport")

      actionsupport

    method默认访问execute()方法

    execute返回默认success去对应的全局视图中去找 找不见返回404错误页面

   问题:什么时候不配置class  即处理的action

      答案:当访问WEB-INF下的资源的时候

      <action name="xxx" >

        <result name = "success">/WEB-INT/index.jsp</result>  //记住不能加 入 type ="redirect"因为在重定向中不能直接访问web-inf下的资源 必须要在服务器内部访问

      </action>

    

  

struts2配置 匹配原则 配置各项默认的更多相关文章

  1. struts2视频学习笔记 03-06(Struts 2配置文件无提示问题,Action配置中的各项默认值,各种转发类型)

    课时3 解决Struts 2配置文件无提示问题(eclipse):window→preference→XML→XML Catlog

  2. Struts2的动态Action和全局跳转视图以及配置各项默认值

    1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器) 2:Struts2中常用的常量介绍:<!-- 一:全局配置 ...

  3. 03. struts2中Action配置的各项默认值

    Action中的各项默认值 Action各项配置 <action name="helloworld" class="com.liuyong666.action.He ...

  4. Struts2笔记——struts.xml配置详解

    访问HelloWorld应用的路径的设置 * 在struts1中,通过<action path=“/primer/helloWorldAction.action”>节点的path属性指定访 ...

  5. Struts2(五)常量的配置

    Struts2 常量大多在 默认的配置文件中已经配置好,但根据用户的需求不同,开发的要求不同,需要修改这些常量值,修改的方法就是在配置的文件对常量进行重新配置 在struts.xml 文件中使用< ...

  6. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

  7. struts2 的struts.xml配置详解

    在应用struts框架进行开发时,必不可少的一步就是对struts.xml进行配置,对于该文件了解越多,我们开发起一应用程序定会更加顺手.下面我们看一下struts.xml的内容,每一项都有什么作用. ...

  8. Struts2 拦截器具体配置过程

    拦截器差点儿遍布每个程序中,所以贴出拦截器配置的具体过程,希望可以帮到大家. Struts2 拦截器具体配置过程 <interceptors> <!-- 先定义拦截器 --> ...

  9. Struts2学习笔记(二)——配置详解

    1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...

随机推荐

  1. 如何安全使用公共Wifi,防止信息泄露?

    购物中心,机场,餐厅,咖啡馆,图书馆,公共交通,酒店客房均提供免费无线网络连接,这些网络每天被数百万人使用.然而,大多数人没有意识到的是,免费的公共Wi-Fi并不安全.即使需要密码才能登录,这并不一定 ...

  2. HTML5 Canvas绘制的下雪效果

    在HTML页面的HEAD区域直接引入snow.js即可,如下:<script type="text/javascript" src="js/snow.js" ...

  3. mmap,malloc分配随机内存

    随机数1G #cat malloc_rand_1g.c #include <stdio.h> /* printf, scanf, NULL */ #include <stdlib.h ...

  4. -1 深度学习基础caffe

    一.反思 二.反向传播 三.ubuntu安装caffe 四.追踪关键词

  5. 【hihocoder 1308】搜索二·骑士问题

    [题目链接]:http://hihocoder.com/problemset/problem/1308 [题意] [题解] 用bfs处理出3个骑士到每个点的最短路; 然后枚举最后3个骑士到了哪一个点. ...

  6. SpringMVC的DispatcherServlet加载过程

    首先在web.xml中配置容器启动监听器,这样在容器启动后Spring会初始化一个ServletContext,负责加载springmvc的九大组件(调用DispatcherServlet.onRef ...

  7. SGU 185 Two shortest

    Two shortest Time Limit: 500ms Memory Limit: 4096KB This problem will be judged on SGU. Original ID: ...

  8. PatentTips - Optimizing power usage by factoring processor architectural events to PMU

    BACKGROUND Processor power consumption has become a major issue in recent years. The current trend o ...

  9. nodejs-mysql模块

    安装mysql模块 1 npm install -g mysql node中使用Mysql模块来执行mysql命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var ht ...

  10. Servlet过滤器和监听器知识总结

    Servlet过滤器是 Servlet 程序的一种特殊用法,主要用来完成一些通用的操作,如编码的过滤.判断用户的登录状态.过滤器使得Servlet开发者能够在客户端请求到达 Servlet资源之前被截 ...