再次认识Struts2(一)

Struts2  web MVC框架  

m:action  v:view c:action StrutsPrepareAndExecuteFilter 

与Struts1和webwork的关系   Struts2=Struts1+webwork

核心:XWork   核心功能:前端拦截器(interceptor) 运行时表单属性验证 类型转换 表达式语言(OGNL)IoC反转控制(Inversion of Control)

流程:index.jsp---->helloAction---->hellor.jsp


框架目录结构

 apps:例子程序  apps下的struts2-blank示例程序中拷贝

 docs:文档

 lib:框架所需的jar和插件包

 src:core 源代码

           xwork:Struts2底层使用了xwork

核心:前端核心控制器  filter     

 Struts2框架能执行,必须先加载StrutsPrepareAndExecuteFilter

  在StrutsPrepareAndExecuteFilter的init方法 对 Dispatcher进行初始化,并描述了struts2配置文件的加载顺序

  1.org.apache.struts2.default.properties文件        作用:定义了struts2中的所有常量     

  2.位于struts2-core.jar下的struts-default.xml       作用:配置了bean、interceptor、result等    

  3.struts-plugin.xml    作用:插件的配置文件

     struts.xml 作用:struts2所使用的配置文件。

     自定义的struts.properties   自定义常量。
  4.web.xml
在开发中,后加载文件中的配置会将先加载文件中的配置覆盖。


在struts.xml中配置 action

<package name="default"namespace="/"extends="struts-default">

  <action name="hello"class=""method="">

      <result name="good">/hello.jsp</result> 

  </action>

</package>

package  声明一个包,用于管理action    

         name  声明包名  包名不能重复

         namespace  包所在空间   默认值为 “”

         extends 继承的包名

         abstract 值为true 表明该包可以被继承

action  声明一个action

          name 声明action名称

          class action类名称    默认值为 com.opensumphony.xwork2.ActionSupport

          method 需要访问的action类中的方法名称 请求处理方法   此方法无参数 返回值为String       默认值为execute

 result  用于确定返回结果路径

            name  值 与action类中 method方法 返回值做对比   确定跳转路径          默认值为“success”

  action 路径问题       根据本身路径和package下的namespace确定

 访问 localhost/test/a/b/c/hello       查找name=hellp的 action顺序为  /a/b/c    /a/b   /a    /  查找不到404

  配置默认的action    用于处理其它action处理不了的路径    <default-action-ref  name="该action的名称"/>

  默认的action处理类    action的属性不写  默认为 com.opensymphory.xwork2.ActionSupport

  配置包下的action默认处理类    <default-class-ref   class="cn,demo"/>


  常量配置

  一般在  struts.xml中配置   <constant name="常量名称" value=“常量值”></constant>

  基本不使用  struts.properties中配置

  也可以在web.xml 中使用 StrutsPrepareAndExecuteFilter 初始化参数来配置   

   <init-param>  

               <param-name>struts.action.extension</param-name>  

                <param-value>do,,</param-value>

    </init-param>          

   常用常量

    struts.action.extension=action,,    用于指定struts2框架默认拦截的后缀名      .action或者什么都没有

    <constant name="struts.i18n.encoding" value="UTF-8"/>相当于request.setCharacterEncoding("UTF-8"); 解决post请求乱码

    <constant name="struts.serve.static.browserCache"value="false"/> 为true 浏览器会缓存静态内容 一般开发环境设置为false

    <constant name="struts.devMode"value="true"/>提供详细报错页面,便于查看      修改struts.xml后也不需要手动重启服务器

   

struts.xml文件的分离

    方便阅读       让一个模块配置一个配置文件   

    在struts.xml文件中通过 <include file="test.xml"/>


创建action类

1创建pojo类 (plain old java objects) 只继承object类,没有实现任何借口的类

  优点:无耦合  缺点:任何工作都要自己实现

2创建类,实现Action接口  com.opensymphony.xwork2.Action 

   优点:耦合低      提供了5中视图(SUCCESS NONE ERROR INPUT LOGIN)

   缺点:实现工作多 

3创建类 继承ActionSupport类(该类实现了Action接口)   com.opensymphony.xwork2.ActionSupport

    优点:提供表单验证、错误信息设置、读取国际化信息等功能  开发频率较高

    缺点:耦合度较高

在struts.xml中设置 访问action

   1通过设置method的值,来确定访问action类中的哪一个方法  <action name=""book_add"class="cn.Book"method=”add“></action>

        当访问的url是book_add时,就会调用Book类中的add方法

    2通过通配符来简化配置  必须定义统一的命名规范  不建议使用过多的通配符

     book.jsp

     <a href="${pageContext.request.contextPath}/Book_add">book add</a></br>

     <a href="$(pageContext.request.contextPath}/Book_update)">book_update</a></br>

      在struts.xml文件中 *_*    第一个为Book  第二个为add     {1}Action ===BookAction   method={2}    =====method=add

     3动态方法调用 不建议使用

       必须先在default.properties的配置文件中设置   struts.enable.DynamicMethodInvocation=true

       在struts.xml 中  <action name="book" class="cn.BookAction"></action>

       访问路径  http://localhost/test/book!add

       通过动态方法调用  book!add  调用到 BookAction 中的add方法

在action中获取servlet api

   不建议直接使用 servlet api

   1 通过ActionContext 对象来获取

        extends  ActionSupport 

        创建ActionContext 对象   ActionContext context=ActionContext.getContext();   

        获取servlet api (不是真正的Servlet api 而是一个map集合)    

            context.getApplication(); 获取到的为map集合

            context.getSession();获取到的为map集合

            context.getParameter(); 相当于request.getParameterMap();

            context.put(String,Object) 相当于 request.setAttribute(String,String);

    2 通过注入方式获取  

       要求action必须实现指定接口 获取指定对象

       implements ServletContextAware    注入ServletContext 对象

       implements ServletRequestAware    注入request 对象

       implements ServletResponseAware  注入response 对象

       重写接口中的方法

       private HttpServletRequest request

     使用接口中方法的参数对声明的指定对象赋值

       public void setServletRequest(HttpServletRequest request){

          this.request=request;  

        }

   3 通过ServletActionContext获取   

      在ServletActionContext中方法都是static的  

             getRequest();

             getResponse();

             getPageContext();

   

  result标签    

   name值为 method的返回值  进行跳转

    type 用于定义跳转的方式 

        dispatcher  默认值   从action跳转到页面

        chain 请求转发  从action跳转到另一个action

        redirect  重定向   从action跳转到页面

        redirectAction 从action跳转到另一个action

        stream   从服务器返回一个流  一般用于下载

     在package下部署全局结果页面

     <global-results>

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

     </global-results>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值